import {z} from 'zod' export type PricingFormula = {lastUpdateByName?: string | undefined, createdByName?: string | undefined, typedId?: string | undefined, lastUpdateDate?: string | undefined, label?: string | undefined, formulaNature?: string | undefined, version?: number | undefined, simulationSet?: string | undefined, uniqueName?: string | undefined, formulaType?: ('PRICING' | 'WORKFLOW' | 'LIBRARY') | undefined, createdBy?: number | undefined, elements?: (Array<{version?: number | undefined, typedId?: string | undefined, elementName?: string | undefined, elementLabel?: string | undefined, elementDescription?: string | undefined, elementGroups?: Array | undefined, conditionElementName?: string | undefined, hideWarnings?: boolean | undefined, excludeFromExport?: boolean | undefined, protectedExpression?: boolean | undefined, elementTimeout?: number | undefined, displayOptions?: number | undefined, formatType?: ('NUMERIC' | 'NUMERIC_LONG' | 'MONEY' | 'PERCENT' | 'TEXT' | 'MONEY_EUR' | 'MONEY_USD' | 'MONEY_GBP' | 'MONEY_JPY' | 'MONEY_CHF' | 'MONEY_PLN' | 'DATETIME' | 'DATE' | 'INTEGER' | 'LINK' | 'COMPLEX' | 'SPARKLINE') | undefined, elementSuffix?: string | undefined, allowOverride?: boolean | undefined, summarize?: boolean | undefined, hideOnNull?: boolean | undefined, userGroup?: string | undefined, cssProperties?: string | undefined, resultGroup?: string | undefined, combinationType?: ('ADD' | 'SUBTRACT' | 'RESULT' | 'INFO' | 'FUNCTION' | 'BRICK' | 'PYTHON') | undefined, criticalAlert?: ({message?: string | undefined, operator?: string | undefined, threshold?: number | undefined, comparisonElement?: string | undefined}) | undefined, redAlert?: Record | undefined, yellowAlert?: Record | undefined, labelTranslations?: string | undefined, createDate?: string | undefined, createdBy?: number | undefined, lastUpdateDate?: string | undefined, lastUpdateBy?: number | undefined, formulaExpression?: string | undefined}>) | undefined, userGroupEdit?: string | undefined, userGroupViewDetails?: string | undefined, validAfter?: string | undefined, inputDescriptors?: (Array<{name?: string | undefined, label?: string | undefined, labelTranslations?: string | undefined, group?: string | undefined, dataType?: string | undefined, inputType?: string | undefined, formatType?: string | undefined, from?: string | undefined, to?: string | undefined, values?: string | undefined, required?: boolean | undefined, alwaysEditable?: boolean | undefined}>) | undefined, status?: ('ACTIVE' | 'SIMULATIONONLY' | 'INACTIVE') | undefined, createDate?: string | undefined, lastUpdateBy?: number | undefined}; export type CreateApiFormulamanagerTestexecArgs = {body: {data: {targetDate?: string | undefined, testFormula: PricingFormula, knownElements?: Array | undefined, productSKU?: string | undefined, item?: string | undefined, items?: Array | undefined, nature?: string | undefined, natureValue?: string | undefined, headerPhases?: Record | undefined, formulaExpression?: string | undefined, allResults?: boolean | undefined, type?: string | undefined, debugPort?: string | undefined, allowObjectMods?: boolean | undefined, selectedRows?: Array | undefined, formulaFilterParam?: string | undefined, forceLineRecalculation?: boolean | undefined}}}; export type CreateApiFormulamanagerTestexecOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiFormulamanagerTestexecResponse = z.object({response: z.object({node: z.string().optional(), data: z.array(z.object({elementName: z.string().optional(), elementLabel: z.string().optional(), elementResultDescription: z.string().nullable().optional(), traceMessages: z.string().nullable().optional(), alertMessage: z.string().nullable().optional(), warnings: z.array(z.string()).nullable().optional(), alertType: z.string().nullable().optional(), displayOptions: z.number().int().optional(), formatType: z.string().nullable().optional(), overridden: z.boolean().optional(), overridable: z.boolean().optional(), summarize: z.boolean().optional(), hideOnNull: z.boolean().optional(), hideWarnings: z.boolean().optional(), excludeFromExport: z.boolean().optional(), userGroup: z.string().nullable().optional(), resultGroup: z.string().nullable().optional(), labelTranslations: z.string().nullable().optional(), suffix: z.string().nullable().optional(), cssProperties: z.string().nullable().optional(), cellStyles: z.string().nullable().optional(), overrideValueOptions: z.string().nullable().optional(), overrideAllowEmpty: z.boolean().optional(), fieldType: z.number().int().optional(), resultType: z.string().optional(), elementResultClass: z.string().nullable().optional(), elementResult: z.string().nullable().optional()})).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 }; /** * Test a Logic * * POST /formulamanager.testexec * * Also accepts auth: X-PriceFx-jwt */ export const createApiFormulamanagerTestexec = async (args: CreateApiFormulamanagerTestexecArgs, options?: CreateApiFormulamanagerTestexecOptions) => { 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(/\/+$/, '')}/formulamanager.testexec${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(createApiFormulamanagerTestexecResponse, await res.json()) };