Skip to content

Commit

Permalink
fix: remove throw error in map
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin9foong committed Sep 25, 2024
1 parent 0b13dfd commit eb13119
Showing 1 changed file with 77 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,63 +303,68 @@ export const createMultiRespondentFormSubmission = ({
return saveAttachmentsToDbIfExists({
formId: form._id,
attachments: encryptedPayload.attachments,
}).map(async (attachmentMetadata) => {
// Create Incoming Submission
const {
submissionPublicKey,
encryptedSubmissionSecretKey,
encryptedContent,
responseMetadata,
version,
mrfVersion,
} = encryptedPayload

const submissionContent: MultirespondentSubmissionContent = {
form: form._id,
authType: form.authType,
myInfoFields: form.getUniqueMyInfoAttrs(),
form_fields: form.form_fields,
form_logics: form.form_logics,
workflow: form.workflow,
submissionPublicKey,
encryptedSubmissionSecretKey,
encryptedContent,
attachmentMetadata,
version,
workflowStep: 0,
mrfVersion,
}
})
.andThen((attachmentMetadata) => {
// Create Incoming Submission
const {
submissionPublicKey,
encryptedSubmissionSecretKey,
encryptedContent,
responseMetadata,
version,
mrfVersion,
} = encryptedPayload

const submission = new MultirespondentSubmission(submissionContent)
const submissionContent: MultirespondentSubmissionContent = {
form: form._id,
authType: form.authType,
myInfoFields: form.getUniqueMyInfoAttrs(),
form_fields: form.form_fields,
form_logics: form.form_logics,
workflow: form.workflow,
submissionPublicKey,
encryptedSubmissionSecretKey,
encryptedContent,
attachmentMetadata,
version,
workflowStep: 0,
mrfVersion,
}

try {
await submission.save()
} catch (error) {
logger.error({
message: 'Multirespondent submission save error',
meta: logMeta,
error,
})
// eslint-disable-next-line typesafe/no-throw-sync-func
throw new SubmissionSaveError()
}
const submission = new MultirespondentSubmission(submissionContent)

const submissionId = submission.id
logger.info({
message: 'Saved submission to MongoDB',
meta: { ...logMeta, submissionId, responseMetadata },
return ResultAsync.fromPromise(
submission.save().then(() => ({
submission,
responseMetadata,
})),
(error) => {
logger.error({
message: 'Multirespondent submission save error',
meta: logMeta,
error,
})
return new SubmissionSaveError()
},
)
})

// TODO 6395 make responseMetadata mandatory
if (responseMetadata) {
reportSubmissionResponseTime(responseMetadata, {
mode: 'multirespodent',
payment: 'false',
.map(({ submission, responseMetadata }) => {
const submissionId = submission.id
logger.info({
message: 'Saved submission to MongoDB',
meta: { ...logMeta, submissionId, responseMetadata },
})
}

return submission
})
// TODO 6395 make responseMetadata mandatory
if (responseMetadata) {
reportSubmissionResponseTime(responseMetadata, {
mode: 'multirespodent',
payment: 'false',
})
}

return submission
})
}

export const performMultiRespondentPostSubmissionCreateActions = ({
Expand Down Expand Up @@ -453,17 +458,19 @@ export const updateMultiRespondentFormSubmission = ({
})
.map(async (attachmentMetadata) => {
const submission = await MultirespondentSubmission.findById(submissionId)
return { submission, attachmentMetadata }
})
.andThen(({ submission, attachmentMetadata }) => {
if (!submission) {
logger.error({
message: 'Submission not found',
meta: { ...logMeta, submissionId },
})
// eslint-disable-next-line typesafe/no-throw-sync-func
throw new SubmissionNotFoundError()
return errAsync(new SubmissionNotFoundError())
}
return { submission, attachmentMetadata }
return okAsync({ submission, attachmentMetadata })
})
.map(async ({ submission, attachmentMetadata }) => {
.andThen(({ submission, attachmentMetadata }) => {
const {
responseMetadata,
submissionPublicKey,
Expand All @@ -483,18 +490,19 @@ export const updateMultiRespondentFormSubmission = ({
submission.attachmentMetadata = attachmentMetadata
submission.mrfVersion = mrfVersion

try {
await submission.save()
} catch (err) {
logger.error({
message: 'Multirespondent submission save error',
meta: logMeta,
error: err,
})
// eslint-disable-next-line typesafe/no-throw-sync-func
throw new SubmissionSaveError()
}

return ResultAsync.fromPromise(
submission.save().then(() => ({ submission, responseMetadata })),
(error) => {
logger.error({
message: 'Multirespondent submission save error',
meta: logMeta,
error,
})
return new SubmissionSaveError()
},
)
})
.map(({ submission, responseMetadata }) => {
logger.info({
message: 'Saved submission to MongoDB',
meta: { ...logMeta, submissionId: submission.id, responseMetadata },
Expand Down Expand Up @@ -546,8 +554,7 @@ export const performMultiRespondentPostSubmissionUpdateActions = ({
meta: logMeta,
error,
})
// eslint-disable-next-line typesafe/no-throw-sync-func
throw error
return error
})
: ok(false)

Expand All @@ -557,8 +564,7 @@ export const performMultiRespondentPostSubmissionUpdateActions = ({
meta: logMeta,
error: isStepRejectedResult.error,
})
// eslint-disable-next-line typesafe/no-throw-sync-func
throw isStepRejectedResult.error
return errAsync(isStepRejectedResult.error)
}

const isStepRejected = isStepRejectedResult.value
Expand Down

0 comments on commit eb13119

Please sign in to comment.