case 4240
This commit is contained in:
@@ -1131,45 +1131,49 @@ namespace AyaNova.Biz
|
|||||||
//
|
//
|
||||||
internal async Task<QuoteState> StateCreateAsync(QuoteState newObject)
|
internal async Task<QuoteState> StateCreateAsync(QuoteState newObject)
|
||||||
{
|
{
|
||||||
await StatePreliminaryValidateCanAddAsync(newObject);
|
using (var transaction = await ct.Database.BeginTransactionAsync())//case 4240 wrapped in transaction because header is updated as well as states collection so both need to be in sync
|
||||||
if (HasErrors)
|
|
||||||
return null;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
|
await StatePreliminaryValidateCanAddAsync(newObject);
|
||||||
var quote = await ct.Quote.FirstOrDefaultAsync(x => x.Id == newObject.QuoteId);
|
if (HasErrors)
|
||||||
var NewStatusInfo = await ct.QuoteStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == newObject.QuoteStatusId);
|
|
||||||
|
|
||||||
QuoteStatus LastStatusInfo = null;
|
|
||||||
if (quote.LastStatusId != null)
|
|
||||||
LastStatusInfo = await ct.QuoteStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == quote.LastStatusId);
|
|
||||||
|
|
||||||
//Level 2 validation - Quote status RemoveRoles, SelectRoles and User roles
|
|
||||||
|
|
||||||
//If we have a last role, can it be removed by this User?
|
|
||||||
if (LastStatusInfo != null && CurrentUserRoles.HasAnyFlags(LastStatusInfo.RemoveRoles) == false)
|
|
||||||
{
|
|
||||||
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:QuoteQuoteStatusType -> LT:RemoveRoles");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
else
|
||||||
//Can the new role be selected by this user?
|
|
||||||
if (CurrentUserRoles.HasAnyFlags(NewStatusInfo.SelectRoles) == false)
|
|
||||||
{
|
{
|
||||||
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:QuoteQuoteStatusType -> LT:SelectRoles");
|
|
||||||
return null;
|
var quote = await ct.Quote.FirstOrDefaultAsync(x => x.Id == newObject.QuoteId);
|
||||||
|
var NewStatusInfo = await ct.QuoteStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == newObject.QuoteStatusId);
|
||||||
|
|
||||||
|
QuoteStatus LastStatusInfo = null;
|
||||||
|
if (quote.LastStatusId != null)
|
||||||
|
LastStatusInfo = await ct.QuoteStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == quote.LastStatusId);
|
||||||
|
|
||||||
|
//Level 2 validation - Quote status RemoveRoles, SelectRoles and User roles
|
||||||
|
|
||||||
|
//If we have a last role, can it be removed by this User?
|
||||||
|
if (LastStatusInfo != null && CurrentUserRoles.HasAnyFlags(LastStatusInfo.RemoveRoles) == false)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:QuoteQuoteStatusType -> LT:RemoveRoles");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//Can the new role be selected by this user?
|
||||||
|
if (CurrentUserRoles.HasAnyFlags(NewStatusInfo.SelectRoles) == false)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:QuoteQuoteStatusType -> LT:SelectRoles");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//Seems legit, we'll allow it
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
await ct.QuoteState.AddAsync(newObject);
|
||||||
|
quote.LastStatusId = newObject.QuoteStatusId;
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
newObject.NewQuoteConcurrency = quote.Concurrency;
|
||||||
|
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, AyaType.QuoteStatus, AyaEvent.Created), ct);
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
await StateHandlePotentialNotificationEvent(AyaEvent.Created, newObject);
|
||||||
|
return newObject;
|
||||||
}
|
}
|
||||||
//Seems legit, we'll allow it
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await ct.QuoteState.AddAsync(newObject);
|
|
||||||
quote.LastStatusId = newObject.QuoteStatusId;
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
newObject.NewQuoteConcurrency = quote.Concurrency;
|
|
||||||
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, AyaType.QuoteStatus, AyaEvent.Created), ct);
|
|
||||||
await StateHandlePotentialNotificationEvent(AyaEvent.Created, newObject);
|
|
||||||
return newObject;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1712,54 +1712,59 @@ namespace AyaNova.Biz
|
|||||||
//
|
//
|
||||||
internal async Task<WorkOrderState> StateCreateAsync(WorkOrderState newObject)
|
internal async Task<WorkOrderState> StateCreateAsync(WorkOrderState newObject)
|
||||||
{
|
{
|
||||||
//first validate if this is even a possibility before getting into the further validation
|
using (var transaction = await ct.Database.BeginTransactionAsync())//case 4240 wrapped in transaction because workorder header is updated as well as states collection so both need to be in sync
|
||||||
//for roles can select, can remove etc
|
|
||||||
await StatePreliminaryValidateCanAddAsync(newObject);
|
|
||||||
if (HasErrors)
|
|
||||||
return null;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
if (newObject.UserId == 0)
|
//first validate if this is even a possibility before getting into the further validation
|
||||||
newObject.UserId = UserId;
|
//for roles can select, can remove etc
|
||||||
|
await StatePreliminaryValidateCanAddAsync(newObject);
|
||||||
var wo = await ct.WorkOrder.FirstOrDefaultAsync(x => x.Id == newObject.WorkOrderId);
|
if (HasErrors)
|
||||||
var NewStatusInfo = await ct.WorkOrderStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == newObject.WorkOrderStatusId);
|
|
||||||
WorkOrderStatus LastStatusInfo = null;
|
|
||||||
if (wo.LastStatusId != null)
|
|
||||||
LastStatusInfo = await ct.WorkOrderStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == wo.LastStatusId);
|
|
||||||
|
|
||||||
//Level 2 validation - Work order status RemoveRoles, SelectRoles and User roles
|
|
||||||
|
|
||||||
//If we have a last role, can it be removed by this User?
|
|
||||||
if (LastStatusInfo != null && CurrentUserRoles.HasAnyFlags(LastStatusInfo.RemoveRoles) == false)
|
|
||||||
{
|
|
||||||
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:WorkOrderStatus -> LT:RemoveRoles");
|
|
||||||
return null;
|
return null;
|
||||||
}
|
else
|
||||||
//Can the new role be selected by this user?
|
|
||||||
if (CurrentUserRoles.HasAnyFlags(NewStatusInfo.SelectRoles) == false)
|
|
||||||
{
|
{
|
||||||
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:WorkOrderStatus -> LT:SelectRoles");
|
if (newObject.UserId == 0)
|
||||||
return null;
|
newObject.UserId = UserId;
|
||||||
|
|
||||||
|
var wo = await ct.WorkOrder.FirstOrDefaultAsync(x => x.Id == newObject.WorkOrderId);
|
||||||
|
var NewStatusInfo = await ct.WorkOrderStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == newObject.WorkOrderStatusId);
|
||||||
|
WorkOrderStatus LastStatusInfo = null;
|
||||||
|
if (wo.LastStatusId != null)
|
||||||
|
LastStatusInfo = await ct.WorkOrderStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == wo.LastStatusId);
|
||||||
|
|
||||||
|
//Level 2 validation - Work order status RemoveRoles, SelectRoles and User roles
|
||||||
|
|
||||||
|
//If we have a last role, can it be removed by this User?
|
||||||
|
if (LastStatusInfo != null && CurrentUserRoles.HasAnyFlags(LastStatusInfo.RemoveRoles) == false)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:WorkOrderStatus -> LT:RemoveRoles");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//Can the new role be selected by this user?
|
||||||
|
if (CurrentUserRoles.HasAnyFlags(NewStatusInfo.SelectRoles) == false)
|
||||||
|
{
|
||||||
|
AddError(ApiErrorCode.NOT_AUTHORIZED, "generalerror", "LT:WorkOrderStatus -> LT:SelectRoles");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
//Seems legit, we'll allow it
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
await ct.WorkOrderState.AddAsync(newObject);
|
||||||
|
//Set duration to completed in workorder header
|
||||||
|
//Clear it if set and not completed state
|
||||||
|
//or
|
||||||
|
//Set it if not set and completed state
|
||||||
|
if (NewStatusInfo.Completed && wo.DurationToCompleted == TimeSpan.Zero)
|
||||||
|
wo.DurationToCompleted = DateTime.UtcNow - wo.CreatedDate;
|
||||||
|
else if (wo.DurationToCompleted != TimeSpan.Zero && !NewStatusInfo.Completed)
|
||||||
|
wo.DurationToCompleted = TimeSpan.Zero;
|
||||||
|
wo.LastStatusId = newObject.WorkOrderStatusId;
|
||||||
|
await ct.SaveChangesAsync();
|
||||||
|
newObject.NewWOConcurrency = wo.Concurrency;
|
||||||
|
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, AyaType.WorkOrderStatus, AyaEvent.Created), ct);
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
await StateHandlePotentialNotificationEvent(AyaEvent.Created, newObject);
|
||||||
|
return newObject;
|
||||||
}
|
}
|
||||||
//Seems legit, we'll allow it
|
|
||||||
|
|
||||||
await ct.WorkOrderState.AddAsync(newObject);
|
|
||||||
//Set duration to completed in workorder header
|
|
||||||
//Clear it if set and not completed state
|
|
||||||
//or
|
|
||||||
//Set it if not set and completed state
|
|
||||||
if (NewStatusInfo.Completed && wo.DurationToCompleted == TimeSpan.Zero)
|
|
||||||
wo.DurationToCompleted = DateTime.UtcNow - wo.CreatedDate;
|
|
||||||
else if (wo.DurationToCompleted != TimeSpan.Zero && !NewStatusInfo.Completed)
|
|
||||||
wo.DurationToCompleted = TimeSpan.Zero;
|
|
||||||
wo.LastStatusId = newObject.WorkOrderStatusId;
|
|
||||||
await ct.SaveChangesAsync();
|
|
||||||
newObject.NewWOConcurrency = wo.Concurrency;
|
|
||||||
|
|
||||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, AyaType.WorkOrderStatus, AyaEvent.Created), ct);
|
|
||||||
await StateHandlePotentialNotificationEvent(AyaEvent.Created, newObject);
|
|
||||||
return newObject;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user