import {z} from 'zod' export type StrategyAddressUpdate = {street: string, number: string, complement?: (string | null) | undefined, district: string, city: string, state: string, country: string, zip_code: string}; export type StrategyContactUpdate = {name?: (string | null) | undefined, email?: (string | null) | undefined, phone?: (string | null) | undefined}; export type StrategyOperatingHourUpdate = {day: string, is_24h?: boolean | undefined, opens_at?: (string | null) | undefined, closes_at?: (string | null) | undefined}; export type StrategyHolidayHoursUpdate = {is_24h?: boolean | undefined, opens_at?: (string | null) | undefined, closes_at?: (string | null) | undefined}; export type StrategyUpdate = {name?: string | undefined, description?: (string | null) | undefined, type?: ('STORE' | 'DC' | 'DARK_STORE' | 'SELLER' | 'FULFILLMENT_NODE') | undefined, category?: ('SALES' | 'OPERATIONS' | 'SALES_AND_OPERATIONS') | undefined, active?: boolean | undefined, daily_capacity?: number | undefined, sfs?: boolean | undefined, endless_aisle?: boolean | undefined, operates_on_holidays?: boolean | undefined, cluster_id?: (string | null) | undefined, state_machine_id?: (string | null) | undefined, color?: (string | null) | undefined, address?: (StrategyAddressUpdate | null) | undefined, contact?: (StrategyContactUpdate | null) | undefined, operating_hours?: (Array | null) | undefined, holiday_operating_hours?: (StrategyHolidayHoursUpdate | null) | undefined, tags?: (Array | null) | undefined, metadata?: (Record | null) | undefined}; export type PatchApiApiV1StrategiesIdArgs = {id: string, body: StrategyUpdate}; export type PatchApiApiV1StrategiesIdOptions = {baseUrl?: string | undefined, basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const strategyUpdateResponse = z.object({id: z.string(), code: z.string(), name: z.string(), description: z.string().nullable(), type: z.enum(["STORE", "DC", "DARK_STORE", "SELLER", "FULFILLMENT_NODE"]), category: z.enum(["SALES", "OPERATIONS", "SALES_AND_OPERATIONS"]), active: z.boolean(), sfs: z.boolean(), endless_aisle: z.boolean(), operates_on_holidays: z.boolean(), cluster_id: z.string().nullable(), state_machine_id: z.string().nullable(), color: z.string().nullable(), daily_capacity: z.number(), address: z.object({street: z.string(), number: z.string(), complement: z.string().nullable(), district: z.string(), city: z.string(), state: z.string(), country: z.string(), zip_code: z.string()}), contact: z.object({name: z.string().nullable(), email: z.string().nullable(), phone: z.string().nullable()}), operating_hours: z.array(z.object({day: z.string(), is_24h: z.boolean(), opens_at: z.string().nullable(), closes_at: z.string().nullable()})), holiday_operating_hours: z.object({is_24h: z.boolean(), opens_at: z.string().nullable(), closes_at: z.string().nullable()}), tags: z.array(z.string()).nullable(), metadata: z.object({}).nullable(), created_at: z.string(), updated_at: 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 }; /** * Update strategy * * PATCH /api/v1/strategies/{id} * * Also accepts auth: bearer */ export const patchApiApiV1StrategiesId = async (args: PatchApiApiV1StrategiesIdArgs, options?: PatchApiApiV1StrategiesIdOptions) => { const {baseUrl = "https://oms-api.tenant.krzlabs.io", 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(/\/+$/, '')}/api/v1/strategies/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(strategyUpdateResponse, await res.json()) };