Skip to content

Commit

Permalink
refactor status change methode
Browse files Browse the repository at this point in the history
  • Loading branch information
TIL-EBP committed Oct 29, 2024
1 parent e43f268 commit bf1c462
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,26 @@ const validatePatch = (user: User, patch: PatchAsset, record?: AssetEditDetail)
);
}

const checkStatusChange = (key: 'internalUse' | 'publicUse', newStatus: string) => {
const hasStatusChanged = (key: 'internalUse' | 'publicUse') => {
const hasChanged = record == null || record[key].statusAssetUseItemCode !== patch[key].statusAssetUseItemCode;
return (
hasChanged &&
newStatus !== 'tobechecked' &&
patch[key].statusAssetUseItemCode !== 'tobechecked' &&
((record != null && !policy.hasRole(Role.MasterEditor, record.workgroupId)) ||
!policy.hasRole(Role.MasterEditor, patch.workgroupId))
);
};

// Specialization of the policy where we disallow the internal status to be changed to anything else than `tobechecked`
// if the current user is not a master-editor for the asset's current or future workgroup.
const hasInternalUSeChanged = checkStatusChange('internalUse', patch.internalUse.statusAssetUseItemCode);
if (hasStatusChanged('internalUse')) {
throw new HttpException("Changing the asset's internalUse status is not allowed", HttpStatus.FORBIDDEN);
}

// Specialization of the policy where we disallow the public status to be changed to anything else than `tobechecked`
// if the current user is not a master-editor for the asset's current or future workgroup.
const hasPublicUSeChanged = checkStatusChange('publicUse', patch.publicUse.statusAssetUseItemCode);

if (hasInternalUSeChanged || hasPublicUSeChanged) {
throw new HttpException("Changing the asset's status is not allowed", HttpStatus.FORBIDDEN);
if (hasStatusChanged('publicUse')) {
throw new HttpException("Changing the asset's public use status is not allowed", HttpStatus.FORBIDDEN);
}

// Specialization of the policy where we disallow the status work item code to be changed to `published`
Expand Down

0 comments on commit bf1c462

Please sign in to comment.