update first and last name endpoint

This commit is contained in:
faris Aljohari
2024-07-15 15:57:22 +03:00
parent f8cd47e940
commit 5e25eeb883
3 changed files with 77 additions and 5 deletions

View File

@ -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,
);
}
}
}

View File

@ -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<UpdateProfilePictureDataDto>) {
Object.assign(this, dto);
}

View File

@ -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,