File size: 1,851 Bytes
97dab2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, InsertResult } from 'typeorm';
// import { Customer } from '../../models/customer.entity';
import { Customer } from '@bpm/data/models/customer.entity';
import { CustomerDto } from '@bpm/data/dto/customer/customer.dto';
import { UpdateCustomerDto } from '@bpm/data/dto/customer/update-customer.dto';
import { ApploggerService } from '@bpm/common';

@Injectable()
export class CustomerService {
  constructor(
    @InjectRepository(Customer)
    private readonly CustomerRepo: Repository<Customer>,
    private readonly logger: ApploggerService,
  ) {}

  async findAll(): Promise<Customer[]> {
    return this.CustomerRepo.find();
  }

  async findOne(id: string): Promise<Customer> {
    const Customer = await this.CustomerRepo.findOne({
      where: { CustomerID: id.toUpperCase() },
    });
    if (!Customer) {
      this.logger.error(`Customer with ID ${id} not found`);
      throw new NotFoundException(`Customer with ID ${id} not found`);
    }
    return Customer;
  }

  async create(dto: CustomerDto): Promise<Customer> {
    const exists = await this.CustomerRepo.findOne({ where: { CustomerID: dto.CustomerID } });
    if (exists) {
      throw new ConflictException(`Customer with ID ${dto.CustomerID} already exists.`);
    }
    const customer = this.CustomerRepo.create(dto);
    return this.CustomerRepo.save(customer);
  }

  async update(id: string, dto: UpdateCustomerDto): Promise<Customer> {
    const Customer = await this.findOne(id);
    const updated = { ...Customer, ...dto };
    return this.CustomerRepo.save(updated);
  }

  async remove(id: string): Promise<void> {
    const Customer = await this.findOne(id);
    await this.CustomerRepo.remove(Customer);
  }
}