feat: create junior

This commit is contained in:
Abdalhamid Alhamad
2024-12-09 13:11:18 +03:00
parent 3fd29b3905
commit 970a41c895
35 changed files with 625 additions and 26 deletions

View File

@ -0,0 +1,65 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { Transactional } from 'typeorm-transactional';
import { Roles } from '~/auth/enums';
import { UserService } from '~/auth/services';
import { PageOptionsRequestDto } from '~/core/dtos';
import { CustomerService } from '~/customer/services';
import { CreateJuniorRequestDto } from '../dtos/request';
import { Junior } from '../entities';
import { JuniorRepository } from '../repositories';
@Injectable()
export class JuniorService {
constructor(
private readonly juniorRepository: JuniorRepository,
private readonly userService: UserService,
private readonly customerService: CustomerService,
) {}
@Transactional()
async createJuniors(body: CreateJuniorRequestDto, guardianId: string) {
const existingUser = await this.userService.findUser([{ email: body.email }, { phoneNumber: body.phoneNumber }]);
if (existingUser) {
throw new BadRequestException('USER.ALREADY_EXISTS');
}
const user = await this.userService.createUser({
email: body.email,
countryCode: body.countryCode,
phoneNumber: body.phoneNumber,
roles: [Roles.JUNIOR],
});
await this.customerService.createCustomer(
{
firstName: body.firstName,
lastName: body.lastName,
dateOfBirth: body.dateOfBirth,
junior: Junior.create({
id: user.id,
guardianId,
relationship: body.relationship,
civilIdFrontId: body.civilIdFrontId,
civilIdBackId: body.civilIdBackId,
}),
},
user,
);
return this.findJuniorById(user.id, guardianId);
}
async findJuniorById(juniorId: string, guardianId: string) {
const junior = await this.juniorRepository.findJuniorById(juniorId, guardianId);
if (!junior) {
throw new BadRequestException('JUNIOR.NOT_FOUND');
}
return junior;
}
findJuniorsByGuardianId(guardianId: string, pageOptions: PageOptionsRequestDto) {
return this.juniorRepository.findJuniorsByGuardianId(guardianId, pageOptions);
}
}