import {z} from 'zod' export type BudgetResetPeriod = 'DAILY' | 'WEEKLY' | 'FORTNIGHTLY' | 'MONTHLY' | 'QUARTERLY' | 'ANNUALLY' | 'JULY_FINANCIAL_YEAR' | 'APRIL_FINANCIAL_YEAR' | 'NEVER'; export type BudgetCustomFieldInput = {id: string, allow_options?: Array | undefined}; export type BudgetUpdateRequest = {name?: string | undefined, reset_amount?: number | undefined, reset_period?: BudgetResetPeriod | undefined, has_member_limits?: boolean | undefined, rollover_funds?: boolean | undefined, limit_categories?: boolean | undefined, categories?: Array | undefined, custom_fields?: Array | undefined, payment_types?: (Array<'bills' | 'cards' | 'reimbursements'>) | undefined}; export type PatchApiV1BusinessesClientIdBudgetsBudgetIdArgs = {client_id: number, budget_id: string, body: BudgetUpdateRequest}; export type PatchApiV1BusinessesClientIdBudgetsBudgetIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const budgetResetPeriod = z.enum(["DAILY", "WEEKLY", "FORTNIGHTLY", "MONTHLY", "QUARTERLY", "ANNUALLY", "JULY_FINANCIAL_YEAR", "APRIL_FINANCIAL_YEAR", "NEVER"]); export const budget = z.object({id: z.string(), name: z.string(), budget_type: z.enum(["BUDGET", "SUBSCRIPTION"]), available_amount: z.number(), spent_amount: z.number(), reset_amount: z.number(), reset_period: budgetResetPeriod, current_period_start: z.string().nullable(), current_period_end: z.string().nullable(), has_member_limits: z.boolean(), rollover_funds: z.boolean(), limit_categories: z.boolean(), categories: z.array(z.object({id: z.string(), name: z.string()})), custom_fields: z.array(z.object({id: z.string(), name: z.string(), allow_options: z.array(z.string()).nullable()})), is_deleted: z.boolean(), created: z.string(), updated: z.string(), payment_types: z.array(z.enum(["bills", "cards", "reimbursements"]))}); 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 budget * * PATCH /v1/businesses/{client_id}/budgets/{budget_id} */ export const patchApiV1BusinessesClientIdBudgetsBudgetId = async (args: PatchApiV1BusinessesClientIdBudgetsBudgetIdArgs, options?: PatchApiV1BusinessesClientIdBudgetsBudgetIdOptions) => { const {baseUrl = "https://public.letsweel.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/businesses/${encodeURIComponent(String(args.client_id))}/budgets/${encodeURIComponent(String(args.budget_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(budget, await res.json()) };