This commit is contained in:
@@ -694,32 +694,6 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
|
||||
// public async Task<List<string>> ImportData(AyImportData importData)
|
||||
// {
|
||||
// List<string> ImportResult = new List<string>();
|
||||
// string ImportTag = $"imported-{FileUtil.GetSafeDateFileName()}";
|
||||
|
||||
// var jsset = JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "CustomFields" }) });
|
||||
// foreach (JObject j in importData.Data)
|
||||
// {
|
||||
// var w = j.ToObject<Part>(jsset);
|
||||
// if (j["CustomFields"] != null)
|
||||
// w.CustomFields = j["CustomFields"].ToString();
|
||||
// w.Tags.Add(ImportTag);//so user can find them all and revert later if necessary
|
||||
// var res = await CreateAsync(w);
|
||||
// if (res == null)
|
||||
// {
|
||||
// ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
|
||||
// this.ClearErrors();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// ImportResult.Add($"{w.Name} - ok");
|
||||
// }
|
||||
// }
|
||||
// return ImportResult;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -422,24 +422,167 @@ namespace AyaNova.Biz
|
||||
public async Task<List<string>> ImportData(AyImportData importData)
|
||||
{
|
||||
List<string> ImportResult = new List<string>();
|
||||
string ImportTag = $"imported-{FileUtil.GetSafeDateFileName()}";
|
||||
string ImportTag = ImportUtil.GetImportTag();
|
||||
|
||||
//ignore these fields
|
||||
var jsset = JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "CustomFields" }) });
|
||||
|
||||
foreach (JObject j in importData.Data)
|
||||
{
|
||||
var w = j.ToObject<Unit>(jsset);
|
||||
if (j["CustomFields"] != null)
|
||||
w.CustomFields = j["CustomFields"].ToString();
|
||||
w.Tags.Add(ImportTag);//so user can find them all and revert later if necessary
|
||||
var res = await CreateAsync(w);
|
||||
try
|
||||
{
|
||||
//Compile linked objects if specified
|
||||
long? ImportCustomerId = -1;//default meaning not included / don't set
|
||||
if (j["CustomerViz"] != null)
|
||||
{
|
||||
//something was specified, may be deliberate attempt to null it out so default to that
|
||||
ImportCustomerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["CustomerViz"]))
|
||||
{
|
||||
//a name was specified so attempt to find it
|
||||
ImportCustomerId = await ct.Customer.AsNoTracking().Where(z => z.Name == (string)j["CustomerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportCustomerId == 0)
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "CustomerViz", $"'{(string)j["CustomerViz"]}'");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "CustomerViz", $"'{(string)j["CustomerViz"]}'");
|
||||
}
|
||||
|
||||
long? ImportParentUnitId = -1;
|
||||
if (j["ParentUnitViz"] != null)
|
||||
{
|
||||
ImportParentUnitId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ParentUnitViz"]))
|
||||
{
|
||||
ImportParentUnitId = await ct.Unit.AsNoTracking().Where(z => z.Serial == (string)j["ParentUnitViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportParentUnitId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "ParentUnitViz", $"'{(string)j["ParentUnitViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportUnitModelId = -1;
|
||||
if (j["UnitModelNameViz"] != null)
|
||||
{
|
||||
ImportUnitModelId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["UnitModelNameViz"]))
|
||||
{
|
||||
ImportUnitModelId = await ct.UnitModel.AsNoTracking().Where(z => z.Name == (string)j["UnitModelNameViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportUnitModelId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "UnitModelNameViz", $"'{(string)j["UnitModelNameViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportPurchasedFromVendorId = -1;
|
||||
if (j["PurchasedFromVendorViz"] != null)
|
||||
{
|
||||
ImportPurchasedFromVendorId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["PurchasedFromVendorViz"]))
|
||||
{
|
||||
ImportPurchasedFromVendorId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["PurchasedFromVendorViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportPurchasedFromVendorId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "PurchasedFromVendorViz", $"'{(string)j["PurchasedFromVendorViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportReplacedByUnitId = -1;
|
||||
if (j["ReplacedByUnitViz"] != null)
|
||||
{
|
||||
ImportReplacedByUnitId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ReplacedByUnitViz"]))
|
||||
{
|
||||
ImportReplacedByUnitId = await ct.Unit.AsNoTracking().Where(z => z.Serial == (string)j["ReplacedByUnitViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportReplacedByUnitId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "ReplacedByUnitViz", $"'{(string)j["ReplacedByUnitViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
long? ImportContractId = -1;
|
||||
DateTime? ImportContractExpires = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"]))
|
||||
{
|
||||
ImportContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportContractId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "ContractViz", $"'{(string)j["ContractViz"]}'");
|
||||
|
||||
if (JsonUtil.JTokenIsNullOrEmpty(j["ContractExpires"]))
|
||||
ImportContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be
|
||||
else
|
||||
ImportContractExpires = (DateTime)j["ContractExpires"];
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
long existingId = await ct.Unit.AsNoTracking().Where(z => z.Name == (string)j["Name"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
|
||||
if (existingId == 0)
|
||||
{
|
||||
if (importData.DoImport)
|
||||
{
|
||||
//import this record
|
||||
var Target = j.ToObject<Unit>(jsset);
|
||||
Target.Tags.Add(ImportTag);
|
||||
|
||||
if (ImportCustomerId != -1)
|
||||
Target.ManufacturerId = ImportCustomerId;
|
||||
|
||||
if (ImportParentUnitId != -1)
|
||||
Target.WholeSalerId = ImportParentUnitId;
|
||||
|
||||
if (ImportUnitModelId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportUnitModelId;
|
||||
|
||||
var res = await CreateAsync(Target);
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"* {w.Serial} - {this.GetErrorsAsString()}");
|
||||
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"{w.Serial} - ok");
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (importData.DoUpdate)
|
||||
{
|
||||
//update this record with any data provided
|
||||
//load existing record
|
||||
var Target = await GetAsync((long)existingId);
|
||||
var Source = j.ToObject<Unit>(jsset);
|
||||
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
|
||||
propertiesToUpdate.Remove("Name");
|
||||
ImportUtil.Update(Source, Target, propertiesToUpdate);
|
||||
if (ImportCustomerId != -1)
|
||||
Target.ManufacturerId = ImportCustomerId;
|
||||
if (ImportParentUnitId != -1)
|
||||
Target.WholeSalerId = ImportParentUnitId;
|
||||
if (ImportUnitModelId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportUnitModelId;
|
||||
|
||||
var res = await PutAsync(Target);
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ImportResult.Add($"❌ Exception processing import\n record:{j.ToString()}\nError:{ex.Message}\nSource:{ex.Source}\nStack:{ex.StackTrace.ToString()}");
|
||||
}
|
||||
}
|
||||
return ImportResult;
|
||||
@@ -448,7 +591,6 @@ namespace AyaNova.Biz
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//JOB / OPERATIONS
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user