import {z} from 'zod' export type CreateApiPayrollEmployeesTaxCardFetchArgs = {body: {customer_id?: string | undefined, employee_id: string, contract_id: string, payment_date: string}}; export type CreateApiPayrollEmployeesTaxCardFetchOptions = {baseUrl?: string | undefined, firebaseToken?: string | undefined, fetch?: typeof fetch | undefined}; export const payrollTaxCardFetchSummary = z.object({customer_id: z.string(), employee_id: z.string(), contract_id: z.string(), fetch_id: z.string().optional(), environment: z.enum(["test", "production"]).optional(), payment_date: z.string(), outcome: z.enum(["applied", "unchanged", "not_found", "review_required"]), changed: z.boolean(), authority_base_basis_points: z.number().int().optional(), authority_additional_basis_points: z.number().int().optional(), annual_income_ceiling_cents: z.number().int().optional(), valid_from: z.string().optional(), employee_requested_minimum_basis_points: z.number().int().optional(), applied_base_basis_points: z.number().int().optional(), applied_additional_basis_points: z.number().int().optional(), includes_stock_options_and_grants: z.boolean(), nonresident_progressive_tax_claim: z.boolean(), voided_draft_run_ids: z.array(z.string()), note: z.string().optional()}); export const payrollWorkspaceData = z.record(z.string(), z.unknown()); export const createApiPayrollEmployeesTaxCardFetchResponse = z.object({tax_card: payrollTaxCardFetchSummary, workspace: payrollWorkspaceData}); 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 }; /** * Fetch an employee's wage tax card from Vero * * POST /payroll/employees/tax-card/fetch */ export const createApiPayrollEmployeesTaxCardFetch = async (args: CreateApiPayrollEmployeesTaxCardFetchArgs, options?: CreateApiPayrollEmployeesTaxCardFetchOptions) => { 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(/\/+$/, '')}/payroll/employees/tax-card/fetch${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(createApiPayrollEmployeesTaxCardFetchResponse, await res.json()) };