import {z} from 'zod' export type CalculationGrid = {lastUpdateByName?: (string | null) | undefined, createdByName?: (string | null) | undefined, updateDate?: (string | null) | undefined, typedId?: string | undefined, configuration?: string | undefined, relativeTargetDateDays?: (number | null) | undefined, lastUpdateDate?: string | undefined, numberOfSubmittedItems?: number | undefined, numberOfAutoApprovedItems?: number | undefined, locale?: (string | null) | undefined, numberOfDeniedItems?: number | undefined, calculationDate?: (string | null) | undefined, keyGenerationType?: ('DATA_DRIVEN' | 'LOGIC_DRIVEN' | 'MANUAL') | undefined, writeOnlyChangedItems?: boolean | undefined, userGroupViewDetails?: (string | null) | undefined, id?: number | undefined, calculationGridType?: ('CG1' | 'CG2' | 'CG3' | 'CG4' | 'CG5' | 'CG6' | 'null' | null) | undefined, createDate?: string | undefined, calculationMessages?: (string | null) | undefined, updatedBy?: number | undefined, numberOfApprovedItems?: number | undefined, keySize?: number | undefined, targetDate?: string | undefined, label?: string | undefined, version?: number | undefined, partialCalculationDate?: (string | null) | undefined, numberOfOpenItems?: number | undefined, createdBy?: number | undefined, numberOfItems?: number | undefined, userGroupEdit?: (string | null) | undefined, nodeId?: (number | null) | undefined, keepManualOverrides?: boolean | undefined, calculationStartDate?: (string | null) | undefined, lastUpdateBy?: number | undefined, status?: ('DRAFT' | 'PENDING' | 'SCHEDULED' | 'PROCESSING' | 'SCHEDULED_DIRTY' | 'ERROR' | 'READY' | 'CANCELLED') | undefined, approvalLevel?: 'LINE_ITEM' | undefined}; export type CreateApiUpdateCGArgs = {body: {data?: ({typedId?: string | undefined, label?: string | undefined}) | undefined, oldValues?: CalculationGrid | undefined, operationType?: string | undefined, textMatchStyle?: string | undefined}}; export type CreateApiUpdateCGOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const calculationGrid = z.object({lastUpdateByName: z.string().max(255).nullable().optional(), createdByName: z.string().max(255).nullable().optional(), updateDate: z.string().nullable().optional(), typedId: z.string().optional(), configuration: z.string().optional(), relativeTargetDateDays: z.number().int().nullable().optional(), lastUpdateDate: z.string().optional(), numberOfSubmittedItems: z.number().int().optional(), numberOfAutoApprovedItems: z.number().int().optional(), locale: z.string().max(255).nullable().optional(), numberOfDeniedItems: z.number().int().optional(), calculationDate: z.string().nullable().optional(), keyGenerationType: z.enum(["DATA_DRIVEN", "LOGIC_DRIVEN", "MANUAL"]).max(255).optional(), writeOnlyChangedItems: z.boolean().optional(), userGroupViewDetails: z.string().max(255).nullable().optional(), id: z.number().int().optional(), calculationGridType: z.enum(["CG1", "CG2", "CG3", "CG4", "CG5", "CG6", "null"]).max(255).nullable().optional(), createDate: z.string().optional(), calculationMessages: z.string().nullable().optional(), updatedBy: z.number().int().optional(), numberOfApprovedItems: z.number().int().optional(), keySize: z.number().int().optional(), targetDate: z.string().optional(), label: z.string().max(255).optional(), version: z.number().int().optional(), partialCalculationDate: z.string().nullable().optional(), numberOfOpenItems: z.number().int().optional(), createdBy: z.number().int().optional(), numberOfItems: z.number().int().optional(), userGroupEdit: z.string().max(255).nullable().optional(), nodeId: z.number().int().nullable().optional(), keepManualOverrides: z.boolean().optional(), calculationStartDate: z.string().nullable().optional(), lastUpdateBy: z.number().int().optional(), status: z.enum(["DRAFT", "PENDING", "SCHEDULED", "PROCESSING", "SCHEDULED_DIRTY", "ERROR", "READY", "CANCELLED"]).max(255).optional(), approvalLevel: z.literal("LINE_ITEM").max(255).optional()}); export const createApiUpdateCGResponse = z.object({response: z.object({node: z.string().optional(), data: z.array(calculationGrid).optional(), status: z.number().int().optional()}).optional()}); 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 }; /** * Update a Calculation Grid * * POST /update/CG * * Also accepts auth: X-PriceFx-jwt */ export const createApiUpdateCG = async (args: CreateApiUpdateCGArgs, options?: CreateApiUpdateCGOptions) => { const {baseUrl = "https://companynode.pricefx.com/pricefx/companypartition", fetch: fetchFn = fetch, basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basic !== undefined) headers.set('authorization', `Basic ${btoa(`${basic.username}:${basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/update/CG${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(createApiUpdateCGResponse, await res.json()) };