From 951ce8fb2c98db6bfe175efc85f2ebc8cbf93f16 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 29 Mar 2022 15:09:36 +0000 Subject: [PATCH] --- server/AyaNova/biz/PartBiz.cs | 28 +----- server/AyaNova/biz/UnitBiz.cs | 168 +++++++++++++++++++++++++++++++--- 2 files changed, 156 insertions(+), 40 deletions(-) diff --git a/server/AyaNova/biz/PartBiz.cs b/server/AyaNova/biz/PartBiz.cs index 3b9b4470..470917e8 100644 --- a/server/AyaNova/biz/PartBiz.cs +++ b/server/AyaNova/biz/PartBiz.cs @@ -694,33 +694,7 @@ namespace AyaNova.Biz } - // public async Task> ImportData(AyImportData importData) - // { - // List ImportResult = new List(); - // 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(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; - // } - - + //////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/server/AyaNova/biz/UnitBiz.cs b/server/AyaNova/biz/UnitBiz.cs index 6cfc9ae8..03ef405f 100644 --- a/server/AyaNova/biz/UnitBiz.cs +++ b/server/AyaNova/biz/UnitBiz.cs @@ -356,7 +356,7 @@ namespace AyaNova.Biz vc.Add(await ct.Vendor.AsNoTracking().Where(x => x.Id == o.PurchasedFromVendorId).Select(x => x.Name).FirstOrDefaultAsync(), "vendorname", o.PurchasedFromVendorId); o.PurchasedFromVendorViz = vc.Get("vendorname", o.PurchasedFromVendorId); } - + if (o.ContractId != null) { if (!vc.Has("contract", o.ContractId)) @@ -422,24 +422,167 @@ namespace AyaNova.Biz 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.Serial} - {this.GetErrorsAsString()}"); - this.ClearErrors(); + //Compile linked objects if specified + long? ImportCustomerId = -1;//default meaning not included / don't set + if (j["CustomerViz"] != null) + { + //something was specified, may be deliberate attempt to null it out so default to that + ImportCustomerId = null; + if (!JsonUtil.JTokenIsNullOrEmpty(j["CustomerViz"])) + { + //a name was specified so attempt to find it + ImportCustomerId = await ct.Customer.AsNoTracking().Where(z => z.Name == (string)j["CustomerViz"]).Select(x => x.Id).FirstOrDefaultAsync(); + if (ImportCustomerId == 0) + AddError(ApiErrorCode.VALIDATION_REQUIRED, "CustomerViz", $"'{(string)j["CustomerViz"]}'"); + } + } + else + { + AddError(ApiErrorCode.VALIDATION_REQUIRED, "CustomerViz", $"'{(string)j["CustomerViz"]}'"); + } + + long? ImportParentUnitId = -1; + if (j["ParentUnitViz"] != null) + { + ImportParentUnitId = null; + if (!JsonUtil.JTokenIsNullOrEmpty(j["ParentUnitViz"])) + { + ImportParentUnitId = await ct.Unit.AsNoTracking().Where(z => z.Serial == (string)j["ParentUnitViz"]).Select(x => x.Id).FirstOrDefaultAsync(); + if (ImportParentUnitId == 0) + AddError(ApiErrorCode.NOT_FOUND, "ParentUnitViz", $"'{(string)j["ParentUnitViz"]}'"); + } + } + + long? ImportUnitModelId = -1; + if (j["UnitModelNameViz"] != null) + { + ImportUnitModelId = null; + if (!JsonUtil.JTokenIsNullOrEmpty(j["UnitModelNameViz"])) + { + ImportUnitModelId = await ct.UnitModel.AsNoTracking().Where(z => z.Name == (string)j["UnitModelNameViz"]).Select(x => x.Id).FirstOrDefaultAsync(); + if (ImportUnitModelId == 0) + AddError(ApiErrorCode.NOT_FOUND, "UnitModelNameViz", $"'{(string)j["UnitModelNameViz"]}'"); + } + } + + long? ImportPurchasedFromVendorId = -1; + if (j["PurchasedFromVendorViz"] != null) + { + ImportPurchasedFromVendorId = null; + if (!JsonUtil.JTokenIsNullOrEmpty(j["PurchasedFromVendorViz"])) + { + ImportPurchasedFromVendorId = await ct.Vendor.AsNoTracking().Where(z => z.Name == (string)j["PurchasedFromVendorViz"]).Select(x => x.Id).FirstOrDefaultAsync(); + if (ImportPurchasedFromVendorId == 0) + AddError(ApiErrorCode.NOT_FOUND, "PurchasedFromVendorViz", $"'{(string)j["PurchasedFromVendorViz"]}'"); + } + } + + long? ImportReplacedByUnitId = -1; + if (j["ReplacedByUnitViz"] != null) + { + ImportReplacedByUnitId = null; + if (!JsonUtil.JTokenIsNullOrEmpty(j["ReplacedByUnitViz"])) + { + ImportReplacedByUnitId = await ct.Unit.AsNoTracking().Where(z => z.Serial == (string)j["ReplacedByUnitViz"]).Select(x => x.Id).FirstOrDefaultAsync(); + if (ImportReplacedByUnitId == 0) + AddError(ApiErrorCode.NOT_FOUND, "ReplacedByUnitViz", $"'{(string)j["ReplacedByUnitViz"]}'"); + } + } + + + long? ImportContractId = -1; + DateTime? ImportContractExpires = null; + if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"])) + { + ImportContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync(); + if (ImportContractId == 0) + AddError(ApiErrorCode.NOT_FOUND, "ContractViz", $"'{(string)j["ContractViz"]}'"); + + if (JsonUtil.JTokenIsNullOrEmpty(j["ContractExpires"])) + ImportContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be + else + ImportContractExpires = (DateTime)j["ContractExpires"]; + + } + + + + long existingId = await ct.Unit.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 (ImportCustomerId != -1) + Target.ManufacturerId = ImportCustomerId; + + if (ImportParentUnitId != -1) + Target.WholeSalerId = ImportParentUnitId; + + if (ImportUnitModelId != -1) + Target.AlternativeWholeSalerId = ImportUnitModelId; + + 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 (ImportCustomerId != -1) + Target.ManufacturerId = ImportCustomerId; + if (ImportParentUnitId != -1) + Target.WholeSalerId = ImportParentUnitId; + if (ImportUnitModelId != -1) + Target.AlternativeWholeSalerId = ImportUnitModelId; + + 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.Serial} - ok"); + ImportResult.Add($"❌ Exception processing import\n record:{j.ToString()}\nError:{ex.Message}\nSource:{ex.Source}\nStack:{ex.StackTrace.ToString()}"); } } return ImportResult; @@ -448,7 +591,6 @@ namespace AyaNova.Biz - //////////////////////////////////////////////////////////////////////////////////////////////// //JOB / OPERATIONS //