import { Injectable, NotFoundException } from '@nestjs/common'; import { PrismaService } from '../../common/prisma.service'; import { CreateSegmentFareDto, UpdateSegmentFareDto } from './segment-fare.dto'; @Injectable() export class SegmentFareService { constructor(private readonly prisma: PrismaService) {} findAll(routeId?: string) { return this.prisma.segmentFareRule.findMany({ where: routeId ? { routeId } : undefined, include: { seatClass: true, route: { select: { id: true, code: true, name: true } } }, orderBy: [{ routeId: 'asc' }, { originStopSequence: 'asc' }, { destinationStopSequence: 'asc' }], }); } async findOne(id: string) { const rule = await this.prisma.segmentFareRule.findUnique({ where: { id }, include: { seatClass: true, route: { select: { id: true, code: true, name: true } } }, }); if (!rule) throw new NotFoundException(`SegmentFareRule ${id} not found`); return rule; } create(dto: CreateSegmentFareDto) { return this.prisma.segmentFareRule.create({ data: { routeId: dto.routeId, originStopSequence: dto.originStopSequence, destinationStopSequence: dto.destinationStopSequence, seatClassId: dto.seatClassId, baseFareMinor: dto.baseFareMinor, nationality: dto.nationality ?? null, currency: dto.currency ?? 'ETB', validFrom: new Date(dto.validFrom), validUntil: dto.validUntil ? new Date(dto.validUntil) : null, }, include: { seatClass: true }, }); } async update(id: string, dto: UpdateSegmentFareDto) { await this.findOne(id); return this.prisma.segmentFareRule.update({ where: { id }, data: { ...(dto.baseFareMinor !== undefined && { baseFareMinor: dto.baseFareMinor }), ...(dto.currency !== undefined && { currency: dto.currency }), ...(dto.validFrom !== undefined && { validFrom: new Date(dto.validFrom) }), ...(dto.validUntil !== undefined && { validUntil: new Date(dto.validUntil) }), }, include: { seatClass: true }, }); } async remove(id: string) { await this.findOne(id); await this.prisma.segmentFareRule.delete({ where: { id } }); return { deleted: true }; } }