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);