import {z} from 'zod' export type ItemRoutingOperationTimeOption = 'fixedSeconds' | 'fixedMinutes' | 'fixedHours' | 'fixedDays' | 'secondsPerUnit' | 'minutesPerUnit' | 'hoursPerUnit' | 'daysPerUnit' | 'unitsPerHour'; export type ItemRoutingOperationTime = {time?: number | undefined, option?: ItemRoutingOperationTimeOption | undefined}; export type ItemRoutingOutsideProcessingCostOption = 'fixed' | 'perUnit'; export type ItemRoutingOutsideProcessingOperationCost = {costOption?: ItemRoutingOutsideProcessingCostOption | undefined, fixedCost?: (number | null) | undefined, perUnitCost?: (number | null) | undefined}; export type ItemRoutingOutsideProcessingCostBreakPointDto = {quantity?: number | undefined, price?: (number | null) | undefined}; export type ItemRoutingOutsideProcessingOperationDto = {outsideProcessingTime?: ItemRoutingOperationTime | undefined, outsideProcessingCost?: ItemRoutingOutsideProcessingOperationCost | undefined, selectedVendorId?: (string | null) | undefined, costBreaks?: (Array | null) | undefined}; export type ItemRoutingNormalOperationDto = {setupTime?: ItemRoutingOperationTime | undefined, laborTime?: ItemRoutingOperationTime | undefined, machineTime?: ItemRoutingOperationTime | undefined, anyEquipment?: boolean | undefined, selectedEquipmentIds?: Array | undefined, isNestable?: boolean | undefined}; export type ItemRoutingOperationCreateDto = {systemOperationId: string, order?: (number | null) | undefined, outsideProcessingOperation?: ItemRoutingOutsideProcessingOperationDto | undefined, operation?: ItemRoutingNormalOperationDto | undefined, leadDays?: (number | null) | undefined, unattended?: (boolean | null) | undefined, instructions?: (string | null) | undefined, inputMaterialIds?: (Array | null) | undefined}; export type CreateApiApiItemsItemIdRoutingOperationsBatchArgs = {itemId: string, body: Array}; export type CreateApiApiItemsItemIdRoutingOperationsBatchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const batchCreatedResponseDto = z.object({ids: z.array(z.string())}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Replace an item's routing operations with the ordered list sent. Send every operation the routing * should have, in order. Each entry reuses the existing step with the same systemOperationId (the first * by order; each step is used once); otherwise a new step is added. On a reused step, omitted fields keep * their current values, except leadDays, which becomes 0, and its equipment selection does not change. * Steps not in the list are removed; their BOM lines and materials stay on the routing but are no longer * tied to a step. Send order on every entry or on none; with none, steps are numbered by the shop's * operation-number increment. Read the current steps first with operations/list. Open jobs keep their own * copy of the routing and do not change. * * POST /api/items/{itemId}/routing/operations/batch */ export const createApiApiItemsItemIdRoutingOperationsBatch = async (args: CreateApiApiItemsItemIdRoutingOperationsBatchArgs, options?: CreateApiApiItemsItemIdRoutingOperationsBatchOptions) => { const {baseUrl = "https://api.fulcrumpro.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/items/${encodeURIComponent(String(args.itemId))}/routing/operations/batch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(batchCreatedResponseDto, await res.json()) };