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

ref: migrate fetch form submission counts flow to Typescript #592

Merged
merged 17 commits into from
Nov 16, 2020

Conversation

karrui
Copy link
Contributor

@karrui karrui commented Nov 5, 2020

Problem

This PR add full flow for GET /{formId}/adminform/submissions/count endpoint. This initial implementation is free of middlewares, and all checks are done in the handler. Authentication middleware is being used for the route now that #568 is in.

Note that the routes are not migrated to typescript yet, and will be done in a separate PR.

Solution

Features:

  • Add SubmissionService#getFormSubmissionsCount neverthrown function
  • Add adminFormUtils#assertHasReadPermissions function to check if user has read permissions.
  • Update app.route('/:formId([a-fA-F0-9]{24})/adminform/submissions/count') handler flow
    • Remove all middleware usage, use new AdminFormController#handleCountFormSubmissions function
    • Add joi validation for valid query.startDate and query.endDate params.

Improvements:

  • move submission module tests into modules/submissions/__tests__ directory
  • add JSDoc + stricter types for dateUtils#createQueryWithDateParam
  • move UserController#getUserIdFromSession to authUtils#getUserIdFromSession

Bug Fixes:

  • Correct typings of SubmissionModel to better follow schema

Tests

  • Add handleCountFormSubmissions tests in admin-form.controller.spec.ts
  • Add getFormSubmissionsCount tests in submission.service.spec.ts

@karrui karrui force-pushed the ref/submission-ctl-service branch 2 times, most recently from 56d8b9b to 0129b96 Compare November 9, 2020 03:23
Base automatically changed from ref/admin-dashboard-form-ts to develop November 9, 2020 08:00
@karrui karrui force-pushed the ref/submission-ctl-service branch from 0129b96 to 52d7771 Compare November 9, 2020 08:01
}

// Successfully retrieved count.
return res.json(countResult.value)
Copy link
Contributor

Choose a reason for hiding this comment

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

if we use isErr like this, isn't it basically as good as try-catch? do you think it might be cleaner to actually chain the results and use a single map and mapErr at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's different from try-catch though. To reach the next step, you have to actively unwrap the Result unlike in try catch where devs may forget to throw.

Doing it this way allows for better error logs, though it also makes sense to chain them and map/mapErr since the service themselves already logs errors

Thoughts, @liangyuanruo?

Copy link
Contributor

@mantariksh mantariksh Nov 16, 2020

Choose a reason for hiding this comment

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

yeah imo it's fine for the logs to get more generic at higher and higher layers, as long as each layer is responsible for logging the error. I think it's better to have cleaner code here by taking advantage of chaining, since there's no significant benefit to having duplicative log messages between the controller and service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We just go back and forth, the initial implementation was chaining, then it became distinct steps, then etc etc HAHAHA. I'll go with this for now, can always open a PR to chain it again. Too many rebases now :P

Copy link
Contributor

Choose a reason for hiding this comment

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

yup this is ok with me too, as long as the Result type is narrowed by testing with isErr(). I wouldn't want to be too opinionated here.

src/app/routes/admin-forms.server.routes.js Outdated Show resolved Hide resolved
src/app/routes/admin-forms.server.routes.js Show resolved Hide resolved
@karrui karrui force-pushed the ref/submission-ctl-service branch from 52d7771 to 711315b Compare November 13, 2020 03:43
@karrui karrui requested a review from mantariksh November 13, 2020 04:10
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.

lgtm, the question on chaining is still outstanding but not a dealbreaker

@karrui karrui force-pushed the ref/submission-ctl-service branch from 711315b to f2479ea Compare November 16, 2020 03:34
@karrui karrui merged commit b233043 into develop Nov 16, 2020
@karrui karrui deleted the ref/submission-ctl-service branch November 16, 2020 05:33
@karrui karrui mentioned this pull request Nov 17, 2020
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.

3 participants