import { BaseRepository } from '@edr/api-common'; import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { BookingRequest } from './entities/booking-request.entity'; @Injectable() export class BookingRequestRepository extends BaseRepository { constructor( @InjectRepository(BookingRequest) repository: Repository, ) { super(repository); } /** All requests on a contract, newest first. */ async findForContract(contractId: string): Promise { return this.repository.find({ where: { contractId }, order: { createdAt: 'DESC' }, }); } /** GL queue: pending requests across all contracts, oldest first. */ async findPending(): Promise { return this.repository.find({ where: { status: 'PENDING' }, order: { createdAt: 'ASC' }, relations: { contract: true }, }); } async findById(id: string): Promise { return this.repository.findOne({ where: { id }, // Load the contract with the bits the detail page surfaces: customer // (company), service type (mile/customs flags), routes (with yard labels) // and cargo scope. relations: { contract: { company: true, serviceType: true, routes: { originYard: true, destinationYard: true }, cargoScope: true, }, }, }); } /** Total rows — used to mint the next sequential reference. */ async count(): Promise { return this.repository.count(); } }