import { BadRequestException, Injectable, NotFoundException, } from '@nestjs/common'; import { CreateBranchDto } from './dto/create-branch.dto.js'; import { BranchEntity } from '../../entities/branch.entity.js'; import { Public } from '../authentication/authentication.decorator.js'; import { UpdateBranchDto } from './dto/update-branch.dto.js'; import { plainToClass } from 'class-transformer'; @Public() @Injectable() export class BranchService { async create(createBranchDto: CreateBranchDto) { const branch = await BranchEntity.findOneBy({ id: createBranchDto.id }); if (branch) { throw new BadRequestException('Branch already exists'); } return await BranchEntity.create({ ...createBranchDto }).save(); } async findAll() { return await BranchEntity.find(); } async findOne(id: string) { return await BranchEntity.findOneBy({ id: id }); } async getBranchOrError(id: string) { console.log(id); const branch = await BranchEntity.findOneBy({ id }); if (!branch) { throw new NotFoundException('Branch not found'); } return branch; } async update(id: string, updateBranchDto: UpdateBranchDto) { let branch = await this.getBranchOrError(id); branch = plainToClass(BranchEntity, { ...branch, ...updateBranchDto, }); return await branch.save(); } async softRemove(id: string) { let branch = await this.getBranchOrError(id); return await branch.softRemove(); } async restore(id: string) { let result = await BranchEntity.getRepository().restore(id); let branch = await BranchEntity.findOneBy({ id }); return branch; } }