/** * `erpWorkOrderOperations` GraphQL query pieces shared by the `MMApiClient` operation reads. */ import { collectNonBlankStrings, type GraphqlOperation, } from "./graphql-helpers.js"; /** Field list for the erpWorkOrderOperations GraphQL queries (names match the REST import payload). */ export const ERP_WORK_ORDER_OPERATION_GRAPHQL_FIELDS = "companyId workOrderId lot split sub sequenceNumber resourceId startQuantity finishQuantity expectedRejectRate scheduledStartDate scheduledFinishDate closedDate cycleTimeMs setupTimeMs productionburdenRateHourly setupburdenRatehourly operationType quantityPerPart status createdAt updatedAt customFieldsSchemaRef customFields"; /** Default number of work order ids per `_in` query chunk. */ export const DEFAULT_WORK_ORDER_IDS_CHUNK_SIZE = 200; /** Stable composite-key ordering used by every paged operations read. */ export const ERP_WORK_ORDER_OPERATION_GRAPHQL_ORDER_BY = "[{ workOrderId: asc }, { lot: asc }, { split: asc }, { sub: asc }, { sequenceNumber: asc }]"; /** Trim, drop blanks and dedupe a work order id list exactly, preserving first-seen order. */ export function normalizeWorkOrderIdList( workOrderIds: readonly string[] ): string[] { return collectNonBlankStrings(undefined, workOrderIds); } /** * Operations of the given work orders (`workOrderId _in`), one page. Ids travel as a * variable, never interpolated, so they need no escaping. */ export function buildWorkOrderOperationsByWorkOrderIdsQuery( workOrderIds: readonly string[], pageSize: number, offset: number ): GraphqlOperation { const operationName = "getErpWorkOrderOperationsByWorkOrderIds"; return { operationName, query: `query ${operationName}($ids: [String!]!, $limit: Int!, $offset: Int!) { ` + `workOrderOperations: erpWorkOrderOperations(where: { workOrderId: { _in: $ids } }, ` + `limit: $limit, offset: $offset, order_by: ${ERP_WORK_ORDER_OPERATION_GRAPHQL_ORDER_BY}) { ` + `${ERP_WORK_ORDER_OPERATION_GRAPHQL_FIELDS} } }`, variables: { ids: workOrderIds, limit: pageSize, offset }, }; }