import { api as client } from "../auth/http"; const BASE = "/contract-templates"; export const BULK_TEMPLATE_DIRECTIONS = ["IMPORT", "EXPORT", "INTERCITY"] as const; export type BulkTemplateDirection = (typeof BULK_TEMPLATE_DIRECTIONS)[number]; export interface ContractTemplateArticle { id: string; title: string; body: string; order: number; } export interface ContractTemplate { id: string; // System container templates use the fixed DIRECTION_CONTAINER(_CUSTOMS) // codes; staff-created bulk templates get generated BULK__* codes. code: string; name: string; description?: string | null; documentTitle: string; whereasClauses: string[]; articles: ContractTemplateArticle[]; isActive: boolean; /** Bulk templates only: the cargo type this template is written for. */ cargoTypeId?: string | null; cargoType?: { id: string; cargoTypeName: string } | null; /** Bulk templates only: IMPORT, EXPORT or INTERCITY. */ tradeDirection?: BulkTemplateDirection | null; /** * Bulk templates only: whether this is the with-customs-clearing variant. * Null for intercity — domestic movements have no customs leg. */ withCustoms?: boolean | null; /** * Bulk templates only: the with-customs variant restricted to Ethiopian-side * clearing (Djibouti stays with the client). */ ethiopianCustomsOnly?: boolean | null; /** The seeded container templates — cannot be deleted. */ isSystem: boolean; createdAt: string; updatedAt: string; } export interface CreateContractTemplatePayload { cargoTypeId: string; tradeDirection: BulkTemplateDirection; /** Omitted for INTERCITY — the API rejects the flag there. */ withCustoms?: boolean; /** Ethiopian-side clearing only; requires withCustoms: true. */ ethiopianCustomsOnly?: boolean; name?: string; description?: string; } export interface UpdateContractTemplatePayload { name?: string; description?: string; documentTitle?: string; whereasClauses?: string[]; isActive?: boolean; } export interface ArticlePayload { title: string; body: string; position?: number; } export const contractTemplatesService = { async list(): Promise { const { data } = await client.get(BASE); return data; }, async create(payload: CreateContractTemplatePayload): Promise { const { data } = await client.post(BASE, payload); return data; }, async remove(code: string): Promise { await client.delete(`${BASE}/${code}`); }, async getByCode(code: string): Promise { const { data } = await client.get(`${BASE}/${code}`); return data; }, async update( code: string, payload: UpdateContractTemplatePayload, ): Promise { const { data } = await client.patch( `${BASE}/${code}`, payload, ); return data; }, async preview(code: string): Promise<{ html: string }> { const { data } = await client.post<{ html: string }>( `${BASE}/${code}/preview`, {}, ); return data; }, async addArticle(code: string, payload: ArticlePayload): Promise { const { data } = await client.post( `${BASE}/${code}/articles`, payload, ); return data; }, async updateArticle( code: string, articleId: string, payload: Partial, ): Promise { const { data } = await client.patch( `${BASE}/${code}/articles/${articleId}`, payload, ); return data; }, async removeArticle(code: string, articleId: string): Promise { const { data } = await client.delete( `${BASE}/${code}/articles/${articleId}`, ); return data; }, async replaceArticles( code: string, articles: Array<{ id?: string; title: string; body: string }>, ): Promise { const { data } = await client.put( `${BASE}/${code}/articles`, { articles }, ); return data; }, };