-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: validateAndProcessEncryptSubmission to typescript (#887)
* chore: use correct type for validators * refactor: migrate validateAndProcessEncryptSubmission to ts * refactor: update tests
- Loading branch information
Showing
18 changed files
with
131 additions
and
25 deletions.
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
70 changes: 70 additions & 0 deletions
70
src/app/modules/submission/encrypt-submission/encrypt-submission.middleware.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,70 @@ | ||
import { RequestHandler } from 'express' | ||
import { SetOptional } from 'type-fest' | ||
|
||
import { createReqMeta } from '../../../../app/utils/request' | ||
import { createLoggerWithLabel } from '../../../../config/logger' | ||
import { FieldResponse, WithForm, WithParsedResponses } from '../../../../types' | ||
import { checkIsEncryptedEncoding } from '../../../utils/encryption' | ||
import { getProcessedResponses } from '../submission.service' | ||
|
||
import { mapRouteError } from './encrypt-submission.utils' | ||
|
||
const logger = createLoggerWithLabel(module) | ||
|
||
type EncryptSubmissionBody = { | ||
responses: FieldResponse[] | ||
encryptedContent: string | ||
attachments?: { | ||
encryptedFile?: { | ||
binary: string | ||
nonce: string | ||
submissionPublicKey: string | ||
} | ||
} | ||
isPreview: boolean | ||
version: number | ||
} | ||
|
||
export const validateAndProcessEncryptSubmission: RequestHandler< | ||
{ formId: string }, | ||
unknown, | ||
EncryptSubmissionBody | ||
> = (req, res, next) => { | ||
const { form } = req as WithForm<typeof req> | ||
const { encryptedContent, responses } = req.body | ||
|
||
// Step 1: Check whether submitted encryption is valid. | ||
return ( | ||
checkIsEncryptedEncoding(encryptedContent) | ||
// Step 2: Encryption is valid, process given responses. | ||
.andThen(() => getProcessedResponses(form, responses)) | ||
// If pass, then set parsedResponses and delete responses. | ||
.map((processedResponses) => { | ||
// eslint-disable-next-line @typescript-eslint/no-extra-semi | ||
;(req.body as WithParsedResponses< | ||
typeof req.body | ||
>).parsedResponses = processedResponses | ||
// Prevent downstream functions from using responses by deleting it. | ||
delete (req.body as SetOptional<EncryptSubmissionBody, 'responses'>) | ||
.responses | ||
return next() | ||
}) | ||
// If error, log and return res error. | ||
.mapErr((error) => { | ||
logger.error({ | ||
message: 'Error validating encrypt submission responses', | ||
meta: { | ||
action: 'validateEncryptSubmission', | ||
...createReqMeta(req), | ||
formId: form._id, | ||
}, | ||
error, | ||
}) | ||
|
||
const { statusCode, errorMessage } = mapRouteError(error) | ||
return res.status(statusCode).json({ | ||
message: errorMessage, | ||
}) | ||
}) | ||
) | ||
} |
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
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
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
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
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
4 changes: 2 additions & 2 deletions
4
src/app/utils/field-validation/validators/radioButtonValidator.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
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
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
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