feat: finalize update junior

This commit is contained in:
Abdalhameed Ahmad
2025-09-07 18:13:18 +03:00
parent edddc2f457
commit 44b5937f7a
8 changed files with 45 additions and 29 deletions

View File

@ -1,3 +1,4 @@
export * from './response.factory.util';
export * from './i18n-context-wrapper.util';
export * from './class-validator-formatter.util'; export * from './class-validator-formatter.util';
export * from './i18n-context-wrapper.util';
export * from './patch.util';
export * from './response.factory.util';

View File

@ -0,0 +1,3 @@
export const setIf = <T, K extends keyof T>(obj: T, key: K, val: T[K] | undefined) => {
if (typeof val !== 'undefined') obj[key] = val as T[K];
};

View File

@ -15,7 +15,7 @@ import { CountryIso } from '~/common/enums';
import { Guardian } from '~/guardian/entities/guradian.entity'; import { Guardian } from '~/guardian/entities/guradian.entity';
import { Junior } from '~/junior/entities'; import { Junior } from '~/junior/entities';
import { User } from '~/user/entities'; import { User } from '~/user/entities';
import { CustomerStatus, KycStatus } from '../enums'; import { CustomerStatus, Gender, KycStatus } from '../enums';
@Entity('customers') @Entity('customers')
export class Customer extends BaseEntity { export class Customer extends BaseEntity {
@ -62,7 +62,7 @@ export class Customer extends BaseEntity {
isPep!: boolean; isPep!: boolean;
@Column('varchar', { length: 255, nullable: true, name: 'gender' }) @Column('varchar', { length: 255, nullable: true, name: 'gender' })
gender!: string; gender!: Gender;
@Column('boolean', { default: false, name: 'is_junior' }) @Column('boolean', { default: false, name: 'is_junior' })
isJunior!: boolean; isJunior!: boolean;

View File

@ -67,7 +67,8 @@
"NOT_FOUND": "لم يتم العثور على الطفل.", "NOT_FOUND": "لم يتم العثور على الطفل.",
"CIVIL_ID_REQUIRED": "مطلوب بطاقة الهوية المدنية.", "CIVIL_ID_REQUIRED": "مطلوب بطاقة الهوية المدنية.",
"CIVIL_ID_NOT_CREATED_BY_GUARDIAN": "تم تحميل بطاقة الهوية المدنية من قبل شخص آخر غير ولي الأمر.", "CIVIL_ID_NOT_CREATED_BY_GUARDIAN": "تم تحميل بطاقة الهوية المدنية من قبل شخص آخر غير ولي الأمر.",
"CIVIL_ID_ALREADY_EXISTS": "بطاقة الهوية المدنية مستخدمة بالفعل من قبل طفل آخر." "CIVIL_ID_ALREADY_EXISTS": "بطاقة الهوية المدنية مستخدمة بالفعل من قبل طفل آخر.",
"CANNOT_UPDATE_REGISTERED_USER": "الطفل قد سجل بالفعل. لا يُسمح بتحديث البيانات."
}, },
"MONEY_REQUEST": { "MONEY_REQUEST": {

View File

@ -66,7 +66,8 @@
"NOT_FOUND": "The junior was not found.", "NOT_FOUND": "The junior was not found.",
"CIVIL_ID_REQUIRED": "Civil ID is required.", "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_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": { "MONEY_REQUEST": {

View File

@ -1,6 +1,5 @@
import { OmitType, PartialType } from '@nestjs/mapped-types'; import { OmitType, PartialType } from '@nestjs/swagger';
import { CreateJuniorRequestDto } from './create-junior.request.dto'; import { CreateJuniorRequestDto } from './create-junior.request.dto';
export class UpdateJuniorRequestDto extends PartialType( const omitted = OmitType(CreateJuniorRequestDto, ['cardColor', 'cardPin']);
OmitType(CreateJuniorRequestDto, ['cardColor', 'cardPin'] as const), export class UpdateJuniorRequestDto extends PartialType(omitted) {}
) {}

View File

@ -1,7 +1,8 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { Gender } from '~/customer/enums';
import { DocumentMetaResponseDto } from '~/document/dtos/response'; import { DocumentMetaResponseDto } from '~/document/dtos/response';
import { Junior } from '~/junior/entities'; import { Junior } from '~/junior/entities';
import { Relationship } from '~/junior/enums'; import { GuardianRelationship, Relationship } from '~/junior/enums';
export class JuniorResponseDto { export class JuniorResponseDto {
@ApiProperty({ example: 'id' }) @ApiProperty({ example: 'id' })
@ -16,9 +17,18 @@ export class JuniorResponseDto {
@ApiProperty({ example: 'test@junior.com' }) @ApiProperty({ example: 'test@junior.com' })
email!: string; email!: string;
@ApiProperty({ example: Gender.MALE })
gender!: Gender;
@ApiProperty({ example: '2000-01-01' })
dateOfBirth!: Date;
@ApiProperty({ enum: Relationship }) @ApiProperty({ enum: Relationship })
relationship!: Relationship; relationship!: Relationship;
@ApiProperty({ enum: GuardianRelationship })
guardianRelationship!: GuardianRelationship;
@ApiProperty({ type: DocumentMetaResponseDto }) @ApiProperty({ type: DocumentMetaResponseDto })
profilePicture!: DocumentMetaResponseDto | null; profilePicture!: DocumentMetaResponseDto | null;
@ -27,7 +37,10 @@ export class JuniorResponseDto {
this.firstName = junior.customer.firstName; this.firstName = junior.customer.firstName;
this.lastName = junior.customer.lastName; this.lastName = junior.customer.lastName;
this.email = junior.customer.user.email; this.email = junior.customer.user.email;
this.gender = junior.customer.gender;
this.dateOfBirth = junior.customer.dateOfBirth;
this.relationship = junior.relationship; this.relationship = junior.relationship;
this.guardianRelationship = GuardianRelationship[junior.relationship];
this.profilePicture = junior.customer.user.profilePicture this.profilePicture = junior.customer.user.profilePicture
? new DocumentMetaResponseDto(junior.customer.user.profilePicture) ? new DocumentMetaResponseDto(junior.customer.user.profilePicture)
: null; : null;

View File

@ -4,6 +4,7 @@ import { Roles } from '~/auth/enums';
import { CardService } from '~/card/services'; import { CardService } from '~/card/services';
import { NeoLeapService } from '~/common/modules/neoleap/services'; import { NeoLeapService } from '~/common/modules/neoleap/services';
import { PageOptionsRequestDto } from '~/core/dtos'; import { PageOptionsRequestDto } from '~/core/dtos';
import { setIf } from '~/core/utils';
import { CustomerService } from '~/customer/services'; import { CustomerService } from '~/customer/services';
import { DocumentService, OciService } from '~/document/services'; import { DocumentService, OciService } from '~/document/services';
import { UserType } from '~/user/enums'; import { UserType } from '~/user/enums';
@ -88,17 +89,14 @@ export class JuniorService {
async updateJunior(juniorId: string, body: UpdateJuniorRequestDto, guardianId: string) { async updateJunior(juniorId: string, body: UpdateJuniorRequestDto, guardianId: string) {
this.logger.log(`Updating junior ${juniorId}`); this.logger.log(`Updating junior ${juniorId}`);
const junior = await this.findJuniorById(juniorId, false, guardianId); const junior = await this.findJuniorById(juniorId, false, guardianId);
if (body.profilePictureId) { const customer = junior.customer;
junior.customer.user.profilePictureId = body.profilePictureId; const user = customer.user;
}
if (body.firstName) { if (user.password) {
junior.customer.user.firstName = body.firstName; this.logger.error(`Cannot update junior ${juniorId} with registered user`);
junior.customer.firstName = body.firstName; throw new BadRequestException('JUNIOR.CANNOT_UPDATE_REGISTERED_USER');
}
if (body.lastName) {
junior.customer.user.lastName = body.lastName;
junior.customer.lastName = body.lastName;
} }
if (body.email) { if (body.email) {
const existingUser = await this.userService.findUser({ email: body.email }); const existingUser = await this.userService.findUser({ email: body.email });
if (existingUser && existingUser.id !== junior.customer.user.id) { if (existingUser && existingUser.id !== junior.customer.user.id) {
@ -107,16 +105,16 @@ export class JuniorService {
} }
junior.customer.user.email = body.email; junior.customer.user.email = body.email;
} }
setIf(user, 'profilePictureId', body.profilePictureId);
setIf(user, 'firstName', body.firstName);
setIf(user, 'lastName', body.lastName);
if (body.dateOfBirth) { setIf(customer, 'firstName', body.firstName);
junior.customer.dateOfBirth = body.dateOfBirth; setIf(customer, 'lastName', body.lastName);
} setIf(customer, 'dateOfBirth', body.dateOfBirth as unknown as Date);
if (body.relationship) {
junior.relationship = body.relationship;
}
console.log('++++++');
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`); this.logger.log(`Junior ${juniorId} updated successfully`);
return junior; return junior;
} }