mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 08:34:55 +00:00
feat: finalize update junior
This commit is contained in:
@ -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';
|
||||
|
||||
3
src/core/utils/patch.util.ts
Normal file
3
src/core/utils/patch.util.ts
Normal 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];
|
||||
};
|
||||
@ -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;
|
||||
|
||||
@ -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": {
|
||||
|
||||
@ -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": {
|
||||
|
||||
@ -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) {}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user