mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 08:34:55 +00:00
feat: soft delete junior
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { IsNull, Not } from 'typeorm';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { Roles } from '~/auth/enums';
|
||||
import { CardService } from '~/card/services';
|
||||
@ -183,6 +184,31 @@ export class JuniorService {
|
||||
return this.cardService.transferToChild(juniorId, body.amount);
|
||||
}
|
||||
|
||||
async deleteJunior(juniorId: string, guardianId: string) {
|
||||
const doesBelong = await this.doesJuniorBelongToGuardian(guardianId, juniorId);
|
||||
|
||||
if (!doesBelong) {
|
||||
this.logger.error(`Junior ${juniorId} does not belong to guardian ${guardianId}`);
|
||||
throw new BadRequestException('JUNIOR.NOT_BELONG_TO_GUARDIAN');
|
||||
}
|
||||
|
||||
const hasPassword = await this.userService.findUser({ id: juniorId, password: Not(IsNull()) });
|
||||
|
||||
if (hasPassword) {
|
||||
this.logger.error(`Cannot delete junior ${juniorId} with registered user`);
|
||||
throw new BadRequestException('JUNIOR.CANNOT_DELETE_REGISTERED_USER');
|
||||
}
|
||||
|
||||
const { affected } = await this.juniorRepository.softDelete(juniorId);
|
||||
|
||||
if (affected === 0) {
|
||||
this.logger.error(`Junior ${juniorId} not found`);
|
||||
throw new BadRequestException('JUNIOR.NOT_FOUND');
|
||||
}
|
||||
|
||||
this.logger.log(`Junior ${juniorId} deleted successfully`);
|
||||
}
|
||||
|
||||
private async prepareJuniorImages(juniors: Junior[]) {
|
||||
this.logger.log(`Preparing junior images`);
|
||||
await Promise.all(
|
||||
|
||||
Reference in New Issue
Block a user