Skip to content
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

refactor(email-submission/controller): refactored email submission #1594

Merged
merged 12 commits into from
Apr 16, 2021

Conversation

seaerchin
Copy link
Contributor

@seaerchin seaerchin commented Apr 8, 2021

Problem

handleEmailSubmission uses alot of mutated variables and if/else, which could have been better written in order to avoid mental overhead on the maintainer and future developers

Solution

This PR refactors the handleEmailSubmission so that it reads in a sequential fashion and there is only one let declared variable.

Design Considerations

  1. let scoped variable - spcpSubmissionFailure. This is because the flag is set only in 1 place and the frontend does do work if it is true. Although there could be a better alternative to avoid the let, it is typed as undefined | true for explicitness and i don't want to significantly refactor the controller just for this.
  2. the generation of hashedFields/parsedResponses is still pretty unwieldy and done as a switch case - this is because MyInfo treats this differently

Manual Tests

  • 1. Create a single field form without authentication and set its mode to email. Submitting the form should have the field data
  • 2. Create a single field SP form and set its mode to email. Submitting the form should have the field data
  • 3. Create a single field CP form and set its mode to email. Submitting the form should have the field data
  • 4. Create a single field MyInfo form and set its mode to email. Submitting the form should have the field data
  • 5. Submit storage mode form to email mode endpoint
  • 6. Submit form which has been made private
  • 7. Submit form which has been archived
  • 8. Submit captcha-enabled form without captcha
  • 9. Submit captcha-disabled form
  • 10. Submit form which has exceeded submission limit
  • 11. Submit form with invalid responses
  • 12. Create multiple email fields with email confirmation, check that the correct confirmation email is sent for each one
  • 13. Submit form with invalid attachments

@karrui karrui marked this pull request as draft April 8, 2021 14:49
@karrui
Copy link
Contributor

karrui commented Apr 12, 2021

@LoneRifle any conflicts here and what you're doing?

@LoneRifle
Copy link
Collaborator

Blocks #1571

@seaerchin seaerchin force-pushed the refactor/handle-email-submission branch from 4abb296 to ee140b9 Compare April 12, 2021 09:42
@seaerchin seaerchin requested review from mantariksh and karrui April 13, 2021 04:50
@seaerchin seaerchin marked this pull request as ready for review April 13, 2021 04:50
@seaerchin
Copy link
Contributor Author

Blocks #1571

@LoneRifle should be fine now; seeing as you will write unit tests for this file, lmk if it reads better/what you'd liek to see improved!

@seaerchin seaerchin force-pushed the refactor/handle-email-submission branch from 7829920 to ad085da Compare April 13, 2021 05:13
)
.andThen(({ parsedResponses, form }) => {
const { authType } = form
switch (authType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

point 3 in design considerations

@seaerchin
Copy link
Contributor Author

@karrui @mantariksh are there any issues apart from the unclear logging?

parsedResponses,
hashedFields: new Set<string>(),
}) as Result<
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might it be better to extract this Result type into a separate type definition, and add it as a generic argument to this particular andThen? so it's very clear exactly what this andThen block should be returning

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree with the first point. extracting it has value, because it helps the reader to understand this whole chunk without getting lost in the details.

for the second point, this is to aid in typehinting for typescript, and even if we were to add it as a generic argument to this particular andThen, we would still need the annotation on the errors just so that type level information is preserved and not lost (because of the ok).

i think this would impact readability just by virtue of how verbose it is (due to the errors) and wouldn't significantly impact clarity because the destructuring of the next andThen makes the shape and types clear

)

// Send email confirmations
return SubmissionService.sendEmailConfirmations({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the intent of the code might be clearer if we prefix this with void and shift it above the call to res.json, to make it more obvious that we're not awaiting on the email confirmations

Copy link
Contributor Author

@seaerchin seaerchin Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i combined it into a single map block and prefixed sendEmailConfirmations with void; main reason is because we need to call orElse on sendEmailConfirmations which requires typehints. The typehinting here is rather verbose due to the parameter forwarding

@seaerchin seaerchin requested review from karrui and mantariksh April 13, 2021 09:25
Copy link
Contributor

@mantariksh mantariksh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you also add manual tests for the following (and anything else you can think of)? can't take any chances with email submissions endpoint.

  • submit storage mode form to email mode endpoint
  • submit form which has been made private
  • submit form which has been archived
  • submit captcha-enabled form without captcha
  • submit captcha-disabled form
  • submit form which has exceeded submission limit
  • submit form with invalid responses
  • create multiple email fields with email confirmation, check that the correct confirmation email is sent for each one

@seaerchin seaerchin requested a review from mantariksh April 14, 2021 04:08
@seaerchin
Copy link
Contributor Author

seaerchin commented Apr 14, 2021

@mantariksh based on the old code, there are the following decision points

  1. check if form is email mode
  2. check if form is public
  3. check if there is a captcha
  4. check if form has hit submission limits
  5. check if attachments are valid
  6. sending of admin email
  7. sending of email confirmation (to person who submits form)

i added 1 test based on point 5; unsure how to test point 7. i think the tests that you've described covers the rest of the decision points and i've verified them on local

@seaerchin seaerchin requested review from mantariksh and karrui April 15, 2021 05:58
Copy link
Contributor

@karrui karrui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm; update your PR description? Remove the logErrorWithMeta block hehe since it is already removed

@seaerchin seaerchin merged commit 0d5d7f9 into develop Apr 16, 2021
@seaerchin seaerchin deleted the refactor/handle-email-submission branch April 16, 2021 06:22
@tshuli tshuli mentioned this pull request Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants