import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { BaseRepository } from '@edr/api-common'; import { DeepPartial, Repository } from 'typeorm'; import { Vendor } from './entities/vendor.entity'; import { AssetAcquisition } from './entities/asset-acquisition.entity'; import { AssetDisposal } from './entities/asset-disposal.entity'; @Injectable() export class ProcurementRepository extends BaseRepository { constructor( @InjectRepository(AssetAcquisition) private readonly acquisitionRepository: Repository, @InjectRepository(Vendor) private readonly vendorRepository: Repository, @InjectRepository(AssetDisposal) private readonly disposalRepository: Repository, ) { super(acquisitionRepository); } // ---- Vendors ---- async createVendor(data: DeepPartial): Promise { const vendor = this.vendorRepository.create(data); return this.vendorRepository.save(vendor); } async findVendors(): Promise { return this.vendorRepository.find({ order: { createdAt: 'DESC' } }); } async updateVendor(id: string, data: DeepPartial): Promise { await this.vendorRepository.update(id, data as never); return this.vendorRepository.findOneBy({ id }); } async softDeleteVendor(id: string): Promise { await this.vendorRepository.softDelete(id); } // ---- Acquisitions ---- async createAcquisition(data: DeepPartial): Promise { const acquisition = this.acquisitionRepository.create(data); return this.acquisitionRepository.save(acquisition); } async findAcquisitions(vehicleId?: string): Promise { return this.acquisitionRepository.find({ where: vehicleId ? { vehicleId } : {}, relations: ['vehicle', 'vendor'], order: { acquisitionDate: 'DESC' }, }); } async findAcquisitionById(id: string): Promise { return this.acquisitionRepository.findOne({ where: { id }, relations: ['vehicle', 'vendor'], }); } async updateAcquisition( id: string, data: DeepPartial, ): Promise { await this.acquisitionRepository.update(id, data as never); return this.findAcquisitionById(id); } async softDeleteAcquisition(id: string): Promise { await this.acquisitionRepository.softDelete(id); } async findLatestAcquisitionByVehicle(vehicleId: string): Promise { return this.acquisitionRepository.findOne({ where: { vehicleId }, relations: ['vehicle', 'vendor'], order: { acquisitionDate: 'DESC' }, }); } // ---- Disposals ---- async createDisposal(data: DeepPartial): Promise { const disposal = this.disposalRepository.create(data); return this.disposalRepository.save(disposal); } async findDisposals(): Promise { return this.disposalRepository.find({ order: { disposalDate: 'DESC' } }); } async softDeleteDisposal(id: string): Promise { await this.disposalRepository.softDelete(id); } async findLatestDisposalByVehicle(vehicleId: string): Promise { return this.disposalRepository.findOne({ where: { vehicleId }, order: { disposalDate: 'DESC' }, }); } }