diff --git a/src/junior/services/junior.service.ts b/src/junior/services/junior.service.ts index 203ce1f..c783e2a 100644 --- a/src/junior/services/junior.service.ts +++ b/src/junior/services/junior.service.ts @@ -120,6 +120,7 @@ export class JuniorService { setIf(customer, 'firstName', body.firstName); setIf(customer, 'lastName', body.lastName); setIf(customer, 'dateOfBirth', body.dateOfBirth as unknown as Date); + setIf(customer, 'gender', body.gender); setIf(junior, 'relationship', body.relationship); await Promise.all([junior.save(), customer.save(), user.save()]); diff --git a/src/user/dtos/request/update-user.request.dto.ts b/src/user/dtos/request/update-user.request.dto.ts index 5024952..a52800a 100644 --- a/src/user/dtos/request/update-user.request.dto.ts +++ b/src/user/dtos/request/update-user.request.dto.ts @@ -1,6 +1,7 @@ -import { ApiProperty } from '@nestjs/swagger'; -import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator'; +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; +import { IsDateString, IsEnum, IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator'; import { i18nValidationMessage as i18n } from 'nestjs-i18n'; +import { Gender } from '~/customer/enums'; export class UpdateUserRequestDto { @ApiProperty({ example: 'John' }) @IsString({ message: i18n('validation.IsString', { path: 'general', property: 'user.firstName' }) }) @@ -18,4 +19,14 @@ export class UpdateUserRequestDto { @IsUUID('4', { message: i18n('validation.IsUUID', { path: 'general', property: 'user.profilePictureId' }) }) @IsOptional() profilePictureId!: string; + + @ApiPropertyOptional({ enum: Gender }) + @IsEnum(Gender, { message: i18n('validation.IsEnum', { path: 'general', property: 'customer.gender' }) }) + @IsOptional() + gender!: Gender; + + @ApiPropertyOptional({ example: '2020-01-01' }) + @IsDateString({}, { message: i18n('validation.IsDateString', { path: 'general', property: 'customer.dateOfBirth' }) }) + @IsOptional() + dateOfBirth!: Date; } diff --git a/src/user/services/user.service.ts b/src/user/services/user.service.ts index 46b4834..4d1bbc4 100644 --- a/src/user/services/user.service.ts +++ b/src/user/services/user.service.ts @@ -192,11 +192,25 @@ export class UserService { await this.validateProfilePictureId(data.profilePictureId, userId); this.logger.log(`Updating user ${userId} with data ${JSON.stringify(data)}`); - const { affected } = await this.userRepository.update(userId, data); + + const { gender, dateOfBirth, ...userData } = data; + + const { affected } = await this.userRepository.update(userId, userData); if (affected === 0) { this.logger.error(`User with id ${userId} not found`); throw new BadRequestException('USER.NOT_FOUND'); } + + if (gender !== undefined || dateOfBirth !== undefined) { + const customerData: Partial<{ gender: typeof gender; dateOfBirth: Date }> = {}; + if (gender !== undefined) { + customerData.gender = gender; + } + if (dateOfBirth !== undefined) { + customerData.dateOfBirth = dateOfBirth; + } + await this.customerService.updateCustomer(userId, customerData); + } } async updateUserEmail(userId: string, email: string) {