This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user