import { Body, Controller, Get, Param, ParseUUIDPipe, Post } from "@nestjs/common"; import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger"; import { BookingStaff } from "../../common/booking-guards"; import { FREIGHT_PERMS } from "../../seed/freight-permissions.registry"; import { ResolveEimsRegistrationDto } from "./dto/resolve-eims-registration.dto"; import { EimsInvoiceRegistrationService } from "./eims-invoice-registration.service"; /** * Staff-triggered EIMS actions on an existing invoice. Registration is manual and one invoice at a * time — nothing in invoice creation submits automatically. * * Filing gets its own permission (`invoices:eims_register`) rather than riding on an existing key: * registration is irreversible at MoR, so it must not follow from the right to download a PDF. * The key is seeded through FINANCE_PERMISSIONS, which reaches `iam.permissions` via * ADVANCED_BACKOFFICE_PERMISSIONS → BOOKING_RULE_ENGINE_PERMISSIONS → EDR_FREIGHT_PERMISSIONS. */ @ApiTags("eims") @ApiBearerAuth() @Controller("invoices") export class EimsInvoiceController { constructor(private readonly registration: EimsInvoiceRegistrationService) {} @Post(":id/eims/register") @BookingStaff(FREIGHT_PERMS.invoices.eimsRegister) @ApiOperation({ summary: "Register the invoice with MoR EIMS. Idempotent — an invoice that already has an IRN is returned unchanged.", }) register(@Param("id", ParseUUIDPipe) id: string) { return this.registration.registerInvoiceWithEims(id); } @Post(":id/eims/verify") @BookingStaff(FREIGHT_PERMS.invoices.eimsRegister) @ApiOperation({ summary: "Verify the invoice's stored IRN against EIMS" }) verify(@Param("id", ParseUUIDPipe) id: string) { return this.registration.verifyInvoiceWithEims(id); } @Post(":id/eims/resolve") @BookingStaff(FREIGHT_PERMS.invoices.eimsRegister) @ApiOperation({ summary: "Resolve an unacknowledged submission: record the IRN confirmed with MoR, or discard it. Clears the system-wide block.", }) resolve( @Param("id", ParseUUIDPipe) id: string, @Body() dto: ResolveEimsRegistrationDto, ) { return this.registration.resolveEimsRegistration(id, dto); } @Get(":id/eims/status") @BookingStaff(FREIGHT_PERMS.invoices.view) @ApiOperation({ summary: "EIMS registration status, IRN and last error for the invoice" }) status(@Param("id", ParseUUIDPipe) id: string) { return this.registration.getEimsStatus(id); } }