Skip to content

Commit

Permalink
fix(workspaces): remove form from collab workspace (#6843)
Browse files Browse the repository at this point in the history
* fix: return archived form on successful archive

* fix: iterate and remove form from all collaborators on archive

* docs: update return type of archiveForm
  • Loading branch information
foochifa authored Oct 30, 2023
1 parent 8fc9ceb commit 9de3626
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ describe('admin-form.service', () => {

// Assert
expect(actual.isOk()).toEqual(true)
expect(actual._unsafeUnwrap()).toEqual(true)
expect(actual._unsafeUnwrap()).toEqual(mockArchivedForm)
})

it('should return DatabaseError if any database errors occur', async () => {
Expand Down
19 changes: 15 additions & 4 deletions src/app/modules/form/admin-form/admin-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,12 +823,23 @@ export const handleArchiveForm: ControllerHandler<{ formId: string }> = async (
)
// Step 3: Currently logged in user has permissions to archive form.
.andThen((formToArchive) => AdminFormService.archiveForm(formToArchive))
.andThen(() =>
removeFormsFromAllWorkspaces({
.andThen((archivedForm) => {
// Step 4: For each collaborator, remove the form from their workspaces
archivedForm.permissionList?.forEach(async (permissions) => {
await UserService.findUserByEmail(permissions.email).map(
async (user) =>
await removeFormsFromAllWorkspaces({
formIds: [formId],
userId: user._id,
}),
)
})
// Step 5: remove form from workspace of current user
return removeFormsFromAllWorkspaces({
formIds: [formId],
userId: sessionUserId,
}),
)
})
})
.map(() => res.json({ message: 'Form has been archived' }))
.mapErr((error) => {
logger.warn({
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/form/admin-form/admin-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@ export const extractMyInfoFieldIds = (
/**
* Archives given form.
* @param form the form to archive
* @returns ok(true) if successful
* @returns ok(IFormSchema) if successful
* @returns err(DatabaseError) if any database errors occur
*/
export const archiveForm = (
form: IPopulatedForm,
): ResultAsync<
true,
IFormSchema,
| DatabaseError
| DatabaseValidationError
| DatabaseConflictError
Expand All @@ -288,8 +288,8 @@ export const archiveForm = (
})

return transformMongoError(error)
// On success, return true
}).map(() => true)
// On success, return form
}).map((form) => form)
}

/**
Expand Down

0 comments on commit 9de3626

Please sign in to comment.