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

feat(sms-limiting): sms counts retrieval #2276

Merged
merged 36 commits into from
Jul 14, 2021

Conversation

seaerchin
Copy link
Contributor

@seaerchin seaerchin commented Jun 30, 2021

Problem

Part of allowing sms limiting is to have a way to count the number of free sms that an agency user has sent out thus far. Because the frontend also requires this count to determine the appropriate state on the modal, there is also a new endpoint exposed in this PR

Solution

Relevant technical overview (per the design doc) is here

Some relevant technical choices are listed below for discussion

  1. isOnboardedAccount set using a pre-save hook. This is done so that the updating/setting is automatic, and developers don't have to be concerned with this in the future.
  2. Exposing the endpoint to count is done on SmsController - decided on this because other controllers don't seem too concerned with the sms counts. Arguably, this could be done on AdminFormController or UserController but those seem to be more concerned with the properties of the form/user respectively. (am ok w/ changing if needed tho)

Breaking Changes

  • Yes - this PR contains breaking changes
    • Details ...
  • No - this PR is backwards compatible

Manual Tests
Because this endpoint (and method) will be used subsequently to determine whether the form should be disabled or not on the frontend, this needs to be tested for speed.

the test plan is as follows on staging:

  1. Populate the staging db with 2.5k sms counts.
  2. next, query the endpoint at /:formId([a-fA-F0-9]{24})/verified-sms/count/free
  3. log the time taken for this operation
  4. Repeat the above 3 steps for the various warning limits (2.5k, 5k, 7.5k; 10k for disabled, with different emails)

@seaerchin seaerchin requested review from karrui and mantariksh June 30, 2021 04:14
@seaerchin seaerchin force-pushed the feat/sms-counts-retrieval branch from b153443 to 882365c Compare June 30, 2021 06:36
@karrui
Copy link
Contributor

karrui commented Jun 30, 2021

isOnboardedAccount set using a pre-save hook. This is done so that the updating/setting is automatic, and developers don't have to be concerned with this in the future.

Just take note that our current way of manually onboarding users will not trigger this pre-save hook; will need to add another manual check when we do so until we have an automated way to do this on the GUI

@seaerchin
Copy link
Contributor Author

seaerchin commented Jun 30, 2021

@karrui could you help me understand this? my understanding is that we save the credentials in AWS secrets manager and subsequently, update all forms of the specified user to have the specified message service id. if that's the case, wouldn't this check work because we are checking the form's msgsrvcid, which would already have been updated?

@karrui
Copy link
Contributor

karrui commented Jul 1, 2021

@karrui could you help me understand this? my understanding is that we save the credentials in AWS secrets manager and subsequently, update all forms of the specified user to have the specified message service id. if that's the case, wouldn't this check work because we are checking the form's msgsrvcid, which would already have been updated?

Pre-save hooks don't run when we update using an external GUI such as Robo3T

src/app/routes/api/v3/sms/sms.routes.ts Outdated Show resolved Hide resolved
tests/unit/backend/helpers/jest-db.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms_count.server.model.ts Outdated Show resolved Hide resolved
tests/unit/backend/helpers/jest-db.ts Outdated Show resolved Hide resolved
tests/unit/backend/helpers/jest-db.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms.controller.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms.controller.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms.controller.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms.controller.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms.service.ts Outdated Show resolved Hide resolved
@seaerchin
Copy link
Contributor Author

i think i'll change this so that the endpoint is rooted at /api/v3/admin/forms/:formId/verified-sms/count/free. However, not going to change the controller to sms controller due to point #2 in the PR write up (unless you think it should change also)

also, i'll rebase this off develop (sorry kr) because i didn't take into account config initially, and it's been changed due to feature manager removal.

seaerchin added 16 commits July 1, 2021 17:33
…e counts and msgsrvcid

fix(sms.controller.ts): added mapRouteError for sms controller
@seaerchin seaerchin force-pushed the feat/sms-limiting branch from 02b0b0f to 3870a78 Compare July 1, 2021 09:35
@seaerchin seaerchin force-pushed the feat/sms-counts-retrieval branch from 882365c to fcb489d Compare July 1, 2021 09:35
expect(actualSavedObject).toEqual(expected)
})

it('should save successfully and set isOnboarded to false when sms is sent using default credentials', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should separate the tests into default -> false, custom -> true tests

src/app/services/sms/sms.controller.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms.util.ts Outdated Show resolved Hide resolved
src/app/services/sms/sms.types.ts Show resolved Hide resolved
@seaerchin seaerchin requested a review from karrui July 7, 2021 07:14
@seaerchin seaerchin force-pushed the feat/sms-counts-retrieval branch from 70ba350 to 9d58ab8 Compare July 7, 2021 07:31
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, just need to log that one extra error

@seaerchin
Copy link
Contributor Author

Tested on staging - counts retrieval on staging takes ~70ms for 10k sms counts.

Screenshot 2021-07-14 at 10 35 58 AM

@seaerchin seaerchin merged commit 7f730dd into feat/sms-limiting Jul 14, 2021
@seaerchin seaerchin deleted the feat/sms-counts-retrieval branch July 14, 2021 02:53
seaerchin added a commit that referenced this pull request Aug 2, 2021
* feat(sms_count.server.model): adds new isOnboardedAcc key and scripts to add/delete the key

* refactor(sms.types): adds property that checks if a sms is sent using formsg's credentials

* refactor(sms_count.server.model): adds pre-save hook to automatically set property

* feat(sms.types): updated typings of IVerificationsmscount

* test(sms_count.server): fixed tests and added extra tests for isOnboardedAccount

* feat(sms.service): adds new method to retrieve free sms counts of a user

* test(sms.service): adds new unit tests for sms count  retrieval

* test(sms_count.server.model): updated test case for a client using default credentials

* feat(sms.controller): added new sms controller iwth method to retrieve counts and msgsrvcid

fix(sms.controller.ts): added mapRouteError for sms controller

* feat(routes): exposes new route at sms/:userId/:formId

* fix(sms.controller): removed sms count retrieval requiring the userId as it sohuld be for the admin

* refactor(sms): count retrieval no longer required userId

* test(sms.controller): added unit tests for controller method to retrieve counts

* fix(sms.controller): changed to relative imports in spec to avoid e2e tests breaking

* refactor(sms_count): sms service wraps model count documents call

* test(sms.service): reworked tests to account for mocking of db

* refactor(sms.controller): removed extra middleware from sms controller

* test(sms.controller.spec): renamed sms controller's exposed method in tests

* refactor(sms.routes): removed unused sms route

* refactor(sms.controller): changed counts retrieval to be solely focused on counts

* feat(admin-forms.form.routes): adds new route for retrieving free sms ocunts for admin of a form

* refactor(sms_count): shifts default msgsrvcid to be from config, helper method defined in test

* test(sms.controller.spec): updated tests to use mocks

* test(sms_count.server.model.spec): updated db tests to use smsConfig

* fix(v3.routes): removed extra import statement

* refactor(types): renamed smscounts meta and shifted it to api

renamed to smsCountsDto because it is used to transfer data b/w fe and be. against giving it a more
specific name because this is a type signifying a transfer of sms count data. context for what this
sms counts actually means should be given by the caller

* test(sms_count.server.model.spec): updated test to use sms_config

* docs(sms): updated docs for sms counts retrieval in controller and route

* test(sms.service.spec): made tests more explicit

* refactor(admin-form.controller): shifts over handler for count retrieval from sms to admin-form

* test(admin-form.controller.spec): shifts over associated tests

* refactor(sms_count.server.model.spec): changed tests structure

* chore(sms.util): removed unneeded method

* refactor(sms.types): added in docs for retrieveFreeSmsCounts

* fix(admin-forms.form.routes): removed erroneous import

* fix(sms.service): adds error logging
seaerchin added a commit that referenced this pull request Aug 2, 2021
* feat(sms_count.server.model): adds new isOnboardedAcc key and scripts to add/delete the key

* refactor(sms.types): adds property that checks if a sms is sent using formsg's credentials

* refactor(sms_count.server.model): adds pre-save hook to automatically set property

* feat(sms.types): updated typings of IVerificationsmscount

* test(sms_count.server): fixed tests and added extra tests for isOnboardedAccount

* feat(sms.service): adds new method to retrieve free sms counts of a user

* test(sms.service): adds new unit tests for sms count  retrieval

* test(sms_count.server.model): updated test case for a client using default credentials

* feat(sms.controller): added new sms controller iwth method to retrieve counts and msgsrvcid

fix(sms.controller.ts): added mapRouteError for sms controller

* feat(routes): exposes new route at sms/:userId/:formId

* fix(sms.controller): removed sms count retrieval requiring the userId as it sohuld be for the admin

* refactor(sms): count retrieval no longer required userId

* test(sms.controller): added unit tests for controller method to retrieve counts

* fix(sms.controller): changed to relative imports in spec to avoid e2e tests breaking

* refactor(sms_count): sms service wraps model count documents call

* test(sms.service): reworked tests to account for mocking of db

* refactor(sms.controller): removed extra middleware from sms controller

* test(sms.controller.spec): renamed sms controller's exposed method in tests

* refactor(sms.routes): removed unused sms route

* refactor(sms.controller): changed counts retrieval to be solely focused on counts

* feat(admin-forms.form.routes): adds new route for retrieving free sms ocunts for admin of a form

* refactor(sms_count): shifts default msgsrvcid to be from config, helper method defined in test

* test(sms.controller.spec): updated tests to use mocks

* test(sms_count.server.model.spec): updated db tests to use smsConfig

* fix(v3.routes): removed extra import statement

* refactor(types): renamed smscounts meta and shifted it to api

renamed to smsCountsDto because it is used to transfer data b/w fe and be. against giving it a more
specific name because this is a type signifying a transfer of sms count data. context for what this
sms counts actually means should be given by the caller

* test(sms_count.server.model.spec): updated test to use sms_config

* docs(sms): updated docs for sms counts retrieval in controller and route

* test(sms.service.spec): made tests more explicit

* refactor(admin-form.controller): shifts over handler for count retrieval from sms to admin-form

* test(admin-form.controller.spec): shifts over associated tests

* refactor(sms_count.server.model.spec): changed tests structure

* chore(sms.util): removed unneeded method

* refactor(sms.types): added in docs for retrieveFreeSmsCounts

* fix(admin-forms.form.routes): removed erroneous import

* fix(sms.service): adds error logging
@seaerchin seaerchin mentioned this pull request Aug 2, 2021
24 tasks
seaerchin added a commit that referenced this pull request Aug 2, 2021
* feat(sms-limiting): sms counts retrieval (#2276)

* feat(sms_count.server.model): adds new isOnboardedAcc key and scripts to add/delete the key

* refactor(sms.types): adds property that checks if a sms is sent using formsg's credentials

* refactor(sms_count.server.model): adds pre-save hook to automatically set property

* feat(sms.types): updated typings of IVerificationsmscount

* test(sms_count.server): fixed tests and added extra tests for isOnboardedAccount

* feat(sms.service): adds new method to retrieve free sms counts of a user

* test(sms.service): adds new unit tests for sms count  retrieval

* test(sms_count.server.model): updated test case for a client using default credentials

* feat(sms.controller): added new sms controller iwth method to retrieve counts and msgsrvcid

fix(sms.controller.ts): added mapRouteError for sms controller

* feat(routes): exposes new route at sms/:userId/:formId

* fix(sms.controller): removed sms count retrieval requiring the userId as it sohuld be for the admin

* refactor(sms): count retrieval no longer required userId

* test(sms.controller): added unit tests for controller method to retrieve counts

* fix(sms.controller): changed to relative imports in spec to avoid e2e tests breaking

* refactor(sms_count): sms service wraps model count documents call

* test(sms.service): reworked tests to account for mocking of db

* refactor(sms.controller): removed extra middleware from sms controller

* test(sms.controller.spec): renamed sms controller's exposed method in tests

* refactor(sms.routes): removed unused sms route

* refactor(sms.controller): changed counts retrieval to be solely focused on counts

* feat(admin-forms.form.routes): adds new route for retrieving free sms ocunts for admin of a form

* refactor(sms_count): shifts default msgsrvcid to be from config, helper method defined in test

* test(sms.controller.spec): updated tests to use mocks

* test(sms_count.server.model.spec): updated db tests to use smsConfig

* fix(v3.routes): removed extra import statement

* refactor(types): renamed smscounts meta and shifted it to api

renamed to smsCountsDto because it is used to transfer data b/w fe and be. against giving it a more
specific name because this is a type signifying a transfer of sms count data. context for what this
sms counts actually means should be given by the caller

* test(sms_count.server.model.spec): updated test to use sms_config

* docs(sms): updated docs for sms counts retrieval in controller and route

* test(sms.service.spec): made tests more explicit

* refactor(admin-form.controller): shifts over handler for count retrieval from sms to admin-form

* test(admin-form.controller.spec): shifts over associated tests

* refactor(sms_count.server.model.spec): changed tests structure

* chore(sms.util): removed unneeded method

* refactor(sms.types): added in docs for retrieveFreeSmsCounts

* fix(admin-forms.form.routes): removed erroneous import

* fix(sms.service): adds error logging

* feat(sms-limiting): prerelease (#2500)

* refactor(admin-form/controller): removed env var being sent and changed quota to be on method

* refactor(sms.types): updated disableSms to use mongoose types; updated typing of verification schema

* fix(sms.config): added missing property
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.

2 participants