Skip to content

Commit

Permalink
feat: bulk delete users (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Aug 19, 2024
1 parent 8c5185d commit 718f7bd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/partner-admin/super-admin-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class SuperAdminAuthGuard implements CanActivate {
);
}
try {
const user = await this.userRepository.findOneBy({ firebaseUid: userUid });
const user = await this.userRepository.findOneByOrFail({ firebaseUid: userUid });
request['userEntity'] = user;
return !!user.isSuperAdmin && user.email.indexOf('@chayn.co') !== -1;
} catch (error) {
Expand Down
7 changes: 7 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export class UserController {
return await this.userService.deleteUser(req['user'].user as UserEntity);
}

@ApiBearerAuth('access-token')
@Delete('/bulk-delete')
@UseGuards(SuperAdminAuthGuard)
async bulkDeleteUsers(): Promise<UserEntity[]> {
return await this.userService.bulkDeleteUsers();
}

// This route must go before the Delete user route below as we want nestjs to check against this one first
@ApiBearerAuth('access-token')
@Delete('/cypress')
Expand Down
23 changes: 20 additions & 3 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Logger } from 'src/logger/logger';
import { ServiceUserProfilesService } from 'src/service-user-profiles/service-user-profiles.service';
import { SubscriptionUserService } from 'src/subscription-user/subscription-user.service';
import { TherapySessionService } from 'src/therapy-session/therapy-session.service';
import { SIGNUP_TYPE } from 'src/utils/constants';
import { isProduction, SIGNUP_TYPE } from 'src/utils/constants';
import { FIREBASE_ERRORS } from 'src/utils/errors';
import { FIREBASE_EVENTS, USER_SERVICE_EVENTS } from 'src/utils/logs';
import { ILike, IsNull, Not, Repository } from 'typeorm';
Expand Down Expand Up @@ -327,16 +327,33 @@ export class UserService {
return deletedUsers;
}

public async bulkDeleteUsers(): Promise<UserEntity[]> {
if (isProduction) {
throw new Error('Bulk delete cannot be performed on production database');
}

let deletedUsers: UserEntity[];

try {
const users = await this.userRepository.find();

deletedUsers = await this.batchDeleteUsers(users);
} catch (error) {
this.logger.error(`deleteFilteredUsers - Unable to delete all users`, error);
}
return deletedUsers;
}

public async deleteCypressTestUsers(clean = false): Promise<UserEntity[]> {
let deletedUsers: UserEntity[];
try {
const queryResult = await this.userRepository.find({
const users = await this.userRepository.find({
where: {
email: ILike('%cypresstestemail+%'),
},
});

deletedUsers = await this.batchDeleteUsers(queryResult);
deletedUsers = await this.batchDeleteUsers(users);
} catch (error) {
// If this fails we don't want to break cypress tests but we want to be alerted
this.logger.error(`deleteCypressTestUsers - Unable to delete all cypress users`, error);
Expand Down

0 comments on commit 718f7bd

Please sign in to comment.