This commit is contained in:
@@ -327,27 +327,82 @@ namespace AyaNova.Biz
|
|||||||
return await GetReportData(dataListSelectedRequest, jobId);
|
return await GetReportData(dataListSelectedRequest, jobId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<List<string>> ImportData(AyImportData importData)
|
public async Task<List<string>> ImportData(AyImportData importData)
|
||||||
{
|
{
|
||||||
List<string> ImportResult = new List<string>();
|
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" }) });
|
var jsset = JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "CustomFields" }) });
|
||||||
foreach (JObject j in importData.Data)
|
foreach (JObject j in importData.Data)
|
||||||
{
|
{
|
||||||
var w = j.ToObject<UnitModel>(jsset);
|
try
|
||||||
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()}");
|
//Compile linked objects if specified
|
||||||
this.ClearErrors();
|
long? ImportVendorId = -1;//default meaning not included / don't set
|
||||||
|
if (j["VendorViz"] != null)
|
||||||
|
{
|
||||||
|
//something was specified, may be deliberate attempt to null it out so default to that
|
||||||
|
ImportVendorId = null;
|
||||||
|
if (!JsonUtil.JTokenIsNullOrEmpty(j["VendorViz"]))
|
||||||
|
{
|
||||||
|
//a name was specified so attempt to find it
|
||||||
|
ImportVendorId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["VendorViz"]).Select(x => x.Id).FirstOrDefaultAsync();
|
||||||
|
if (ImportVendorId == 0)
|
||||||
|
AddError(ApiErrorCode.NOT_FOUND, "VendorViz", $"'{(string)j["VendorViz"]}'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
long existingId = await ct.UnitModel.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<UnitModel>(jsset);
|
||||||
|
Target.Tags.Add(ImportTag);
|
||||||
|
if (ImportVendorId != -1)
|
||||||
|
Target.VendorId = ImportVendorId;
|
||||||
|
var res = await CreateAsync(Target);
|
||||||
|
if (res == null)
|
||||||
|
{
|
||||||
|
ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}");
|
||||||
|
this.ClearErrors();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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<UnitModel>(jsset);
|
||||||
|
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
|
||||||
|
propertiesToUpdate.Remove("Name");
|
||||||
|
ImportUtil.Update(Source, Target, propertiesToUpdate);
|
||||||
|
if (ImportVendorId != -1)
|
||||||
|
Target.VendorId = ImportVendorId;
|
||||||
|
var res = await PutAsync(Target);
|
||||||
|
if (res == null)
|
||||||
|
{
|
||||||
|
ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}");
|
||||||
|
this.ClearErrors();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ImportResult.Add($"✔️ {Target.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
ImportResult.Add($"{w.Name} - ok");
|
ImportResult.Add($"❌ Exception processing import\n record:{j.ToString()}\nError:{ex.Message}\nSource:{ex.Source}\nStack:{ex.StackTrace.ToString()}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ImportResult;
|
return ImportResult;
|
||||||
|
|||||||
Reference in New Issue
Block a user