From fd15cdbf40b1d98c01fc90b840078318156e37c7 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Wed, 4 Jan 2023 16:33:13 -0500 Subject: [PATCH] fix(server): log user delete errors (#1255) --- .../src/processors/user-deletion.processor.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/server/apps/microservices/src/processors/user-deletion.processor.ts b/server/apps/microservices/src/processors/user-deletion.processor.ts index b44e630bdd..9b5ef4d5b4 100644 --- a/server/apps/microservices/src/processors/user-deletion.processor.ts +++ b/server/apps/microservices/src/processors/user-deletion.processor.ts @@ -3,6 +3,7 @@ import { APIKeyEntity, AssetEntity, UserEntity } from '@app/database'; import { QueueNameEnum, userDeletionProcessorName } from '@app/job'; import { IUserDeletionJob } from '@app/job/interfaces/user-deletion.interface'; import { Process, Processor } from '@nestjs/bull'; +import { Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Job } from 'bull'; import { join } from 'path'; @@ -11,6 +12,8 @@ import { Repository } from 'typeorm'; @Processor(QueueNameEnum.USER_DELETION) export class UserDeletionProcessor { + private logger = new Logger(UserDeletionProcessor.name); + constructor( @InjectRepository(UserEntity) private userRepository: Repository, @@ -25,14 +28,29 @@ export class UserDeletionProcessor { @Process(userDeletionProcessorName) async processUserDeletion(job: Job) { const { user } = job.data; + // just for extra protection here - if (userUtils.isReadyForDeletion(user)) { + if (!userUtils.isReadyForDeletion(user)) { + this.logger.warn(`Skipped user that was not ready for deletion: id=${user.id}`); + return; + } + + this.logger.log(`Deleting user: ${user.id}`); + + try { const basePath = APP_UPLOAD_LOCATION; const userAssetDir = join(basePath, user.id); + this.logger.warn(`Removing user from filesystem: ${userAssetDir}`); fs.rmSync(userAssetDir, { recursive: true, force: true }); + + this.logger.warn(`Removing user from database: ${user.id}`); await this.apiKeyRepository.delete({ userId: user.id }); await this.assetRepository.delete({ userId: user.id }); await this.userRepository.remove(user); + } catch (error: any) { + this.logger.error(`Failed to remove user`); + this.logger.error(error, error?.stack); + throw error; } } }