Skip to content

Commit

Permalink
[UTOPIA-769] [Backend] [Bug fix] updated role validation logic when d…
Browse files Browse the repository at this point in the history
…rafter saves after MPO has provided his inputs - with no change in data
  • Loading branch information
kushal-arora-fw committed Feb 15, 2023
1 parent 42f0b93 commit 52a6188
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { IFormField } from '../interfaces/form-field.interface';
*/
export const validateRoleForFormField = <T>(
metadata: IFormField<T>,
value: any,
updatedValue: any,
storedValue: any,
userType: UserTypesEnum,
path: string,
) => {
if (!value) return; // if value not edited - no need to validate permissions
if (!updatedValue) return; // if value not edited - no need to validate permissions

if (typeof updatedValue === 'string' && updatedValue === storedValue) return; // if value is not updated by the current user;

if (!metadata?.allowedUserTypesEdit) return; // if allowedUserTypesEdit is null, all roles can edit this field/key

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@ export const CollectionNoticeMetadata: Array<IFormField<CollectionNotice>> = [
* This method validates role access to CollectionNotice values
*/
export const validateRoleForCollectionNotice = (
collectionNotice: CollectionNotice,
updatedValue: CollectionNotice,
storedValue: CollectionNotice,
userType: UserTypesEnum,
) => {
if (!collectionNotice) return;
if (!updatedValue) return;

const keys = Object.keys(collectionNotice) as Array<keyof CollectionNotice>;
const keys = Object.keys(updatedValue) as Array<keyof CollectionNotice>;

keys.forEach((key) => {
const value = collectionNotice?.[key];
const updatedKeyValue = updatedValue?.[key];
const storedKeyValue = storedValue?.[key];
const metadata = CollectionNoticeMetadata.find((m) => m.key === key);

validateRoleForFormField(
metadata,
value,
updatedKeyValue,
storedKeyValue,
userType,
`collectionNotice.${key}`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,25 @@ export class CollectionUseAndDisclosure {
* This method validates role access to collectionUseAndDisclosure
*/
export const validateRoleForCollectionUseAndDisclosure = (
piaCollectionUseAndDisclosure: CollectionUseAndDisclosure,
updatedValue: CollectionUseAndDisclosure,
storedValue: CollectionUseAndDisclosure,
userType: UserTypesEnum,
) => {
if (!piaCollectionUseAndDisclosure) return;
if (!updatedValue) return;

// steps walkthrough validations
const steps = piaCollectionUseAndDisclosure?.steps;
if (steps?.length) {
steps.forEach((step: StepWalkthrough) => {
validateRoleForStepWalkthrough(step, userType);
const updatedSteps = updatedValue?.steps;
const storedSteps = storedValue?.steps;
if (updatedSteps?.length) {
updatedSteps.forEach((step: StepWalkthrough, i: number) => {
validateRoleForStepWalkthrough(step, storedSteps?.[i], userType);
});
}

// collection notice validations
validateRoleForCollectionNotice(
piaCollectionUseAndDisclosure?.collectionNotice,
updatedValue?.collectionNotice,
storedValue?.collectionNotice,
userType,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,25 @@ export const StepWalkthroughMetadata: Array<IFormField<StepWalkthrough>> = [
* This method validates role access to StepWalkthrough values
*/
export const validateRoleForStepWalkthrough = (
step: StepWalkthrough,
updatedStep: StepWalkthrough,
storedStep: StepWalkthrough,
userType: UserTypesEnum,
) => {
if (!step) return;
if (!updatedStep) return;

const keys = Object.keys(step) as Array<keyof StepWalkthrough>;
const keys = Object.keys(updatedStep) as Array<keyof StepWalkthrough>;

keys.forEach((key) => {
const value = step?.[key];
const updatedValue = updatedStep?.[key];
const storedValue = storedStep?.[key];
const metadata = StepWalkthroughMetadata.find((m) => m.key === key);

validateRoleForFormField(metadata, value, userType, `steps.${key}`);
validateRoleForFormField(
metadata,
updatedValue,
storedValue,
userType,
`steps.${key}`,
);
});
};
10 changes: 6 additions & 4 deletions src/backend/src/modules/pia-intake/pia-intake.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class PiaIntakeService {

// sending DRAFTER to userType as only a drafter can create a new PIA;
// A user could have MPO privileges, however while creating a PIA he/she is acting as a drafter
this.validateJsonbFields(createPiaIntakeDto, UserTypesEnum.DRAFTER);
this.validateJsonbFields(createPiaIntakeDto, null, UserTypesEnum.DRAFTER);

const piaInfoForm: PiaIntakeEntity = await this.piaIntakeRepository.save({
...createPiaIntakeDto,
Expand Down Expand Up @@ -90,7 +90,7 @@ export class PiaIntakeService {
}

// validate jsonb fields for role access
this.validateJsonbFields(updatePiaIntakeDto, userType);
this.validateJsonbFields(updatePiaIntakeDto, existingRecord, userType);

// remove the provided saveId
delete updatePiaIntakeDto.saveId;
Expand Down Expand Up @@ -448,11 +448,13 @@ export class PiaIntakeService {
* 6. additionalRisks
*/
validateJsonbFields(
pia: CreatePiaIntakeDto | UpdatePiaIntakeDto,
updatedValue: CreatePiaIntakeDto | UpdatePiaIntakeDto,
storedValue: PiaIntakeEntity,
userType: UserTypesEnum,
) {
validateRoleForCollectionUseAndDisclosure(
pia?.collectionUseAndDisclosure,
updatedValue?.collectionUseAndDisclosure,
storedValue?.collectionUseAndDisclosure,
userType,
);
// space for future validators, as needed
Expand Down

0 comments on commit 52a6188

Please sign in to comment.