mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
feat: add validation for documents
This commit is contained in:
@ -4,6 +4,7 @@ import { Roles } from '~/auth/enums';
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
import { CustomerNotificationSettings } from '~/customer/entities/customer-notification-settings.entity';
|
||||
import { CustomerService } from '~/customer/services';
|
||||
import { DocumentService, OciService } from '~/document/services';
|
||||
import { UserService } from '~/user/services';
|
||||
import { CreateJuniorRequestDto, SetThemeRequestDto } from '../dtos/request';
|
||||
import { Junior } from '../entities';
|
||||
@ -18,6 +19,8 @@ export class JuniorService {
|
||||
private readonly juniorTokenService: JuniorTokenService,
|
||||
private readonly userService: UserService,
|
||||
private readonly customerService: CustomerService,
|
||||
private readonly documentService: DocumentService,
|
||||
private readonly ociService: OciService,
|
||||
) {}
|
||||
|
||||
@Transactional()
|
||||
@ -30,6 +33,8 @@ export class JuniorService {
|
||||
throw new BadRequestException('USER.ALREADY_EXISTS');
|
||||
}
|
||||
|
||||
await this.validateJuniorDocuments(guardianId, body.civilIdFrontId, body.civilIdBackId);
|
||||
|
||||
const user = await this.userService.createUser({
|
||||
email: body.email,
|
||||
countryCode: body.countryCode,
|
||||
@ -75,6 +80,11 @@ export class JuniorService {
|
||||
@Transactional()
|
||||
async setTheme(body: SetThemeRequestDto, juniorId: string) {
|
||||
this.logger.log(`Setting theme for junior ${juniorId}`);
|
||||
const document = await this.documentService.findDocumentById(body.avatarId);
|
||||
if (!document || document.createdById !== juniorId) {
|
||||
this.logger.error(`Document ${body.avatarId} not found or not created by junior ${juniorId}`);
|
||||
throw new BadRequestException('DOCUMENT.NOT_FOUND');
|
||||
}
|
||||
const junior = await this.findJuniorById(juniorId);
|
||||
if (junior.theme) {
|
||||
this.logger.log(`Removing existing theme for junior ${juniorId}`);
|
||||
@ -86,9 +96,12 @@ export class JuniorService {
|
||||
return this.juniorRepository.findThemeForJunior(juniorId);
|
||||
}
|
||||
|
||||
findJuniorsByGuardianId(guardianId: string, pageOptions: PageOptionsRequestDto) {
|
||||
async findJuniorsByGuardianId(guardianId: string, pageOptions: PageOptionsRequestDto): Promise<[Junior[], number]> {
|
||||
this.logger.log(`Finding juniors for guardian ${guardianId}`);
|
||||
return this.juniorRepository.findJuniorsByGuardianId(guardianId, pageOptions);
|
||||
const [juniors, itemCount] = await this.juniorRepository.findJuniorsByGuardianId(guardianId, pageOptions);
|
||||
this.logger.log(`Juniors found for guardian ${guardianId}`);
|
||||
await this.prepareJuniorImages(juniors);
|
||||
return [juniors, itemCount];
|
||||
}
|
||||
|
||||
async validateToken(token: string) {
|
||||
@ -108,4 +121,49 @@ export class JuniorService {
|
||||
|
||||
return !!junior;
|
||||
}
|
||||
|
||||
private async validateJuniorDocuments(userId: string, civilIdFrontId: string, civilIdBackId: string) {
|
||||
this.logger.log(`Validating junior documents`);
|
||||
if (!civilIdFrontId || !civilIdBackId) {
|
||||
this.logger.error('Civil id front and back are required');
|
||||
throw new BadRequestException('JUNIOR.CIVIL_ID_REQUIRED');
|
||||
}
|
||||
|
||||
const [civilIdFront, civilIdBack] = await Promise.all([
|
||||
this.documentService.findDocumentById(civilIdFrontId),
|
||||
this.documentService.findDocumentById(civilIdBackId),
|
||||
]);
|
||||
|
||||
if (!civilIdFront || !civilIdBack) {
|
||||
this.logger.error('Civil id front or back not found');
|
||||
throw new BadRequestException('JUNIOR.CIVIL_ID_REQUIRED');
|
||||
}
|
||||
|
||||
if (civilIdFront.createdById !== userId || civilIdBack.createdById !== userId) {
|
||||
this.logger.error(`Civil id front or back not created by user with id ${userId}`);
|
||||
throw new BadRequestException('JUNIOR.CIVIL_ID_NOT_CREATED_BY_GUARDIAN');
|
||||
}
|
||||
|
||||
const juniorWithSameCivilId = await this.juniorRepository.findJuniorByCivilId(civilIdFrontId, civilIdBackId);
|
||||
|
||||
if (juniorWithSameCivilId) {
|
||||
this.logger.error(
|
||||
`Junior with civil id front ${civilIdFrontId} and civil id back ${civilIdBackId} already exists`,
|
||||
);
|
||||
throw new BadRequestException('JUNIOR.CIVIL_ID_ALREADY_EXISTS');
|
||||
}
|
||||
}
|
||||
|
||||
private async prepareJuniorImages(juniors: Junior[]) {
|
||||
this.logger.log(`Preparing junior images`);
|
||||
await Promise.all(
|
||||
juniors.map(async (junior) => {
|
||||
const profilePicture = junior.customer.profilePicture;
|
||||
|
||||
if (profilePicture) {
|
||||
profilePicture.url = await this.ociService.generatePreSignedUrl(profilePicture);
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user