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 transfer ownership flow to TypeScript #718

Merged
merged 28 commits into from
Dec 8, 2020

Conversation

karrui
Copy link
Contributor

@karrui karrui commented Nov 24, 2020

Problem

This PR migrates the transfer ownership flow to TypeScript.

Solution

Features:

  • feat(AdminFormSvc): add transferFormOwnership fn
  • feat(AdminFormCtl): add handleTransferFormOwnership handler fn
  • feat(AdminFormRoutes): use new controller fn for transferring owner
    • Old transferOwner Javascript function is removed

Improvements:

  • add tighter pre-validate hook for form schema
  • update Form.transferOwner instance method to return .save()
  • update parameters for Form.transferOwner to accept both a currentOwner and newOwner IUserSchema object, forcing the caller to retrieve those before calling the function

Tests

Add tests for all new functions in the service and controller layer.
Note that the route layer has not been migrated and thus do not have integration tests yet.

  • add missing tests for findAdminById

@karrui
Copy link
Contributor Author

karrui commented Nov 24, 2020

@awhdesmond this touches your code, so please review it if you want to! Will greatly appreciate it :)

Copy link
Contributor

@liangyuanruo liangyuanruo left a comment

Choose a reason for hiding this comment

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

minor changes requested! let me know your thoughts

@karrui
Copy link
Contributor Author

karrui commented Nov 26, 2020

minor changes requested! let me know your thoughts

Updated the responsibility into the service layer. Please rereview and let me know your thoughts. A con of this change is that the service function now looks a little intimidating, even though it's still (imho) fairly readable.

@karrui karrui requested a review from liangyuanruo November 26, 2020 11:35
Copy link
Contributor

@liangyuanruo liangyuanruo left a comment

Choose a reason for hiding this comment

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

Approving first as it's all pretty much there

@karrui karrui force-pushed the ref/transfer-ownership branch from f271810 to 4fe3124 Compare November 30, 2020 07:16
Copy link
Contributor

@liangyuanruo liangyuanruo left a comment

Choose a reason for hiding this comment

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

to refactor error handling in transferFormOwnership function

@karrui karrui force-pushed the ref/transfer-ownership branch from 4fe3124 to 9c11b20 Compare December 2, 2020 03:16
@karrui karrui requested a review from liangyuanruo December 2, 2020 03:17
src/app/modules/user/user.service.ts Outdated Show resolved Hide resolved
src/app/modules/form/admin-form/admin-form.service.ts Outdated Show resolved Hide resolved
src/app/modules/form/admin-form/admin-form.service.ts Outdated Show resolved Hide resolved
src/app/modules/form/admin-form/admin-form.utils.ts Outdated Show resolved Hide resolved
@karrui
Copy link
Contributor Author

karrui commented Dec 7, 2020

Ready for rereview.

@karrui karrui requested a review from mantariksh December 7, 2020 08:50
# Conflicts:
#	src/app/models/form.server.model.ts
#	src/app/modules/form/admin-form/__tests__/admin-form.controller.spec.ts
#	src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts
#	src/app/modules/form/admin-form/admin-form.controller.ts
#	src/app/modules/form/admin-form/admin-form.service.ts
#	src/app/modules/form/admin-form/admin-form.utils.ts
#	src/types/form.ts
#	tests/unit/backend/controllers/admin-forms.server.controller.spec.js
#	tests/unit/backend/models/form.server.model.spec.ts
Comment on lines +451 to +452
this.permissionList =
this.permissionList?.filter((item) => item.email !== newOwner.email) ?? []
Copy link
Contributor

Choose a reason for hiding this comment

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

did we ever figure out if a default value would be provided by mongoose on read, if it wasn't present in the DB?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea, will automatically have an empty array: https://mongoosejs.com/docs/schematypes.html#arrays

Copy link
Contributor

Choose a reason for hiding this comment

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

so this should be safe?

Suggested change
this.permissionList =
this.permissionList?.filter((item) => item.email !== newOwner.email) ?? []
this.permissionList =
this.permissionList.filter((item) => item.email !== newOwner.email)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TypeScript will complain, but that is because it is optional in the schema. Will probably make a IFormDocument type that has all the default optionals populated as required to avoid this then.

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'll do the retyping in a separate PR to avoid polluting this even more

Comment on lines +555 to +559
const validationError = this.invalidate(
'admin',
'Admin for this form is not found.',
) as mongoose.Error.ValidationError
return next(validationError)
Copy link
Contributor

Choose a reason for hiding this comment

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

nice use of invalidate(), this is what we should be doing

Copy link
Contributor

@liangyuanruo liangyuanruo left a comment

Choose a reason for hiding this comment

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

minor nitpicks, feel free to leave aside. lgtm otherwise

# Conflicts:
#	src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts
#	src/app/modules/form/admin-form/admin-form.controller.ts
#	src/app/modules/form/admin-form/admin-form.service.ts
@karrui karrui merged commit b6593cf into develop Dec 8, 2020
@karrui karrui deleted the ref/transfer-ownership branch December 8, 2020 10:23
@mantariksh mantariksh mentioned this pull request Dec 10, 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