mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
feat: create junior
This commit is contained in:
65
src/junior/services/junior.service.ts
Normal file
65
src/junior/services/junior.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user