mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
feat: add loggers to all services
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { Roles } from '~/auth/enums';
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
@ -12,6 +12,7 @@ import { JuniorTokenService } from './junior-token.service';
|
||||
|
||||
@Injectable()
|
||||
export class JuniorService {
|
||||
private readonly logger = new Logger(JuniorService.name);
|
||||
constructor(
|
||||
private readonly juniorRepository: JuniorRepository,
|
||||
private readonly juniorTokenService: JuniorTokenService,
|
||||
@ -21,9 +22,11 @@ export class JuniorService {
|
||||
|
||||
@Transactional()
|
||||
async createJuniors(body: CreateJuniorRequestDto, guardianId: string) {
|
||||
this.logger.log(`Creating junior for guardian ${guardianId}`);
|
||||
const existingUser = await this.userService.findUser([{ email: body.email }, { phoneNumber: body.phoneNumber }]);
|
||||
|
||||
if (existingUser) {
|
||||
this.logger.error(`User with email ${body.email} or phone number ${body.phoneNumber} already exists`);
|
||||
throw new BadRequestException('USER.ALREADY_EXISTS');
|
||||
}
|
||||
|
||||
@ -51,43 +54,56 @@ export class JuniorService {
|
||||
user,
|
||||
);
|
||||
|
||||
this.logger.log(`Junior ${user.id} created successfully`);
|
||||
|
||||
return this.juniorTokenService.generateToken(user.id);
|
||||
}
|
||||
|
||||
async findJuniorById(juniorId: string, withGuardianRelation = false, guardianId?: string) {
|
||||
this.logger.log(`Finding junior ${juniorId}`);
|
||||
const junior = await this.juniorRepository.findJuniorById(juniorId, withGuardianRelation, guardianId);
|
||||
|
||||
if (!junior) {
|
||||
this.logger.error(`Junior ${juniorId} not found`);
|
||||
throw new BadRequestException('JUNIOR.NOT_FOUND');
|
||||
}
|
||||
|
||||
this.logger.log(`Junior ${juniorId} found successfully`);
|
||||
return junior;
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
async setTheme(body: SetThemeRequestDto, juniorId: string) {
|
||||
this.logger.log(`Setting theme for junior ${juniorId}`);
|
||||
const junior = await this.findJuniorById(juniorId);
|
||||
if (junior.theme) {
|
||||
this.logger.log(`Removing existing theme for junior ${juniorId}`);
|
||||
await this.juniorRepository.removeTheme(junior.theme);
|
||||
}
|
||||
|
||||
await this.juniorRepository.setTheme(body, junior);
|
||||
this.logger.log(`Theme set for junior ${juniorId}`);
|
||||
return this.juniorRepository.findThemeForJunior(juniorId);
|
||||
}
|
||||
|
||||
findJuniorsByGuardianId(guardianId: string, pageOptions: PageOptionsRequestDto) {
|
||||
this.logger.log(`Finding juniors for guardian ${guardianId}`);
|
||||
return this.juniorRepository.findJuniorsByGuardianId(guardianId, pageOptions);
|
||||
}
|
||||
|
||||
async validateToken(token: string) {
|
||||
this.logger.log(`Validating token ${token}`);
|
||||
const juniorId = await this.juniorTokenService.validateToken(token);
|
||||
|
||||
return this.findJuniorById(juniorId, true);
|
||||
}
|
||||
|
||||
generateToken(juniorId: string) {
|
||||
this.logger.log(`Generating token for junior ${juniorId}`);
|
||||
return this.juniorTokenService.generateToken(juniorId);
|
||||
}
|
||||
|
||||
async doesJuniorBelongToGuardian(guardianId: string, juniorId: string) {
|
||||
this.logger.log(`Checking if junior ${juniorId} belongs to guardian ${guardianId}`);
|
||||
const junior = await this.findJuniorById(juniorId, false, guardianId);
|
||||
|
||||
return !!junior;
|
||||
|
||||
Reference in New Issue
Block a user