mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
feat: create card for children
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { Roles } from '~/auth/enums';
|
||||
import { CardService } from '~/card/services';
|
||||
import { NeoLeapService } from '~/common/modules/neoleap/services';
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
import { CustomerService } from '~/customer/services';
|
||||
import { DocumentService, OciService } from '~/document/services';
|
||||
@ -23,12 +25,21 @@ export class JuniorService {
|
||||
private readonly documentService: DocumentService,
|
||||
private readonly ociService: OciService,
|
||||
private readonly qrCodeService: QrcodeService,
|
||||
private readonly neoleapService: NeoLeapService,
|
||||
private readonly cardService: CardService,
|
||||
) {}
|
||||
|
||||
@Transactional()
|
||||
async createJuniors(body: CreateJuniorRequestDto, guardianId: string) {
|
||||
this.logger.log(`Creating junior for guardian ${guardianId}`);
|
||||
|
||||
const parentCustomer = await this.customerService.findCustomerById(guardianId);
|
||||
|
||||
if (!parentCustomer.cards || parentCustomer.cards.length === 0) {
|
||||
this.logger.error(`Guardian ${guardianId} does not have a card`);
|
||||
throw new BadRequestException('CUSTOMER.DOES_NOT_HAVE_CARD');
|
||||
}
|
||||
|
||||
const existingUser = await this.userService.findUser({ email: body.email });
|
||||
|
||||
if (existingUser) {
|
||||
@ -44,14 +55,17 @@ export class JuniorService {
|
||||
roles: [Roles.JUNIOR],
|
||||
});
|
||||
|
||||
const customer = await this.customerService.createJuniorCustomer(guardianId, user.id, body);
|
||||
const childCustomer = await this.customerService.createJuniorCustomer(guardianId, user.id, body);
|
||||
|
||||
await this.juniorRepository.createJunior(user.id, {
|
||||
guardianId,
|
||||
relationship: body.relationship,
|
||||
customerId: customer.id,
|
||||
customerId: childCustomer.id,
|
||||
});
|
||||
|
||||
this.logger.debug('Creating card For Child');
|
||||
|
||||
await this.cardService.createCardForChild(parentCustomer, childCustomer, body.cardColor, body.cardColor);
|
||||
this.logger.log(`Junior ${user.id} created successfully`);
|
||||
|
||||
return this.generateToken(user.id);
|
||||
|
||||
Reference in New Issue
Block a user