From 1263b4be91ec537770bb137a29362868c2fe90d8 Mon Sep 17 00:00:00 2001 From: bdolor Date: Mon, 6 Nov 2023 12:21:43 -0800 Subject: [PATCH] chore: add submitForReview to backend --- .../lib/resources/opportunity/code-with-us.ts | 66 +++++++++++++++++++ .../lib/resources/opportunity/code-with-us.ts | 1 + 2 files changed, 67 insertions(+) diff --git a/src/back-end/lib/resources/opportunity/code-with-us.ts b/src/back-end/lib/resources/opportunity/code-with-us.ts index a5654e21a..6d261bbbe 100644 --- a/src/back-end/lib/resources/opportunity/code-with-us.ts +++ b/src/back-end/lib/resources/opportunity/code-with-us.ts @@ -81,6 +81,7 @@ interface ValidatedUpdateRequestBody { session: AuthenticatedSession; body: | ADT<"edit", ValidatedUpdateEditRequestBody> + | ADT<"submitForReview", string> | ADT<"publish", string> | ADT<"suspend", string> | ADT<"cancel", string> @@ -441,6 +442,8 @@ const update: crud.Update< evaluationCriteria: getString(value, "evaluationCriteria"), attachments: getStringArray(value, "attachments") }); + case "submitForReview": + return adt("submitForReview", getString(body, "value")); case "publish": return adt("publish", getString(body, "value", "")); case "suspend": @@ -513,6 +516,7 @@ const update: crud.Update< if ( ![ CWUOpportunityStatus.Draft, + CWUOpportunityStatus.UnderReview, CWUOpportunityStatus.Published, CWUOpportunityStatus.Suspended ].includes(cwuOpportunity.status) @@ -697,6 +701,52 @@ const update: crud.Update< }); } } + case "submitForReview": { + if ( + !isValidStatusChange( + cwuOpportunity.status, + CWUOpportunityStatus.UnderReview + ) + ) { + return invalid({ permissions: [permissions.ERROR_MESSAGE] }); + } + // Perform validation on draft to ensure it's ready for publishing + if ( + !allValid([ + genericValidation.validateTitle(cwuOpportunity.title), + genericValidation.validateTeaser(cwuOpportunity.teaser), + genericValidation.validateRemoteOk(cwuOpportunity.remoteOk), + genericValidation.validateRemoteDesc( + cwuOpportunity.remoteDesc, + cwuOpportunity.remoteOk + ), + genericValidation.validateLocation(cwuOpportunity.location), + genericValidation.validateDescription(cwuOpportunity.description) + ]) + ) { + return invalid({ + opportunity: adt("submitForReview" as const, [ + "This opportunity could not be submitted for review because it is incomplete. Please edit, complete and save the form below before trying to publish it again." + ]) + }); + } + + const validatedSubmitNote = opportunityValidation.validateNote( + request.body.value + ); + if (isInvalid(validatedSubmitNote)) { + return invalid({ + opportunity: adt( + "submitForReview" as const, + validatedSubmitNote.value + ) + }); + } + return valid({ + session: request.session, + body: adt("submitForReview", validatedSubmitNote.value) + } as ValidatedUpdateRequestBody); + } case "publish": { if ( !isValidStatusChange( @@ -892,6 +942,22 @@ const update: crud.Update< ); } break; + case "submitForReview": + dbResult = await db.updateCWUOpportunityStatus( + connection, + request.params.id, + CWUOpportunityStatus.UnderReview, + body.value, + session + ); + //Notify of submission + if (isValid(dbResult)) { + cwuOpportunityNotifications.handleCWUSubmittedForReview( + connection, + dbResult.value + ); + } + break; case "publish": { const existingOpportunity = getValidValue( await db.readOneCWUOpportunity( diff --git a/src/shared/lib/resources/opportunity/code-with-us.ts b/src/shared/lib/resources/opportunity/code-with-us.ts index d49b1d57a..ee236383a 100644 --- a/src/shared/lib/resources/opportunity/code-with-us.ts +++ b/src/shared/lib/resources/opportunity/code-with-us.ts @@ -223,6 +223,7 @@ export interface UpdateWithNoteValidationErrors type UpdateADTErrors = | ADT<"edit", UpdateEditValidationErrors> + | ADT<"submitForReview", string[]> | ADT<"publish", string[]> | ADT<"suspend", string[]> | ADT<"cancel", string[]>