import {z} from 'zod' export type CreateApiCompanySettingsArgs = {body: {customer_id?: string | undefined, fiscal_year_start_month?: number | undefined, vat_period_months?: number | undefined, vat_period_valid_from?: string | undefined}}; export type CreateApiCompanySettingsOptions = {baseUrl?: string | undefined, firebaseToken?: string | undefined, fetch?: typeof fetch | undefined}; export const companyProfile = z.object({customer_id: z.string().optional(), legal_name: z.string().optional(), business_id: z.string().optional(), vat_number: z.string().optional(), country_code: z.string().optional(), currency: z.string().optional(), fiscal_year_start_month: z.number().int().optional(), vat_period_months: z.number().int().optional(), vat_period_source: z.enum(["dated", "column", "default"]).optional(), company_form: z.string().optional(), statement_format: z.string().optional(), editable: z.boolean().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 company accounting settings (admin/approver only) * * POST /company/settings */ export const createApiCompanySettings = async (args: CreateApiCompanySettingsArgs, options?: CreateApiCompanySettingsOptions) => { const {baseUrl = "https://api.app.lastaccountingcompany.com/portal", fetch: fetchFn = fetch, firebaseToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (firebaseToken !== undefined) headers.set('authorization', `Bearer ${firebaseToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/company/settings${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(companyProfile, await res.json()) };