// import { BaseRepository } from "@edr/api-common"; // import { EntityRepository } from "typeorm"; // src/modules/customers/customers.repository.ts import { Injectable } from "@nestjs/common"; import { InjectRepository } from "@nestjs/typeorm"; import { Repository, FindManyOptions, FindOptionsWhere } from "typeorm"; import { Customer } from "./entities/customer.entity"; import { CreateCustomerDto } from "./dto/create-customer.dto"; // import { UpdateCustomerDto } from "./dto/update-customer.dto"; @Injectable() export class CustomersRepository { constructor( @InjectRepository(Customer) private readonly repository: Repository, ) { } async create(dto: CreateCustomerDto): Promise { const customer = this.repository.create(dto); return await this.repository.save(customer); } async findAll(options?: FindManyOptions): Promise { return await this.repository.find(options); } async findById(id: string): Promise { return await this.repository.findOne({ where: { id } as FindOptionsWhere }); } async findByUserId(userId: string): Promise { return await this.repository.findOne({ where: { userId } as FindOptionsWhere }); } async findByEmail(email: string): Promise { return await this.repository.findOne({ where: { email } as FindOptionsWhere }); } async findByVatNumber(vatNumber: string): Promise { return await this.repository.findOne({ where: { vatNumber } as FindOptionsWhere }); } async findByName(name: string): Promise { return await this.repository .createQueryBuilder("customer") .where("customer.companyName ILIKE :name", { name: `%${name}%` }) .getMany(); } async findOneByEmailOrVat(email?: string, vatNumber?: string): Promise { if (!email && !vatNumber) return null; const queryBuilder = this.repository.createQueryBuilder('customer'); if (email && vatNumber) { queryBuilder.where('customer.email = :email', { email }) .orWhere('customer.vatNumber = :vatNumber', { vatNumber }); } else if (email) { queryBuilder.where('customer.email = :email', { email }); } else if (vatNumber) { queryBuilder.where('customer.vatNumber = :vatNumber', { vatNumber }); } return await queryBuilder.getOne(); } async update(id: string, updates: Partial): Promise { await this.repository.update(id, updates); return this.findById(id); } async delete(id: string): Promise { const result = await this.repository.delete(id); return (result.affected ?? 0) > 0; } async count(where?: any): Promise { if (where?.createdAt) { const result = await this.repository .createQueryBuilder('customer') .where('customer.createdAt >= :date', { date: where.createdAt }) .getCount(); return result; } return await this.repository.count(); } async existsByUniqueFields(email: string, vatNumber?: string): Promise { const queryBuilder = this.repository.createQueryBuilder('customer') .where('customer.email = :email', { email }); if (vatNumber) { queryBuilder.orWhere('customer.vatNumber = :vatNumber', { vatNumber }); } const count = await queryBuilder.getCount(); return count > 0; } async countWithVatNumber(): Promise { const count = await this.repository .createQueryBuilder('customer') .where('customer.vatNumber IS NOT NULL') .andWhere("customer.vatNumber != ''") .getCount(); return count; } getRepository(): Repository { return this.repository; } softDelete(id: string): any { return id; } }