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 { constructor( @InjectRepository(TrainScheduleBooking) repository: Repository, ) { super(repository); } private repo(manager?: EntityManager) { return manager ? manager.getRepository(TrainScheduleBooking) : this.repository; } async createMany( records: DeepPartial[], manager?: EntityManager, ): Promise { if (!records.length) return []; const repo = this.repo(manager); return repo.save(repo.create(records)); } async deleteByScheduleAndBooking( trainScheduleId: string, bookingId: string, manager?: EntityManager, ): Promise { await this.repo(manager).delete({ trainScheduleId, bookingId }); } async existsForBooking(bookingId: string, manager?: EntityManager): Promise { const count = await this.repo(manager).count({ where: { bookingId } }); return count > 0; } findByBookingIds(bookingIds: string[], manager?: EntityManager): Promise { 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 { 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 { if (!bookingIds.length) return; await this.repo(manager).update( { trainScheduleId, bookingId: In(bookingIds) }, { loadingStatus }, ); } }