mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-27 19:00:55 +00:00
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
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<Consignment> {
|
|
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<Consignment> {
|
|
const consignment = await this.consignmentsRepository.findById(id);
|
|
if (!consignment) {
|
|
throw new NotFoundException(`Consignment ${id} not found`);
|
|
}
|
|
return consignment;
|
|
}
|
|
}
|