Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export const archiveForm = (
*
* @return ok(updated form) if transfer is successful
* @return err(MissingUserError) if the current form admin cannot be found
* @return err(TransferOwnershipError) if new owner cannot be found in the database or new owner email is same as current owner
* @return err(TransferOwnershipError) if new owner cannot be found in the database, new owner email is same as current owner, or new owner email domain is not whitelisted
* @return err(DatabaseError) if any database errors like missing admin of current owner occurs
*/
export const transferFormOwnership = (
Expand Down Expand Up @@ -342,6 +342,31 @@ export const transferFormOwnership = (
}
return okAsync(currentOwner)
})
// Step 1b: Validate that the new owner's email domain is whitelisted.
.andThen((currentOwner) => {
const emailDomain = newOwnerEmail.split('@').pop()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hi there, instead of making a new database query and validation logic, could we reuse the AuthService validateEmailDomain(email) method?

Copy link
Copy Markdown
Author

@faizkhairi faizkhairi Mar 25, 2026

Choose a reason for hiding this comment

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

Yes, sure can! 👌🏼

Copy link
Copy Markdown
Author

@faizkhairi faizkhairi Mar 25, 2026

Choose a reason for hiding this comment

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

I refactored to reuse AuthService.validateEmailDomain() and map InvalidDomainError to TransferOwnershipError to keep the existing error contract. This also gives us the extra validator.isEmail() guard for free.

Hope this helps!

return ResultAsync.fromPromise(
AgencyModel.findOne({ emailDomain }).exec(),
(error) => {
logger.error({
message:
'Error occurred whilst validating new owner email domain',
meta: logMeta,
error,
})
return new DatabaseError(getMongoErrorMessage(error))
},
).andThen((agency) => {
if (!agency) {
return errAsync(
new TransferOwnershipError(
`${newOwnerEmail} is not part of a whitelisted agency`,
),
)
}
return okAsync(currentOwner)
})
})
.andThen((currentOwner) =>
// Step 2: Retrieve user document for new owner.
UserService.findUserByEmail(newOwnerEmail)
Expand Down
Loading