This commit is contained in:
2022-03-25 17:10:57 +00:00
parent 79ea7828c7
commit d874948d8e
2 changed files with 109 additions and 11 deletions

View File

@@ -430,26 +430,64 @@ namespace AyaNova.Biz
List<string> ImportResult = new List<string>();
string ImportTag = $"imported-{FileUtil.GetSafeDateFileName()}";
//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)
{
long? existingId = await ct.Customer.AsNoTracking().Where(z => z.Name == (string)j["Name"]).Select(x => x.Id).FirstOrDefaultAsync();
var w = j.ToObject<Customer>(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)
if (existingId == null)
{
ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
this.ClearErrors();
if (importData.DoImport)
{
//import this record
var o = j.ToObject<Customer>(jsset);
o.Tags.Add(ImportTag);
var res = await CreateAsync(o);
if (res == null)
{
ImportResult.Add($"❌ {o.Name} - {this.GetErrorsAsString()}");
this.ClearErrors();
}
else
{
ImportResult.Add($"✔️ {o.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<Customer>(jsset);
var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList();
propertiesToUpdate.Remove("Name");
ImportUpdateObject.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}");
}
// bool copyTags=false;
// if(propertiesToUpdate.Contains("Tags"))
// copyTags=true;
}
}
}
return ImportResult;
}