diff --git a/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts b/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts index ea906c79f9..6db2a39994 100644 --- a/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts +++ b/src/app/modules/form/admin-form/__tests__/admin-form.service.spec.ts @@ -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 () => { diff --git a/src/app/modules/form/admin-form/admin-form.controller.ts b/src/app/modules/form/admin-form/admin-form.controller.ts index c2c2ceb8b7..6f052339f2 100644 --- a/src/app/modules/form/admin-form/admin-form.controller.ts +++ b/src/app/modules/form/admin-form/admin-form.controller.ts @@ -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({ diff --git a/src/app/modules/form/admin-form/admin-form.service.ts b/src/app/modules/form/admin-form/admin-form.service.ts index 155119e02f..c568c005c5 100644 --- a/src/app/modules/form/admin-form/admin-form.service.ts +++ b/src/app/modules/form/admin-form/admin-form.service.ts @@ -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 @@ -288,8 +288,8 @@ export const archiveForm = ( }) return transformMongoError(error) - // On success, return true - }).map(() => true) + // On success, return form + }).map((form) => form) } /**