Skip to content

Commit

Permalink
Add deep copy for validation, refactor AssertUpdatablePartnerSharing,…
Browse files Browse the repository at this point in the history
… and ensure safe update with transaction scope (#1076)
  • Loading branch information
adrianwium authored Sep 3, 2024
1 parent 4d58903 commit 32f08f4
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ await _executionStrategyService.ExecuteInExecutionStrategyAsync(async () =>
_organizationService.IsAdmin(request.OrganizationId, true);

var result = GetById(request.Id, true, true, false);
var resultCurrent = ObjectHelper.DeepCopy(result);

AssertUpdatable(result);

Expand Down Expand Up @@ -1161,7 +1162,7 @@ await _executionStrategyService.ExecuteInExecutionStrategyAsync(async () =>

await _executionStrategyService.ExecuteInExecutionStrategyAsync(async () =>
{
await AssertUpdatablePartnerSharing(request, result); //check will abort sharing if possible and needs to be rolled back if the update fails
await AssertUpdatablePartnerSharing(request, resultCurrent); //check will abort sharing if possible and needs to be rolled back if the update fails

using var scope = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled);
result = await _opportunityRepository.Update(result);
Expand Down Expand Up @@ -1602,22 +1603,22 @@ private static void AssertUpdatable(Models.Opportunity opportunity)
throw new ValidationException($"{nameof(Models.Opportunity)} can no longer be updated (current status '{opportunity.Status}'). Required state '{string.Join(" / ", Statuses_Updatable)}'");
}

private async Task AssertUpdatablePartnerSharing(OpportunityRequestUpdate request, Models.Opportunity opportunity)
private async Task AssertUpdatablePartnerSharing(OpportunityRequestUpdate request, Models.Opportunity opportunityCurrent)
{
var reasons = new List<string>();

if (opportunity.ShareWithPartners == true && request.ShareWithPartners != true)
if (opportunityCurrent.ShareWithPartners == true && request.ShareWithPartners != true)
reasons.Add("Option to share with partners cannot be disabled after it has been enabled");

if (opportunity.DateEnd.HasValue && !request.DateEnd.HasValue)
if (opportunityCurrent.DateEnd.HasValue && !request.DateEnd.HasValue)
reasons.Add("End date cannot be removed once it has been set");

if (opportunity.TypeId != request.TypeId)
if (opportunityCurrent.TypeId != request.TypeId)
reasons.Add("Type cannot be changed");

if (reasons.Count == 0) return;

var shared = await _sharingInfoService.IsShared(PartnerSharing.EntityType.Opportunity, opportunity.Id, true);
var shared = await _sharingInfoService.IsShared(PartnerSharing.EntityType.Opportunity, opportunityCurrent.Id, true);
if (!shared) return;

var reasonText = string.Join("; ", reasons);
Expand Down

0 comments on commit 32f08f4

Please sign in to comment.