import { Api } from '../api' import { CreatedJobStatus, DocumentQueryParams, ICustomerOrderLine, IDocumentInfo, IHandlingUnit, IOperation, IOSMMaterial, IPurchaseOrderLine, IRequisitionStatus, IRoutingAlternate, IShipment, IShipmentLine, IShopOrder, IShopOrderPartBreakdown, ISupplierInfo, JobOutput, JobsOutputResponse, OperationsQueryParams, OSMMaterialsQueryParams, PresignedUrlData, PresignedUrlResponse, RoutingAlternatesQueryParams, SmbFolderNode, SupplierQueryParams, } from './imi.interface' import { DocumentFileLookupResult, IJobOrderData, SmbShareListingResponse, } from './imi_sti.interface' export const IMI_API = '/imi' export const SMB_API = '/service/smb' export class IMI { api: Api constructor(api: Api) { this.api = api } getJobsStatus(jobIds: number[]) { return this.api.apisauce.post(`${IMI_API}/jobs/ids`, { data: jobIds, }) } createJobs(shopOrders: [string, string][]) { return this.api.apisauce.post>( `${IMI_API}/jobs/create`, { data: shopOrders }, ) } getJobOutputs(jobIds: number[]) { return this.api.apisauce.post>( `${IMI_API}/jobs/output`, { data: jobIds }, ) } getAllJobsOutputs() { return this.api.apisauce.get(`${IMI_API}/jobs`) } getDocumentInfo(params: DocumentQueryParams) { return this.api.apisauce.post( `${IMI_API}/document-info`, params, ) } uploadExternalFilesUrl(data: PresignedUrlData) { return this.api.apisauce.post( `${IMI_API}/remote-files/presigned-urls`, data, ) } getShopOrders(contract: string, shopOrders: string[]) { return this.api.apisauce.post( `${IMI_API}/shop-orders`, { contract, shopOrders }, ) } /** * Always returns an IOperation[] body, for both branches. * * Status-branch calls (`{ status, contract, limit?, offset? }`) return the * current page as the array, with the total count across all pages in the * `x-count` response header — read it off `res.headers['x-count']`. * Callers already know the `limit`/`offset` they requested, so page * through until `offset + rows.length >= total`. * * Shop-order-branch calls (`{ contract, shopOrder }`) return all operations * for that shop order, unpaginated; there's no `x-count` header for these. */ getOperations(params: OperationsQueryParams) { return this.api.apisauce.post( `${IMI_API}/operations`, params, ) } getRoutingAlternates(params: RoutingAlternatesQueryParams) { return this.api.apisauce.post( `${IMI_API}/routing-alternates`, params, ) } getHandlingUnits(params: { contract: string; shipment: string }) { return this.api.apisauce.post( `${IMI_API}/handling-units`, params, ) } getCustomerOrderLine(params: { shopOrder: string }) { return this.api.apisauce.post( `${IMI_API}/customer-order-line`, params, ) } getShipments(params: { contract: string; shipments: string[] }) { return this.api.apisauce.post( `${IMI_API}/shipments`, params, ) } getSupplier(params: SupplierQueryParams) { return this.api.apisauce.post( `${IMI_API}/supplier`, params, ) } getShipmentLines(params: { shipmentId: string }) { return this.api.apisauce.post( `${IMI_API}/shipment-lines`, params, ) } getPurchaseOrderLines(params: { contract: string; purchaseOrderId: string }) { return this.api.apisauce.post( `${IMI_API}/purchase-order`, params, ) } getShopOrderPartBreakdown(contract: string, shopOrders: string[]) { return this.api.apisauce.post< IShopOrderPartBreakdown[], { message: string } >(`${IMI_API}/osm/so/parts`, { contract, shopOrders }) } getOSMMaterials(params: OSMMaterialsQueryParams) { return this.api.apisauce.post( `${IMI_API}/osm/materials`, params, ) } getRequisitionsStatus(contract: string, requisitions: string[]) { return this.api.apisauce.post( `${IMI_API}/osm/requisitions/status`, { contract, requisitions }, ) } getJobOrderListing(jobOrderNo: string) { return this.api.apisauce.get( `${SMB_API}/job-orders/${jobOrderNo}/listing`, ) } getJobOrderData(jobOrderNo: string) { return this.api.apisauce.get( `${IMI_API}/sti/job-order/${jobOrderNo}`, ) } getDocumentFileNames(jobOrderNo: string, lineNos: string[] | undefined, classCode: string) { const params: Record = { jobOrderNo, classCode } // Omit lineNos entirely when undefined / empty — edocu-imi treats a // missing lineNos as "all lines for this JO". if (lineNos && lineNos.length > 0) { params.lineNos = lineNos.join(',') } return this.api.apisauce.get( `${IMI_API}/sti/document-files`, params, ) } listSmbFilesByPrefix(logicalShareName: string, prefix: string) { return this.api.apisauce.get( `${SMB_API}/shares/${encodeURIComponent(logicalShareName)}/list`, { prefix }, ) } getSmbFileUrl(logicalShareName: string, shareRelativePath: string) { const encodedPath = shareRelativePath .split('/') .map((segment) => encodeURIComponent(segment)) .join('/') return `${this.api.apisauce.getBaseURL()}${SMB_API}/files/${logicalShareName}/${encodedPath}` } }