File size: 1,660 Bytes
f97bd0c
 
 
 
 
c3990d3
 
 
 
 
8b7e5fc
c3990d3
8b7e5fc
 
c3990d3
f97bd0c
 
 
 
c3990d3
8b7e5fc
 
c3990d3
 
8b7e5fc
 
c3990d3
 
8b7e5fc
 
c3990d3
f97bd0c
c3990d3
 
f97bd0c
c3990d3
 
8b7e5fc
 
c3990d3
 
 
 
 
 
 
 
 
9e8f9e3
 
 
 
 
 
 
 
 
8b7e5fc
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
  }
}