"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.getInspections = getInspections; exports.upsertInspection = upsertInspection; const prisma_1 = require("../../lib/prisma"); const errors_1 = require("../../http/errors"); const reservation_presenter_1 = require("./reservation.presenter"); const repo = __importStar(require("./reservation.repo")); async function getInspections(reservationId, companyId) { return repo.findInspections(reservationId, companyId); } async function upsertInspection(reservationId, companyId, type, body, inspectedBy) { const reservation = await repo.findForInspection(reservationId, companyId); const workflow = (0, reservation_presenter_1.buildReservationWorkflow)(reservation); if (workflow.closed) throw new errors_1.AppError('This reservation has already been closed', 400, 'reservation_closed'); if (type === 'CHECKIN' && !workflow.checkInInspectionEditable) { throw new errors_1.AppError('Check-in inspection is not editable at this stage', 400, 'invalid_status'); } if (type === 'CHECKOUT' && !workflow.checkOutInspectionEditable) { throw new errors_1.AppError('Check-out inspection is only editable after the vehicle is returned', 400, 'invalid_status'); } const inspection = await prisma_1.prisma.damageInspection.upsert({ where: { reservationId_type: { reservationId, type } }, update: { mileage: body.mileage ?? null, fuelLevel: body.fuelLevel, fuelCharge: body.fuelCharge ?? null, generalCondition: body.generalCondition ?? null, employeeNotes: body.employeeNotes ?? null, customerAgreed: body.customerAgreed ?? false, inspectedBy, inspectedAt: new Date(), damagePoints: { deleteMany: {}, create: body.damagePoints }, }, create: { reservationId, companyId, type, mileage: body.mileage ?? null, fuelLevel: body.fuelLevel, fuelCharge: body.fuelCharge ?? null, generalCondition: body.generalCondition ?? null, employeeNotes: body.employeeNotes ?? null, customerAgreed: body.customerAgreed ?? false, inspectedBy, damagePoints: { create: body.damagePoints }, }, include: { damagePoints: true }, }); const damageData = (body.damagePoints ?? []).map((p) => ({ viewType: p.viewType, x: p.x, y: p.y, damageType: p.damageType, severity: p.severity ?? 'MINOR', note: p.description ?? '', isPreExisting: p.isPreExisting ?? false, })); await prisma_1.prisma.damageReport.upsert({ where: { reservationId_type: { reservationId, type } }, update: { damages: damageData, fuelLevel: body.fuelLevel, mileage: body.mileage ?? null, inspectedAt: new Date(), inspectedBy, customerSignedAt: (body.customerAgreed ?? false) ? new Date() : null, customerName: `${reservation.customer.firstName} ${reservation.customer.lastName}`, }, create: { reservationId, companyId, type, damages: damageData, photos: [], fuelLevel: body.fuelLevel, mileage: body.mileage ?? null, inspectedAt: new Date(), inspectedBy, customerSignedAt: (body.customerAgreed ?? false) ? new Date() : null, customerName: `${reservation.customer.firstName} ${reservation.customer.lastName}`, }, }); await prisma_1.prisma.reservation.update({ where: { id: reservationId }, data: type === 'CHECKIN' ? { checkInMileage: body.mileage ?? null, checkInFuelLevel: body.fuelLevel } : { checkOutMileage: body.mileage ?? null, checkOutFuelLevel: body.fuelLevel }, }); return inspection; } //# sourceMappingURL=reservation.inspection.service.js.map