From 506974afc8483eb9f0d3660334dbdc0063a79941 Mon Sep 17 00:00:00 2001 From: Abdalhamid Alhamad Date: Sun, 9 Nov 2025 12:42:48 +0300 Subject: [PATCH] Enhance profile picture handling in JuniorService to ensure foreign key consistency and validate document ownership before assignment. --- src/junior/services/junior.service.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/junior/services/junior.service.ts b/src/junior/services/junior.service.ts index b85e716..fdc7e58 100644 --- a/src/junior/services/junior.service.ts +++ b/src/junior/services/junior.service.ts @@ -114,7 +114,28 @@ export class JuniorService { } junior.customer.user.email = body.email; } - setIf(user, 'profilePictureId', body.profilePictureId); + // Update profile picture: ensure FK and relation are consistent to avoid TypeORM overriding the FK + if (typeof body.profilePictureId !== 'undefined') { + if (body.profilePictureId) { + const document = await this.documentService.findDocumentById(body.profilePictureId); + if (!document) { + this.logger.error(`Document with id ${body.profilePictureId} not found`); + throw new BadRequestException('DOCUMENT.NOT_FOUND'); + } + if (document.createdById !== juniorId) { + this.logger.error( + `Document with id ${body.profilePictureId} does not belong to user ${juniorId}`, + ); + } + user.profilePictureId = body.profilePictureId; + // assign relation to keep it consistent with FK during save + user.profilePicture = document as any; + } else { + // if empty string provided (unlikely), clear relation and FK + user.profilePicture = null as any; + user.profilePictureId = null as any; + } + } setIf(user, 'firstName', body.firstName); setIf(user, 'lastName', body.lastName);