import {z} from 'zod' export type RequestedBalanceAdjustment = {personId: string, days?: number | undefined}; export type TimeOffBalanceAdjustmentBody = {balances: Array}; export type CreateApiV1OrgOrgIdTimeoffPolicyBalancesPolicyIdArgs = {orgId: string, policyId: string, date?: string | undefined, enterMatching?: boolean | undefined, body: TimeOffBalanceAdjustmentBody}; export type CreateApiV1OrgOrgIdTimeoffPolicyBalancesPolicyIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const attribution = z.object({principalUserId: z.string().optional(), agentUserIds: z.array(z.string()).optional(), eventId: z.string().optional(), aiChatId: z.string().optional(), aiToolUseId: z.string().optional(), channel: z.enum(["WEB", "MOBILE", "SLACK", "TEAMS", "MCP"]).optional()}); export const logData = z.object({level: z.enum(["INFO", "WARN", "ERROR"]), at: z.string(), message: z.string().optional(), data: z.record(z.string(), z.object({}))}); export const process = z.object({id: z.string(), orgId: z.string(), label: z.string(), type: z.string(), status: z.enum(["PENDING", "RUNNING", "DONE", "ERROR"]), filePath: z.string().optional(), logPath: z.string().optional(), runUserId: z.string(), parentProcessId: z.string().optional(), createId: z.string(), createBehalfId: z.string().optional(), createAttribution: attribution.optional(), createAt: z.string(), updateId: z.string().optional(), updateBehalfId: z.string().optional(), updateAttribution: attribution.optional(), updateAt: z.string().optional(), startAt: z.string().optional(), endAt: z.string().optional(), message: z.string().optional(), progress: z.number().optional(), internalError: z.string().optional(), options: z.object({}), results: z.record(z.string(), z.object({})).optional(), logDataList: z.array(logData).optional(), state: z.object({}).optional(), summary: z.string().optional(), appId: z.string().optional(), uuid: z.string().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 }; /** * Adjust time off balances for a group of people on a given policy and effective date * * POST /v1/org/{orgId}/timeoff/policy/balances/{policyId} */ export const createApiV1OrgOrgIdTimeoffPolicyBalancesPolicyId = async (args: CreateApiV1OrgOrgIdTimeoffPolicyBalancesPolicyIdArgs, options?: CreateApiV1OrgOrgIdTimeoffPolicyBalancesPolicyIdOptions) => { const {baseUrl = "https://localhost", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.date != null) query.append("date", String(args.date)) if (args.enterMatching != null) query.append("enterMatching", String(args.enterMatching)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/org/${encodeURIComponent(String(args.orgId))}/timeoff/policy/balances/${encodeURIComponent(String(args.policyId))}${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(process, await res.json()) };