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: create AccountServices module and refactor access controller #194

Merged
merged 17 commits into from
Dec 28, 2024

Conversation

davidzlu
Copy link
Collaborator

@davidzlu davidzlu commented Nov 16, 2024

Note: This PR was originally for refactoring just the get operations in the access controller before it was expanded to refactoring the entire access controller. This is why the branch name is still 139-get-access-operations.

This PR:

  • Creates a new service layer module in models/services/account.ts, refactors the access controller to move business logic into the service layer, and adds unit tests for the service layer.
  • Fixes build error with User type mismatch in app.ts

There was a tsc error where the user type in the passport and and passport-local-mongoose modules didn't match. This was caused by the way Express.User was getting defined in our custom type declaration file. Express.User now extends the CDJ UserType in models to resolve the build error.

Moved code for getting account into new service layer module in
models/services/account.ts and added unit tests for getAccount
@davidzlu davidzlu added refactor Code refactoring test Add or improve testing labels Nov 16, 2024
@davidzlu davidzlu requested a review from hiyaryan November 16, 2024 04:57
@davidzlu davidzlu self-assigned this Nov 16, 2024
app.ts had a build error due to the User type mismatching between the
passport and passport-local-mongoose packages. To fix this, I modified
our custom Express.user type to extend UserType.

This commit also cleans up User and the previous workaround.
backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/src/types/express/index.d.ts Show resolved Hide resolved
backend/src/models/services/account.ts Outdated Show resolved Hide resolved
backend/src/models/services/account.ts Outdated Show resolved Hide resolved
backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
Copy link
Member

@hiyaryan hiyaryan left a comment

Choose a reason for hiding this comment

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

Nice work @davidzlu! Particularly on resolving that build error 😁 I have some feedback and suggested some changes. Please take a look when you get the chance.

Integrate code review comments for getAccount refactor.
Moved code for updating journal into service layer function and
added unit tests for updateJournal
Move business logic for updateAccount into account services.
Split into updateProfile, updatePassword, and updateConfig.
Add unit tests.
@davidzlu davidzlu requested a review from hiyaryan November 29, 2024 19:54
Refactor deleteItem into deleteConfig and deleteAccount service
functions, and write unit tests.
Refactor controller function to move account creation into createAccount
service function.

Move validateJournal call into earlier in middleware chain.

Add unit test for createAccount.
Refactor verifyEmail into verifyEmail service function, and add unit
tests for verifyEmail service function.
Refactor resetPassword in access controller into resetPassword service
function and add unit tests.
Have controller check if error thrown by service function and pass
to error handler instead of creating 500 ExpressError.
Refactor forgotPassword into forgotPassword service function and
rewrite using async/await instead of callback style. Add unit tests.
Refactor login controller handler into several functions and add unit
tests for ensureUserJournal.
@davidzlu davidzlu changed the title refactor: create AccountServices module and refactor getAccount refactor: create AccountServices module and refactor access controller Dec 10, 2024
The arguments for AccountServices.resetPassword were passed in the wrong
order for the resetPassword controller handler. This commit corrects the
order of the arguments.
Copy link
Member

@hiyaryan hiyaryan left a comment

Choose a reason for hiding this comment

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

Really fine work @davidzlu! 🚀 I have tried my best to carefully review everything over the last 3 days and left some suggestions. Please take a look and let me know if you have any questions or would like to discuss anything. The access controller is looking pristine with this service layer! ✨

backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/src/controllers/access/access.ts Outdated Show resolved Hide resolved
backend/src/models/services/account.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
backend/tests/models/services/account.test.ts Outdated Show resolved Hide resolved
Copy link
Member

@hiyaryan hiyaryan left a comment

Choose a reason for hiding this comment

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

Awesome work @davidzlu. Is it strange to say I'm proud of you? Because I'm proud of you! 😁

@hiyaryan hiyaryan merged commit 2a04e29 into 139-ts-migration Dec 28, 2024
@hiyaryan hiyaryan deleted the 139-get-access-operations branch December 28, 2024 03:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Code refactoring test Add or improve testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants