This commit is contained in:
2022-03-29 15:54:40 +00:00
parent 5ec7d97ed4
commit 55fa485ba1

View File

@@ -360,24 +360,60 @@ 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<Vendor>(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)
try
{
ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
this.ClearErrors();
long existingId = await ct.Vendor.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<Vendor>(jsset);
Target.Tags.Add(ImportTag);
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<Vendor>(jsset);
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
propertiesToUpdate.Remove("Name");
ImportUtil.Update(Source, Target, propertiesToUpdate);
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;