import { Injectable, NotFoundException } from "@nestjs/common"; import { ConsignmentsRepository } from "./consignments.repository"; import { CreateConsignmentDto } from "./dto/create-consignment.dto"; import { FilterConsignmentDto } from "./dto/filter-consignment.dto"; import { Consignment } from "./entities/consignment.entity"; @Injectable() export class ConsignmentsService { constructor( private readonly consignmentsRepository: ConsignmentsRepository, ) {} /** Create a new consignment for a freight booking. */ create(dto: CreateConsignmentDto): Promise { return this.consignmentsRepository.create(dto); } /** Return a paginated list of consignments. */ async findAll( filter: FilterConsignmentDto, ): Promise<{ items: Consignment[]; total: number }> { const page = filter.page ?? 1; const pageSize = filter.pageSize ?? 20; const [items, total] = await this.consignmentsRepository.findAndCount({ where: { ...(filter.status ? { status: filter.status } : {}), ...(filter.bookingId ? { bookingId: filter.bookingId } : {}), }, skip: (page - 1) * pageSize, take: pageSize, order: { createdAt: "DESC" }, }); return { items, total }; } /** Get a single consignment by ID. */ async findById(id: string): Promise { const consignment = await this.consignmentsRepository.findById(id); if (!consignment) { throw new NotFoundException(`Consignment ${id} not found`); } return consignment; } }