This commit is contained in:
2022-03-29 19:09:52 +00:00
parent 4d9b17d17a
commit 32210db34d
3 changed files with 91 additions and 43 deletions

View File

@@ -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

View File

@@ -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<Customer>(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)

View File

@@ -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<HeadOffice>(jsset);
o.Tags.Add(ImportTag);
if (!JsonUtil.JTokenIsNullOrEmpty(j["ContractViz"]))
var Target = j.ToObject<HeadOffice>(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<HeadOffice>(jsset);
var Target = await GetAsync((long)existingId);
var Source = j.ToObject<HeadOffice>(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}");
}
}
}