From 5e25eeb883402cde2a686a4c55a8d9624b96a0f8 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Mon, 15 Jul 2024 15:57:22 +0300 Subject: [PATCH] update first and last name endpoint --- src/users/controllers/user.controller.ts | 28 ++++++++++++++++- src/users/dtos/update.user.dto.ts | 14 ++++++--- src/users/services/user.service.ts | 40 ++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/users/controllers/user.controller.ts b/src/users/controllers/user.controller.ts index 3631937..b088b93 100644 --- a/src/users/controllers/user.controller.ts +++ b/src/users/controllers/user.controller.ts @@ -12,6 +12,7 @@ import { UserService } from '../services/user.service'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; import { + UpdateNameDto, UpdateProfilePictureDataDto, UpdateRegionDataDto, UpdateTimezoneDataDto, @@ -92,7 +93,7 @@ export class UserController { @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Put('/timezone/:userUuid') - async updateTimezoneByUserUuid( + async updateNameByUserUuid( @Param('userUuid') userUuid: string, @Body() updateTimezoneDataDto: UpdateTimezoneDataDto, ) { @@ -114,4 +115,29 @@ export class UserController { ); } } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Put('/name/:userUuid') + async updateTimezoneByUserUuid( + @Param('userUuid') userUuid: string, + @Body() updateNameDto: UpdateNameDto, + ) { + try { + const userData = await this.userService.updateNameByUserUuid( + userUuid, + updateNameDto, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'name updated successfully', + data: userData, + }; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } diff --git a/src/users/dtos/update.user.dto.ts b/src/users/dtos/update.user.dto.ts index a195510..1841e87 100644 --- a/src/users/dtos/update.user.dto.ts +++ b/src/users/dtos/update.user.dto.ts @@ -27,15 +27,21 @@ export class UpdateRegionDataDto { Object.assign(this, dto); } } -export class UpdateTimezoneDataDto { +export class UpdateNameDto { @ApiProperty({ - description: 'timezoneUuid', + description: 'firstName', required: true, }) @IsString() @IsNotEmpty() - public timezoneUuid: string; - + public firstName: string; + @ApiProperty({ + description: 'lastName', + required: true, + }) + @IsString() + @IsNotEmpty() + public lastName: string; constructor(dto: Partial) { Object.assign(this, dto); } diff --git a/src/users/services/user.service.ts b/src/users/services/user.service.ts index 56a5179..11262bb 100644 --- a/src/users/services/user.service.ts +++ b/src/users/services/user.service.ts @@ -1,4 +1,5 @@ import { + UpdateNameDto, UpdateProfilePictureDataDto, UpdateRegionDataDto, UpdateTimezoneDataDto, @@ -167,6 +168,45 @@ export class UserService { throw new BadRequestException('Timezone update failed'); } + return { + uuid: updatedUser.uuid, + firstName: updatedUser.firstName, + lastName: updatedUser.lastName, + profilePicture: updatedUser.profilePicture, + region: updatedUser.region, + timeZoneUuid: updatedUser.timeZone, + }; + } catch (err) { + throw new HttpException( + err.message || 'User not found', + HttpStatus.NOT_FOUND, + ); + } + } + async updateNameByUserUuid(userUuid: string, updateNameDto: UpdateNameDto) { + try { + const user = await this.getUserDetailsByUserUuid(userUuid); + if (!user) { + throw new HttpException('User not found', HttpStatus.NOT_FOUND); + } + + if (!updateNameDto.firstName || !updateNameDto.lastName) { + throw new BadRequestException('First Name and Last Name is required'); + } + + await this.userRepository.update( + { uuid: userUuid }, + { + firstName: updateNameDto.firstName, + lastName: updateNameDto.lastName, + }, + ); + + const updatedUser = await this.getUserDetailsByUserUuid(userUuid); + if (!updatedUser.firstName || !updatedUser.lastName) { + throw new BadRequestException('First Name and Last Name update failed'); + } + return { uuid: updatedUser.uuid, firstName: updatedUser.firstName,