Files
edr-platform/apps/edr-freight-api/src/modules/train-schedules/train-schedule-bookings.repository.ts
2026-07-03 13:47:45 +00:00

74 lines
2.2 KiB
TypeScript

import { BaseRepository } from '@edr/api-common';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DeepPartial, EntityManager, In, Repository } from 'typeorm';
import { TrainScheduleBooking } from './entities/train-schedule-booking.entity';
@Injectable()
export class TrainScheduleBookingsRepository extends BaseRepository<TrainScheduleBooking> {
constructor(
@InjectRepository(TrainScheduleBooking)
repository: Repository<TrainScheduleBooking>,
) {
super(repository);
}
private repo(manager?: EntityManager) {
return manager ? manager.getRepository(TrainScheduleBooking) : this.repository;
}
async createMany(
records: DeepPartial<TrainScheduleBooking>[],
manager?: EntityManager,
): Promise<TrainScheduleBooking[]> {
if (!records.length) return [];
const repo = this.repo(manager);
return repo.save(repo.create(records));
}
async deleteByScheduleAndBooking(
trainScheduleId: string,
bookingId: string,
manager?: EntityManager,
): Promise<void> {
await this.repo(manager).delete({ trainScheduleId, bookingId });
}
async existsForBooking(bookingId: string, manager?: EntityManager): Promise<boolean> {
const count = await this.repo(manager).count({ where: { bookingId } });
return count > 0;
}
findByBookingIds(bookingIds: string[], manager?: EntityManager): Promise<TrainScheduleBooking[]> {
if (!bookingIds.length) return Promise.resolve([]);
return this.repo(manager).find({
where: { bookingId: In(bookingIds) },
select: { id: true, bookingId: true, trainScheduleId: true },
});
}
findByScheduleId(
trainScheduleId: string,
manager?: EntityManager,
): Promise<TrainScheduleBooking[]> {
return this.repo(manager).find({
where: { trainScheduleId },
select: { id: true, bookingId: true, trainScheduleId: true, loadingStatus: true },
});
}
async updateLoadingStatusMany(
trainScheduleId: string,
bookingIds: string[],
loadingStatus: string,
manager?: EntityManager,
): Promise<void> {
if (!bookingIds.length) return;
await this.repo(manager).update(
{ trainScheduleId, bookingId: In(bookingIds) },
{ loadingStatus },
);
}
}