This commit is contained in:
marshal
2026-07-01 20:55:17 +03:00
parent 612df8daff
commit 9c18d086d7
112 changed files with 5654 additions and 1370 deletions

View File

@@ -0,0 +1,61 @@
import { BadRequestException } from '@nestjs/common';
import {
catalogEntriesForTradeDirection,
type ClearanceWorkflowFile,
} from '@edr/types';
const IMPORT_DECLARATION_CODES = new Set(['im4', 'im5']);
const EXPORT_DECLARATION_CODES = new Set(['ex3', 'ex8']);
/** Require at least one declaration file for the trade direction (IM4 or IM5, EX3 or EX8). */
export function assertDeclarationFiles(
files: Express.Multer.File[],
tradeDirection: string,
): void {
if (files.length === 0) {
throw new BadRequestException('No declaration documents uploaded');
}
const allowed =
tradeDirection === 'EXPORT' ? EXPORT_DECLARATION_CODES : IMPORT_DECLARATION_CODES;
const labels = tradeDirection === 'EXPORT' ? 'EX3 or EX8' : 'IM4 or IM5';
const uploaded = new Set(files.map((f) => f.fieldname?.toLowerCase()));
const hasValid = [...allowed].some((code) => uploaded.has(code));
if (!hasValid) {
throw new BadRequestException(`Upload at least one declaration document (${labels}).`);
}
}
export function parseDutyRequiredForm(value: string | boolean | undefined): boolean {
if (typeof value === 'boolean') return value;
if (value === undefined || value === '') return false;
return value === 'true' || value === '1';
}
/** Build labeled phased-customs file rows from resource files. */
export function buildWorkflowFiles(
files: Array<{ code?: string | null; id: string; name: string; url: string }>,
tradeDirection: string,
documentFileKeys: Set<string> = new Set(),
): ClearanceWorkflowFile[] {
const fileByCode = new Map(
files.filter((f) => f.code).map((f) => [f.code as string, f]),
);
const out: ClearanceWorkflowFile[] = [];
for (const entry of catalogEntriesForTradeDirection(tradeDirection)) {
if (documentFileKeys.has(entry.code)) continue;
const file = fileByCode.get(entry.code) ?? null;
if (!file) continue;
out.push({
code: entry.code,
label: entry.label,
uploadedBy: entry.uploadedBy,
category: entry.category,
file: { id: file.id, name: file.name, url: file.url },
});
}
return out;
}