diff --git a/server/src/services/users.service.ts b/server/src/services/users.service.ts index 88a2046495..a437c8a905 100644 --- a/server/src/services/users.service.ts +++ b/server/src/services/users.service.ts @@ -155,10 +155,11 @@ export class UsersService { // removing keys with undefined values cleanObject(updatableParams); - await this.updateUser(userId, updatableParams); - const user = await this.findOne(userId); + let user: User; const performUpdateInTransaction = async (manager) => { + await manager.update(User, userId, updatableParams); + user = await manager.findOne(User, { where: { id: userId } }); await this.removeUserGroupPermissionsIfExists(manager, user, removeGroups, organizationId); await this.addUserGroupPermissions(manager, user, addGroups, organizationId); }; diff --git a/server/test/controllers/organization_users.e2e-spec.ts b/server/test/controllers/organization_users.e2e-spec.ts index 46ade25d99..66728fbb09 100644 --- a/server/test/controllers/organization_users.e2e-spec.ts +++ b/server/test/controllers/organization_users.e2e-spec.ts @@ -188,7 +188,7 @@ describe('organization users controller', () => { expect(viewerUserData.user.password).not.toBe('old-password'); }); - it('should allow unarchive if user is already archived', async () => { + it('should not allow unarchive if user status is not archived', async () => { const adminUserData = await createUser(app, { email: 'admin@tooljet.io', status: 'active', @@ -205,11 +205,34 @@ describe('organization users controller', () => { await request(app.getHttpServer()) .post(`/api/organization_users/${developerUserData.orgUser.id}/unarchive/`) .set('Authorization', authHeaderForUser(adminUserData.user)) - .expect(201); + .expect(400); await developerUserData.orgUser.reload(); expect(developerUserData.orgUser.status).toBe('active'); }); + + it('should not allow unarchive if user status is not archived', async () => { + const adminUserData = await createUser(app, { + email: 'admin@tooljet.io', + status: 'active', + groups: ['admin', 'all_users'], + }); + const organization = adminUserData.organization; + const developerUserData = await createUser(app, { + email: 'developer@tooljet.io', + status: 'invited', + groups: ['developer', 'all_users'], + organization, + }); + + await request(app.getHttpServer()) + .post(`/api/organization_users/${developerUserData.orgUser.id}/unarchive/`) + .set('Authorization', authHeaderForUser(adminUserData.user)) + .expect(400); + + await developerUserData.orgUser.reload(); + expect(developerUserData.orgUser.status).toBe('invited'); + }); }); afterAll(async () => {