From 3bbf4cddd479c520944ac794c27f02e9ac0a5679 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 29 Mar 2022 15:38:19 +0000 Subject: [PATCH] --- server/AyaNova/biz/UnitModelBiz.cs | 81 +++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/server/AyaNova/biz/UnitModelBiz.cs b/server/AyaNova/biz/UnitModelBiz.cs index c77e6061..64f12e4e 100644 --- a/server/AyaNova/biz/UnitModelBiz.cs +++ b/server/AyaNova/biz/UnitModelBiz.cs @@ -327,27 +327,82 @@ namespace AyaNova.Biz return await GetReportData(dataListSelectedRequest, jobId); } - public async Task> ImportData(AyImportData importData) + public async Task> ImportData(AyImportData importData) { List ImportResult = new List(); - 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(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(); + //Compile linked objects if specified + 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(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(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;