diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index de7d201a3eed05d3d3d8af8c6efc3128c2ab6c4f..6ab176a2a5b68d5ae2fd2504b973c61495376c3f 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -285,6 +285,25 @@ export class AdminController { return this.usersService.findById(setUserJob.userId); } + @UseGuards(JwtAuthGuard, RolesGuard) + @Roles('admin') + @ApiBearerAuth('JWT') + @ApiOperation({ description: 'Remove user job' }) + @ApiResponse({ status: HttpStatus.OK, description: 'Return user profile' }) + @ApiResponse({ status: HttpStatus.BAD_REQUEST, description: 'User does not exist' }) + @Put('removeUserJob') + public async removeUserJob(@Body() removeUserJob): Promise<IUser> { + this.logger.debug(`removeUserJob`); + + const userDocument = await this.usersService.findById(removeUserJob.userId); + if (!userDocument) { + this.logger.warn(`User does not exist: ${removeUserJob.userId}`); + throw new HttpException('User does not exist', HttpStatus.BAD_REQUEST); + } + await this.usersService.removeUserJob(userDocument._id); + return this.usersService.findById(removeUserJob.userId); + } + @UseGuards(JwtAuthGuard, RolesGuard) @Roles('admin') @ApiBearerAuth('JWT') @@ -310,6 +329,24 @@ export class AdminController { return this.usersService.findById(setUserEmployer.userId); } + @UseGuards(JwtAuthGuard, RolesGuard) + @Roles('admin') + @ApiBearerAuth('JWT') + @ApiOperation({ description: 'Remove user employer' }) + @ApiResponse({ status: HttpStatus.OK, description: 'Return user profile' }) + @ApiResponse({ status: HttpStatus.BAD_REQUEST, description: 'User does not exist' }) + @Put('removeUserEmployer') + public async removeUserEmployer(@Body() removeUserEmployer): Promise<IUser> { + this.logger.debug(`removeUserEmployer`); + const userDocument = await this.usersService.findById(removeUserEmployer.userId); + if (!userDocument) { + this.logger.warn(`User does not exist: ${removeUserEmployer.userId}`); + throw new HttpException('User does not exist', HttpStatus.BAD_REQUEST); + } + await this.usersService.removeUserEmployer(userDocument._id); + return this.usersService.findById(removeUserEmployer.userId); + } + @UseGuards(JwtAuthGuard, RolesGuard) @Roles('admin') @ApiBearerAuth('JWT') diff --git a/src/users/services/jobs.service.ts b/src/users/services/jobs.service.ts index db994cfb8b47bb3ce7602d64720c221e71c54603..9eaadfda2fe004f2fd369b6838791d2b24890d3b 100644 --- a/src/users/services/jobs.service.ts +++ b/src/users/services/jobs.service.ts @@ -138,7 +138,11 @@ export class JobsService { if (job) { job.name = newJob.name; job.hasPersonalOffer = newJob.hasPersonalOffer; - if (newJob.jobsGroup) job.jobsGroup = new Types.ObjectId(newJob.jobsGroup._id); + if (newJob.jobsGroup) { + job.jobsGroup = new Types.ObjectId(newJob.jobsGroup._id); + } else { + job.jobsGroup = undefined; + } job.save(); return job; } else { diff --git a/src/users/services/users.service.ts b/src/users/services/users.service.ts index 76d5997ea09c46a32d5fe5519b5835f06b3721af..f6e93cd1d9b3c6de4b56ff0b7356611d3d6c8a58 100644 --- a/src/users/services/users.service.ts +++ b/src/users/services/users.service.ts @@ -788,6 +788,20 @@ export class UsersService { return updated; } + /** + * + * @param userId + */ + public async removeUserJob(userId: Types.ObjectId): Promise<IUser> { + this.logger.debug(`updateUserProfile - RemoveJob | ${userId}`); + const updated = await this.userModel.findByIdAndUpdate({ _id: userId }, { $unset: { job: 1 } }).exec(); + if (updated) { + const populatedResult = await this.findPopulatedUserRegistryById(updated._id); + this.userRegistrySearchService.update(populatedResult); + } + return updated; + } + /** * * @param employer @@ -803,6 +817,20 @@ export class UsersService { return updated; } + /** + * + * @param userId + */ + public async removeUserEmployer(userId: Types.ObjectId): Promise<IUser> { + this.logger.debug(`updateUserProfile - RemoveEmployer | ${userId}`); + const updated = await this.userModel.findByIdAndUpdate({ _id: userId }, { $unset: { employer: 1 } }).exec(); + if (updated) { + const populatedResult = await this.findPopulatedUserRegistryById(updated._id); + this.userRegistrySearchService.update(populatedResult); + } + return updated; + } + /** * Update user details (name, surname and phone number) * @param {UpdateDetailsDto}