diff --git a/src/core/utils/index.ts b/src/core/utils/index.ts index 6d80059..67d3af4 100644 --- a/src/core/utils/index.ts +++ b/src/core/utils/index.ts @@ -1,3 +1,4 @@ -export * from './response.factory.util'; -export * from './i18n-context-wrapper.util'; export * from './class-validator-formatter.util'; +export * from './i18n-context-wrapper.util'; +export * from './patch.util'; +export * from './response.factory.util'; diff --git a/src/core/utils/patch.util.ts b/src/core/utils/patch.util.ts new file mode 100644 index 0000000..2ba313f --- /dev/null +++ b/src/core/utils/patch.util.ts @@ -0,0 +1,3 @@ +export const setIf = (obj: T, key: K, val: T[K] | undefined) => { + if (typeof val !== 'undefined') obj[key] = val as T[K]; +}; diff --git a/src/customer/entities/customer.entity.ts b/src/customer/entities/customer.entity.ts index c1b2875..e675a4a 100644 --- a/src/customer/entities/customer.entity.ts +++ b/src/customer/entities/customer.entity.ts @@ -15,7 +15,7 @@ import { CountryIso } from '~/common/enums'; import { Guardian } from '~/guardian/entities/guradian.entity'; import { Junior } from '~/junior/entities'; import { User } from '~/user/entities'; -import { CustomerStatus, KycStatus } from '../enums'; +import { CustomerStatus, Gender, KycStatus } from '../enums'; @Entity('customers') export class Customer extends BaseEntity { @@ -62,7 +62,7 @@ export class Customer extends BaseEntity { isPep!: boolean; @Column('varchar', { length: 255, nullable: true, name: 'gender' }) - gender!: string; + gender!: Gender; @Column('boolean', { default: false, name: 'is_junior' }) isJunior!: boolean; diff --git a/src/i18n/ar/app.json b/src/i18n/ar/app.json index 7c5bcb4..411141d 100644 --- a/src/i18n/ar/app.json +++ b/src/i18n/ar/app.json @@ -67,7 +67,8 @@ "NOT_FOUND": "لم يتم العثور على الطفل.", "CIVIL_ID_REQUIRED": "مطلوب بطاقة الهوية المدنية.", "CIVIL_ID_NOT_CREATED_BY_GUARDIAN": "تم تحميل بطاقة الهوية المدنية من قبل شخص آخر غير ولي الأمر.", - "CIVIL_ID_ALREADY_EXISTS": "بطاقة الهوية المدنية مستخدمة بالفعل من قبل طفل آخر." + "CIVIL_ID_ALREADY_EXISTS": "بطاقة الهوية المدنية مستخدمة بالفعل من قبل طفل آخر.", + "CANNOT_UPDATE_REGISTERED_USER": "الطفل قد سجل بالفعل. لا يُسمح بتحديث البيانات." }, "MONEY_REQUEST": { diff --git a/src/i18n/en/app.json b/src/i18n/en/app.json index f6e5278..b6c4ccb 100644 --- a/src/i18n/en/app.json +++ b/src/i18n/en/app.json @@ -66,7 +66,8 @@ "NOT_FOUND": "The junior was not found.", "CIVIL_ID_REQUIRED": "Civil ID is required.", "CIVIL_ID_NOT_CREATED_BY_GUARDIAN": "The civil ID document was not uploaded by the guardian.", - "CIVIL_ID_ALREADY_EXISTS": "The civil ID is already used by another junior." + "CIVIL_ID_ALREADY_EXISTS": "The civil ID is already used by another junior.", + "CANNOT_UPDATE_REGISTERED_USER": "The junior has already registered. Updating details is not allowed." }, "MONEY_REQUEST": { diff --git a/src/junior/dtos/request/update-junior.request.dto.ts b/src/junior/dtos/request/update-junior.request.dto.ts index c9054b3..be9457b 100644 --- a/src/junior/dtos/request/update-junior.request.dto.ts +++ b/src/junior/dtos/request/update-junior.request.dto.ts @@ -1,6 +1,5 @@ -import { OmitType, PartialType } from '@nestjs/mapped-types'; +import { OmitType, PartialType } from '@nestjs/swagger'; import { CreateJuniorRequestDto } from './create-junior.request.dto'; -export class UpdateJuniorRequestDto extends PartialType( - OmitType(CreateJuniorRequestDto, ['cardColor', 'cardPin'] as const), -) {} +const omitted = OmitType(CreateJuniorRequestDto, ['cardColor', 'cardPin']); +export class UpdateJuniorRequestDto extends PartialType(omitted) {} diff --git a/src/junior/dtos/response/junior.response.dto.ts b/src/junior/dtos/response/junior.response.dto.ts index 52ea9a1..5a0436c 100644 --- a/src/junior/dtos/response/junior.response.dto.ts +++ b/src/junior/dtos/response/junior.response.dto.ts @@ -1,7 +1,8 @@ import { ApiProperty } from '@nestjs/swagger'; +import { Gender } from '~/customer/enums'; import { DocumentMetaResponseDto } from '~/document/dtos/response'; import { Junior } from '~/junior/entities'; -import { Relationship } from '~/junior/enums'; +import { GuardianRelationship, Relationship } from '~/junior/enums'; export class JuniorResponseDto { @ApiProperty({ example: 'id' }) @@ -16,9 +17,18 @@ export class JuniorResponseDto { @ApiProperty({ example: 'test@junior.com' }) email!: string; + @ApiProperty({ example: Gender.MALE }) + gender!: Gender; + + @ApiProperty({ example: '2000-01-01' }) + dateOfBirth!: Date; + @ApiProperty({ enum: Relationship }) relationship!: Relationship; + @ApiProperty({ enum: GuardianRelationship }) + guardianRelationship!: GuardianRelationship; + @ApiProperty({ type: DocumentMetaResponseDto }) profilePicture!: DocumentMetaResponseDto | null; @@ -27,7 +37,10 @@ export class JuniorResponseDto { this.firstName = junior.customer.firstName; this.lastName = junior.customer.lastName; this.email = junior.customer.user.email; + this.gender = junior.customer.gender; + this.dateOfBirth = junior.customer.dateOfBirth; this.relationship = junior.relationship; + this.guardianRelationship = GuardianRelationship[junior.relationship]; this.profilePicture = junior.customer.user.profilePicture ? new DocumentMetaResponseDto(junior.customer.user.profilePicture) : null; diff --git a/src/junior/services/junior.service.ts b/src/junior/services/junior.service.ts index 24432fd..63f5593 100644 --- a/src/junior/services/junior.service.ts +++ b/src/junior/services/junior.service.ts @@ -4,6 +4,7 @@ import { Roles } from '~/auth/enums'; import { CardService } from '~/card/services'; import { NeoLeapService } from '~/common/modules/neoleap/services'; import { PageOptionsRequestDto } from '~/core/dtos'; +import { setIf } from '~/core/utils'; import { CustomerService } from '~/customer/services'; import { DocumentService, OciService } from '~/document/services'; import { UserType } from '~/user/enums'; @@ -88,17 +89,14 @@ export class JuniorService { async updateJunior(juniorId: string, body: UpdateJuniorRequestDto, guardianId: string) { this.logger.log(`Updating junior ${juniorId}`); const junior = await this.findJuniorById(juniorId, false, guardianId); - if (body.profilePictureId) { - junior.customer.user.profilePictureId = body.profilePictureId; - } - if (body.firstName) { - junior.customer.user.firstName = body.firstName; - junior.customer.firstName = body.firstName; - } - if (body.lastName) { - junior.customer.user.lastName = body.lastName; - junior.customer.lastName = body.lastName; + const customer = junior.customer; + const user = customer.user; + + if (user.password) { + this.logger.error(`Cannot update junior ${juniorId} with registered user`); + throw new BadRequestException('JUNIOR.CANNOT_UPDATE_REGISTERED_USER'); } + if (body.email) { const existingUser = await this.userService.findUser({ email: body.email }); if (existingUser && existingUser.id !== junior.customer.user.id) { @@ -107,16 +105,16 @@ export class JuniorService { } junior.customer.user.email = body.email; } + setIf(user, 'profilePictureId', body.profilePictureId); + setIf(user, 'firstName', body.firstName); + setIf(user, 'lastName', body.lastName); - if (body.dateOfBirth) { - junior.customer.dateOfBirth = body.dateOfBirth; - } - if (body.relationship) { - junior.relationship = body.relationship; - } - console.log('++++++'); + setIf(customer, 'firstName', body.firstName); + setIf(customer, 'lastName', body.lastName); + setIf(customer, 'dateOfBirth', body.dateOfBirth as unknown as Date); - await Promise.all([junior.save(), junior.customer.user.save(), junior.customer.save()]); + setIf(junior, 'relationship', body.relationship); + await Promise.all([junior.save(), customer.save(), user.save()]); this.logger.log(`Junior ${juniorId} updated successfully`); return junior; }