From 32210db34d55c673dccde882398c24356d5e083a Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 29 Mar 2022 19:09:52 +0000 Subject: [PATCH] --- devdocs/todo.txt | 2 +- server/AyaNova/biz/CustomerBiz.cs | 72 +++++++++++++++++++++-------- server/AyaNova/biz/HeadOfficeBiz.cs | 60 +++++++++++++++--------- 3 files changed, 91 insertions(+), 43 deletions(-) diff --git a/devdocs/todo.txt b/devdocs/todo.txt index 36a8b283..fe3c05a7 100644 --- a/devdocs/todo.txt +++ b/devdocs/todo.txt @@ -5,7 +5,7 @@ Import / export features TODO: OUTSTANDING FOR IMPORT: - INVENTORY + HeadOffice contract code sb more like unit contract code with null vs empty handling of json CustomerBiz contract code sb mroe like unit contract and updateable (update docs too to show it can be updated currently says no) test each object diff --git a/server/AyaNova/biz/CustomerBiz.cs b/server/AyaNova/biz/CustomerBiz.cs index 6a978c40..552168cb 100644 --- a/server/AyaNova/biz/CustomerBiz.cs +++ b/server/AyaNova/biz/CustomerBiz.cs @@ -437,6 +437,36 @@ namespace AyaNova.Biz { try { + long? ImportContractId = -1; + DateTime? ImportContractExpires = null; + if (j["ContractViz"] != null) + { + ImportContractId = 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? ImportHeadOfficeId = -1; + if (j["HeadOfficeViz"] != null) + { + ImportHeadOfficeId = null; + if (!JsonUtil.JTokenIsNullOrEmpty(j["HeadOfficeViz"])) + { + ImportHeadOfficeId = await ct.HeadOffice.AsNoTracking().Where(z => z.Name == (string)j["HeadOfficeViz"]).Select(x => x.Id).FirstOrDefaultAsync(); + if (ImportHeadOfficeId == 0) + AddError(ApiErrorCode.NOT_FOUND, "HeadOfficeViz", $"'{(string)j["HeadOfficeViz"]}'"); + } + } + long existingId = await ct.Customer.AsNoTracking().Where(z => z.Name == (string)j["Name"]).Select(x => x.Id).FirstOrDefaultAsync(); if (existingId == 0) @@ -447,31 +477,21 @@ namespace AyaNova.Biz var Target = j.ToObject(jsset); Target.Tags.Add(ImportTag); + if (ImportContractId != -1) + { + Target.ContractId = ImportContractId; + Target.ContractExpires = ImportContractExpires; + } + //Set linked objects if (Target.BillHeadOffice) { - if (JsonUtil.JTokenIsNullOrEmpty(j["HeadOfficeViz"])) - AddError(ApiErrorCode.VALIDATION_REQUIRED, "HeadOfficeViz"); + if (ImportHeadOfficeId != -1) + Target.HeadOfficeId = ImportHeadOfficeId; else - { - //if it's not found, it will be zero which will fail validation with a clean error - Target.HeadOfficeId = await ct.HeadOffice.AsNoTracking().Where(z => z.Name == (string)j["HeadOfficeViz"]).Select(x => x.Id).FirstOrDefaultAsync(); - if (Target.HeadOfficeId == 0) - AddError(ApiErrorCode.NOT_FOUND, "HeadOfficeViz", $"'{(string)j["HeadOfficeViz"]}'"); - } + AddError(ApiErrorCode.VALIDATION_REQUIRED, "HeadOfficeViz", "(BillHeadOffice=true)"); } - if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"])) - { - Target.ContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync(); - if (Target.ContractId == 0) - AddError(ApiErrorCode.NOT_FOUND, "ContractViz", $"'{(string)j["ContractViz"]}'"); - if (JsonUtil.JTokenIsNullOrEmpty(j["ContractExpires"])) - Target.ContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be - else - Target.ContractExpires = (DateTime)j["ContractExpires"]; - - } var res = await CreateAsync(Target); if (res == null) @@ -496,6 +516,20 @@ namespace AyaNova.Biz var propertiesToUpdate = j.Properties().Select(p => p.Name).ToList(); propertiesToUpdate.Remove("Name"); ImportUtil.Update(Source, Target, propertiesToUpdate); + if (ImportContractId != -1) + { + Target.ContractId = ImportContractId; + Target.ContractExpires = ImportContractExpires; + } + + if (Target.BillHeadOffice) + { + if (ImportHeadOfficeId != -1) + Target.HeadOfficeId = ImportHeadOfficeId; + else + AddError(ApiErrorCode.VALIDATION_REQUIRED, "HeadOfficeViz", "(BillHeadOffice=true)"); + } + var res = await PutAsync(Target); if (res == null) diff --git a/server/AyaNova/biz/HeadOfficeBiz.cs b/server/AyaNova/biz/HeadOfficeBiz.cs index f6161704..615d3241 100644 --- a/server/AyaNova/biz/HeadOfficeBiz.cs +++ b/server/AyaNova/biz/HeadOfficeBiz.cs @@ -355,6 +355,24 @@ namespace AyaNova.Biz { try { + long? ImportContractId = -1; + DateTime? ImportContractExpires = null; + if (j["ContractViz"] != null) + { + ImportContractId = 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.HeadOffice.AsNoTracking().Where(z => z.Name == (string)j["Name"]).Select(x => x.Id).FirstOrDefaultAsync(); if (existingId == 0) @@ -362,32 +380,23 @@ namespace AyaNova.Biz if (importData.DoImport) { //import this record - var o = j.ToObject(jsset); - o.Tags.Add(ImportTag); - - - if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"])) + var Target = j.ToObject(jsset); + Target.Tags.Add(ImportTag); + if (ImportContractId != -1) { - o.ContractId = await ct.Contract.AsNoTracking().Where(z => z.Name == (string)j["ContractViz"]).Select(x => x.Id).FirstOrDefaultAsync(); - if (o.ContractId == 0) - AddError(ApiErrorCode.NOT_FOUND, "ContractViz", $"'{(string)j["ContractViz"]}'"); - - if (JsonUtil.JTokenIsNullOrEmpty(j["ContractExpires"])) - o.ContractExpires = DateTime.UtcNow.Subtract(new TimeSpan(0, 1, 0));//expired one minute ago to be safe, can't guess what the contract should be - else - o.ContractExpires = (DateTime)j["ContractExpires"]; - + Target.ContractId = ImportContractId; + Target.ContractExpires = ImportContractExpires; } - var res = await CreateAsync(o); + var res = await CreateAsync(Target); if (res == null) { - ImportResult.Add($"❌ {o.Name}\r\n{this.GetErrorsAsString()}"); + ImportResult.Add($"❌ {Target.Name}\r\n{this.GetErrorsAsString()}"); this.ClearErrors(); } else { - ImportResult.Add($"✔️ {o.Name}"); + ImportResult.Add($"✔️ {Target.Name}"); } } } @@ -397,21 +406,26 @@ namespace AyaNova.Biz { //update this record with any data provided //load existing record - var target = await GetAsync((long)existingId); - var source = j.ToObject(jsset); + 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); - var res = await PutAsync(target); + ImportUtil.Update(Source, Target, propertiesToUpdate); + if (ImportContractId != -1) + { + Target.ContractId = ImportContractId; + Target.ContractExpires = ImportContractExpires; + } + var res = await PutAsync(Target); if (res == null) { - ImportResult.Add($"❌ {target.Name} - {this.GetErrorsAsString()}"); + ImportResult.Add($"❌ {Target.Name} - {this.GetErrorsAsString()}"); this.ClearErrors(); } else { - ImportResult.Add($"✔️ {target.Name}"); + ImportResult.Add($"✔️ {Target.Name}"); } } }