Files
edr-platform/apps/edr-freight-api/src/modules/bookings/bookings.service.ts
Michael Abebe 1c5ee19388 chore: fmt
2026-05-12 16:50:18 +03:00

53 lines
1.7 KiB
TypeScript

import { Injectable, NotFoundException } from "@nestjs/common";
import { BookingsRepository } from "./bookings.repository";
import { CreateBookingDto } from "./dto/create-booking.dto";
import { FilterBookingDto } from "./dto/filter-booking.dto";
import { Booking } from "./entities/booking.entity";
@Injectable()
export class BookingsService {
constructor(private readonly bookingsRepository: BookingsRepository) {}
/** Create a new freight booking. */
async create(dto: CreateBookingDto): Promise<Booking> {
return this.bookingsRepository.create({
...dto,
scheduledDate: new Date(dto.scheduledDate),
});
}
/** Return a paginated list of bookings matching the filter. */
async findAll(
filter: FilterBookingDto,
): Promise<{ items: Booking[]; total: number }> {
const page = filter.page ?? 1;
const pageSize = filter.pageSize ?? 20;
const [items, total] = await this.bookingsRepository.findAndCount({
where: {
...(filter.status ? { status: filter.status } : {}),
...(filter.customerId ? { customerId: filter.customerId } : {}),
},
skip: (page - 1) * pageSize,
take: pageSize,
order: { createdAt: "DESC" },
});
return { items, total };
}
/** Get a single booking by ID, throwing if not found. */
async findById(id: string): Promise<Booking> {
const booking = await this.bookingsRepository.findById(id);
if (!booking) {
throw new NotFoundException(`Booking ${id} not found`);
}
return booking;
}
/** Soft-delete a booking. */
async remove(id: string): Promise<void> {
await this.findById(id);
await this.bookingsRepository.softDelete(id);
}
}