import {z} from 'zod' export type EntityReference = {entityId?: string | undefined, entityName?: string | undefined}; export type OnlyId = {id?: string | undefined}; export type CustomAttribute = {attributeDefinitionId?: string | undefined, booleanValue?: boolean | undefined, dateValue?: number | undefined, entityId?: string | undefined, entityReferences?: Array | undefined, numberValue?: unknown | undefined, selectedValueId?: string | undefined, selectedValues?: Array | undefined, stringValue?: string | undefined}; export type ProductionWorkScheduleItem = {id?: string | undefined, version?: string | undefined, costCenterId?: string | undefined, createdDate?: number | undefined, customAttributes?: Array | undefined, description?: string | undefined, lastModifiedDate?: number | undefined, multipleHumanOperation: number, multipleMachineOperation: number, positionNumber?: number | undefined, productionWorkScheduleId?: string | undefined, quantityBase?: number | undefined, setupTime: unknown, shortDescription?: string | undefined, timeType: 'SINGLE_TIME' | 'TIME_PER_UNIT', timeUnit: 'DAY' | 'HOUR' | 'MINUTE' | 'MONTH' | 'SECOND' | 'WEEK' | 'YEAR', unitTime: unknown, validFrom?: number | undefined, validTo?: number | undefined}; export type ProductionWorkSchedule = {id?: string | undefined, version?: string | undefined, createdDate?: number | undefined, customAttributes?: Array | undefined, description?: string | undefined, lastModifiedDate?: number | undefined, status: 'APPROVED' | 'ENTRY_IN_PROGRESS' | 'INVALID', workScheduleItems?: Array | undefined, workScheduleNumber: string}; export type CreateApiProductionWorkScheduleArgs = {dryRun?: boolean | undefined, body: ProductionWorkSchedule}; export type CreateApiProductionWorkScheduleOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const entityReference = z.object({entityId: z.string().optional(), entityName: z.string().optional()}); export const onlyId = z.object({id: z.string().optional()}); export const customAttribute = z.object({attributeDefinitionId: z.string().optional(), booleanValue: z.boolean().optional(), dateValue: z.number().int().optional(), entityId: z.string().optional(), entityReferences: z.array(entityReference).optional(), numberValue: z.string().optional(), selectedValueId: z.string().optional(), selectedValues: z.array(onlyId).optional(), stringValue: z.string().optional()}); export const productionWorkScheduleItem = z.object({id: z.string().optional(), version: z.string().optional(), costCenterId: z.string().optional(), createdDate: z.number().int().optional(), customAttributes: z.array(customAttribute).optional(), description: z.string().optional(), lastModifiedDate: z.number().int().optional(), multipleHumanOperation: z.number().int(), multipleMachineOperation: z.number().int(), positionNumber: z.number().int().optional(), productionWorkScheduleId: z.string().optional(), quantityBase: z.number().int().optional(), setupTime: z.string(), shortDescription: z.string().optional(), timeType: z.enum(["SINGLE_TIME", "TIME_PER_UNIT"]), timeUnit: z.enum(["DAY", "HOUR", "MINUTE", "MONTH", "SECOND", "WEEK", "YEAR"]), unitTime: z.string(), validFrom: z.number().int().optional(), validTo: z.number().int().optional()}); export const productionWorkSchedule = z.object({id: z.string().optional(), version: z.string().optional(), createdDate: z.number().int().optional(), customAttributes: z.array(customAttribute).optional(), description: z.string().optional(), lastModifiedDate: z.number().int().optional(), status: z.enum(["APPROVED", "ENTRY_IN_PROGRESS", "INVALID"]), workScheduleItems: z.array(productionWorkScheduleItem).optional(), workScheduleNumber: 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 }; /** * create a productionWorkSchedule * * POST /productionWorkSchedule */ export const createApiProductionWorkSchedule = async (args: CreateApiProductionWorkScheduleArgs, options: CreateApiProductionWorkScheduleOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/productionWorkSchedule${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(productionWorkSchedule, await res.json()) };