-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[UTOPIA-1177] added CPO review columns + deleted reviews by other user fix + tests #1447
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/backend/src/modules/pia-intake/helper/update-review-submission-fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { ForbiddenException } from '@nestjs/common'; | ||
import { KeycloakUser } from 'src/modules/auth/keycloak-user.model'; | ||
import { PiaIntakeStatusEnum } from '../enums/pia-intake-status.enum'; | ||
import { Review } from '../jsonb-classes/review'; | ||
import { CpoReview } from '../jsonb-classes/review/cpo-review'; | ||
import { ProgramAreaSelectedRolesReview } from '../jsonb-classes/review/programArea/programAreaSelectedRoleReviews'; | ||
|
||
/** | ||
* @method updateReviewSubmissionFields | ||
* @description | ||
* This method updates the dto with the submission related fields, which needs to be filled in by the server based on the user logged in | ||
*/ | ||
export const updateReviewSubmissionFields = ( | ||
updatedValue: | ||
| Review | ||
| Record<string, ProgramAreaSelectedRolesReview> | ||
| Record<string, CpoReview>, // add more types here | ||
storedValue: | ||
| Review | ||
| Record<string, ProgramAreaSelectedRolesReview> | ||
| Record<string, CpoReview>, | ||
user: KeycloakUser, | ||
key: 'mpo' | string, | ||
allowedInSpecificStatus?: PiaIntakeStatusEnum[] | null, | ||
updatedStatus?: PiaIntakeStatusEnum, | ||
) => { | ||
if (!updatedValue?.[key]) return; | ||
|
||
// overwrite the updated values to include the stored fields that may not be passed by the client | ||
if (updatedValue?.[key]) { | ||
updatedValue[key] = { | ||
...(storedValue?.[key] || {}), | ||
...updatedValue?.[key], | ||
}; | ||
} | ||
|
||
// Scenario 1: User is saving review information for the first time [First time save] | ||
// Scenario 2: User is saving review information for the subsequent times [Editing] | ||
|
||
// Either ways, if the value is changed from the stored one, update the submission fields | ||
if ( | ||
storedValue?.[key]?.isAcknowledged !== | ||
updatedValue?.[key]?.isAcknowledged || | ||
storedValue?.[key]?.reviewNote !== updatedValue?.[key]?.reviewNote | ||
) { | ||
// if it is not the same person updating the fields, throw forbidden error | ||
if ( | ||
storedValue?.[key]?.reviewedByGuid && | ||
storedValue?.[key]?.reviewedByGuid !== user.idir_user_guid | ||
) { | ||
throw new ForbiddenException({ | ||
message: `You do not have permissions to edit this review.`, | ||
}); | ||
} | ||
|
||
if ( | ||
allowedInSpecificStatus && | ||
updatedStatus && | ||
!allowedInSpecificStatus.includes(updatedStatus) | ||
) { | ||
throw new ForbiddenException({ | ||
message: 'You do not permissions to update review in this status', | ||
}); | ||
} | ||
|
||
// if first time reviewed | ||
if (!storedValue?.[key]?.reviewedByGuid) { | ||
updatedValue[key].reviewedAt = new Date(); | ||
updatedValue[key].reviewedByGuid = user.idir_user_guid; | ||
updatedValue[key].reviewedByUsername = user.idir_username; | ||
updatedValue[key].reviewedByDisplayName = user.display_name; | ||
} | ||
|
||
// update last review updated at | ||
updatedValue[key].reviewLastUpdatedAt = new Date(); | ||
} | ||
}; |
95 changes: 95 additions & 0 deletions
95
src/backend/src/modules/pia-intake/jsonb-classes/review/cpo-review.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { IsBoolean, IsString } from '@nestjs/class-validator'; | ||
import { Transform } from 'class-transformer'; | ||
import { UserTypesEnum } from 'src/common/enums/users.enum'; | ||
import { IFormField } from 'src/common/interfaces/form-field.interface'; | ||
import { validateRoleForFormField } from 'src/common/validators/form-field-role.validator'; | ||
import { RoleReview } from './role-review'; | ||
|
||
export class CpoReview extends RoleReview { | ||
// overriding the mandatory fields | ||
@IsBoolean() | ||
isAcknowledged: boolean; | ||
|
||
@IsString() | ||
@Transform(({ value }) => value?.trim()) | ||
reviewNote?: string; | ||
} | ||
|
||
export const cpoReviewMetadata: Array<IFormField<CpoReview>> = [ | ||
{ | ||
key: 'isAcknowledged', | ||
type: 'boolean', | ||
allowedUserTypesEdit: [UserTypesEnum.CPO], | ||
isSystemGeneratedField: false, | ||
}, | ||
{ | ||
key: 'reviewNote', | ||
type: 'text', | ||
allowedUserTypesEdit: [UserTypesEnum.CPO], | ||
isSystemGeneratedField: false, | ||
}, | ||
{ | ||
key: 'reviewedByDisplayName', | ||
type: 'text', | ||
allowedUserTypesEdit: [], // empty array signifies that the client can't edit these fields | ||
isSystemGeneratedField: true, | ||
}, | ||
{ | ||
key: 'reviewedByUsername', | ||
type: 'text', | ||
allowedUserTypesEdit: [], | ||
isSystemGeneratedField: true, | ||
}, | ||
{ | ||
key: 'reviewedByGuid', | ||
type: 'text', | ||
allowedUserTypesEdit: [], | ||
isSystemGeneratedField: true, | ||
}, | ||
{ | ||
key: 'reviewedAt', | ||
type: 'text', | ||
allowedUserTypesEdit: [], | ||
isSystemGeneratedField: true, | ||
}, | ||
{ | ||
key: 'reviewLastUpdatedAt', | ||
type: 'text', | ||
allowedUserTypesEdit: [], | ||
isSystemGeneratedField: true, | ||
}, | ||
]; | ||
|
||
export const validateRoleForCpoReview = ( | ||
updatedValue: CpoReview, | ||
storedValue: CpoReview, | ||
userType: UserTypesEnum[], | ||
path: string, | ||
isDeleted?: boolean, // when review is deleted, only check roles of NON-system generated values | ||
) => { | ||
if (!updatedValue) return; | ||
|
||
let keys = Object.keys(updatedValue) as Array<keyof CpoReview>; | ||
|
||
// if review is deleted, only check role for ONLY user generated keys | ||
if (isDeleted) { | ||
keys = keys.filter((key) => { | ||
const metadata = cpoReviewMetadata.find((m) => m.key === key); | ||
return !metadata.isSystemGeneratedField; | ||
}); | ||
} | ||
|
||
keys.forEach((key) => { | ||
const updatedKeyValue = updatedValue?.[key]; | ||
const storedKeyValue = storedValue?.[key]; | ||
const metadata = cpoReviewMetadata.find((m) => m.key === key); | ||
|
||
validateRoleForFormField( | ||
metadata, | ||
updatedKeyValue, | ||
storedKeyValue, | ||
userType, | ||
`${path}.${key}`, | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be ok to add a comment on what isSystemGeneratedField does or it's purpose ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, makes sense. Done. Added at the definition file -
form-field.interface.ts