This commit is contained in:
@@ -442,44 +442,44 @@ namespace AyaNova.Biz
|
||||
if (importData.DoImport)
|
||||
{
|
||||
//import this record
|
||||
var o = j.ToObject<Customer>(jsset);
|
||||
o.Tags.Add(ImportTag);
|
||||
var Target = j.ToObject<Customer>(jsset);
|
||||
Target.Tags.Add(ImportTag);
|
||||
|
||||
//Set linked objects
|
||||
if (o.BillHeadOffice)
|
||||
if (Target.BillHeadOffice)
|
||||
{
|
||||
if (JsonUtil.JTokenIsNullOrEmpty(j["HeadOfficeViz"]))
|
||||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "HeadOfficeViz");
|
||||
else
|
||||
{
|
||||
//if it's not found, it will be zero which will fail validation with a clean error
|
||||
o.HeadOfficeId = await ct.HeadOffice.AsNoTracking().Where(z => z.Name == (string)j["HeadOfficeViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (o.HeadOfficeId == 0)
|
||||
Target.HeadOfficeId = await ct.HeadOffice.AsNoTracking().Where(z => z.Name == (string)j["HeadOfficeViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (Target.HeadOfficeId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "HeadOfficeViz", $"'{(string)j["HeadOfficeViz"]}'");
|
||||
}
|
||||
}
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"]))
|
||||
{
|
||||
o.ContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (o.ContractId == 0)
|
||||
Target.ContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (Target.ContractId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "ContractViz", $"'{(string)j["ContractViz"]}'");
|
||||
|
||||
if (JsonUtil.JTokenIsNullOrEmpty(j["ContractExpires"]))
|
||||
o.ContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be
|
||||
Target.ContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be
|
||||
else
|
||||
o.ContractExpires = (DateTime)j["ContractExpires"];
|
||||
Target.ContractExpires = (DateTime)j["ContractExpires"];
|
||||
|
||||
}
|
||||
|
||||
var res = await CreateAsync(o);
|
||||
var res = await CreateAsync(Target);
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {o.Name}\r\n{this.GetErrorsAsString()}");
|
||||
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"✔️ {o.Name}");
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -489,21 +489,21 @@ namespace AyaNova.Biz
|
||||
{
|
||||
//update this record with any data provided
|
||||
//load existing record
|
||||
var target = await GetAsync((long)existingId);
|
||||
var source = j.ToObject<Customer>(jsset);
|
||||
var Target = await GetAsync((long)existingId);
|
||||
var Source = j.ToObject<Customer>(jsset);
|
||||
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
|
||||
propertiesToUpdate.Remove("Name");
|
||||
ImportUtil.Update(source, target, propertiesToUpdate);
|
||||
var res = await PutAsync(target);
|
||||
ImportUtil.Update(Source, Target, propertiesToUpdate);
|
||||
var res = await PutAsync(Target);
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {target.Name} - {this.GetErrorsAsString()}");
|
||||
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"✔️ {target.Name}");
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,7 +313,7 @@ namespace AyaNova.Biz
|
||||
vc.Clear();
|
||||
return ReportData;
|
||||
}
|
||||
|
||||
|
||||
private VizCache vc = new VizCache();
|
||||
|
||||
//populate viz fields from provided object
|
||||
@@ -343,7 +343,7 @@ namespace AyaNova.Biz
|
||||
|
||||
|
||||
|
||||
public async Task<List<string>> ImportData(AyImportData importData)
|
||||
public async Task<List<string>> ImportData(AyImportData importData)
|
||||
{
|
||||
List<string> ImportResult = new List<string>();
|
||||
string ImportTag = ImportUtil.GetImportTag();
|
||||
@@ -363,7 +363,7 @@ public async Task<List<string>> ImportData(AyImportData importData)
|
||||
var o = j.ToObject<HeadOffice>(jsset);
|
||||
o.Tags.Add(ImportTag);
|
||||
|
||||
|
||||
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"]))
|
||||
{
|
||||
o.ContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
|
||||
@@ -447,7 +447,7 @@ namespace AyaNova.Biz
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Any form customizations to validate?
|
||||
var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.Part.ToString());
|
||||
@@ -531,7 +531,7 @@ namespace AyaNova.Biz
|
||||
vc.Add(await ct.Vendor.AsNoTracking().Where(x => x.Id == o.ManufacturerId).Select(x => x.Name).FirstOrDefaultAsync(), "vendorname", o.ManufacturerId);
|
||||
o.ManufacturerViz = vc.Get("vendorname", o.ManufacturerId);
|
||||
}
|
||||
|
||||
|
||||
if (o.AlternativeWholeSalerId != null)
|
||||
{
|
||||
if (!vc.Has("vendorname", o.AlternativeWholeSalerId))
|
||||
@@ -554,33 +554,152 @@ namespace AyaNova.Biz
|
||||
return await GetReportData(dataListSelectedRequest, jobId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
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<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)
|
||||
|
||||
//Compile linked objects if specified
|
||||
long? ImportManufacturerId = -1;//default meaning not included / don't set
|
||||
if (j["ManufacturerViz"] != null)
|
||||
{
|
||||
ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
//something was specified, may be deliberate attempt to null it out so default to that
|
||||
ImportManufacturerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["ManufacturerViz"]))
|
||||
{
|
||||
//a name was specified so attempt to find it
|
||||
ImportManufacturerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["ManufacturerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportManufacturerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "ManufacturerViz", $"'{(string)j["ManufacturerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportWholeSalerId = -1;
|
||||
if (j["WholeSalerViz"] != null)
|
||||
{
|
||||
ImportWholeSalerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["WholeSalerViz"]))
|
||||
{
|
||||
ImportWholeSalerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["WholeSalerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportWholeSalerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "WholeSalerViz", $"'{(string)j["WholeSalerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long? ImportAlternativeWholeSalerId = -1;
|
||||
if (j["AlternativeWholeSalerViz"] != null)
|
||||
{
|
||||
ImportAlternativeWholeSalerId = null;
|
||||
if (!JsonUtil.JTokenIsNullOrEmpty(j["AlternativeWholeSalerViz"]))
|
||||
{
|
||||
ImportAlternativeWholeSalerId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["AlternativeWholeSalerViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||
if (ImportAlternativeWholeSalerId == 0)
|
||||
AddError(ApiErrorCode.NOT_FOUND, "AlternativeWholeSalerViz", $"'{(string)j["AlternativeWholeSalerViz"]}'");
|
||||
}
|
||||
}
|
||||
|
||||
long existingId = await ct.Part.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<Part>(jsset);
|
||||
Target.Tags.Add(ImportTag);
|
||||
|
||||
if (ImportManufacturerId != -1)
|
||||
Target.ManufacturerId = ImportManufacturerId;
|
||||
|
||||
if (ImportWholeSalerId != -1)
|
||||
Target.WholeSalerId = ImportWholeSalerId;
|
||||
|
||||
if (ImportAlternativeWholeSalerId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportAlternativeWholeSalerId;
|
||||
|
||||
var res = await CreateAsync(Target);
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"{w.Name} - ok");
|
||||
if (importData.DoUpdate)
|
||||
{
|
||||
//update this record with any data provided
|
||||
//load existing record
|
||||
var Target = await GetAsync((long)existingId);
|
||||
var Source = j.ToObject<Part>(jsset);
|
||||
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
|
||||
propertiesToUpdate.Remove("Name");
|
||||
ImportUtil.Update(Source, Target, propertiesToUpdate);
|
||||
if (ImportManufacturerId != -1)
|
||||
Target.ManufacturerId = ImportManufacturerId;
|
||||
if (ImportWholeSalerId != -1)
|
||||
Target.WholeSalerId = ImportWholeSalerId;
|
||||
if (ImportAlternativeWholeSalerId != -1)
|
||||
Target.AlternativeWholeSalerId = ImportAlternativeWholeSalerId;
|
||||
|
||||
var res = await PutAsync(Target);
|
||||
|
||||
if (res == null)
|
||||
{
|
||||
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||
this.ClearErrors();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImportResult.Add($"✔️ {Target.Name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ImportResult;
|
||||
}
|
||||
|
||||
|
||||
// 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;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user