import {z} from 'zod' export type UpdateOperation = {jobId?: string | undefined, personId?: string | undefined, date?: string | undefined, update: {[key: string]: Record}}; export type BulkJobPersonUpdateRequest = {updates: Array, date: string, scenarioId?: string | undefined, note?: string | undefined}; export type CreateApiV2OrgOrgIdJobBulkupdatePreviewArgs = {orgId: string, enrichLinkedFields?: boolean | undefined, format?: string | undefined, body: BulkJobPersonUpdateRequest}; export type CreateApiV2OrgOrgIdJobBulkupdatePreviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const jobEnrichmentPreview = z.object({jobId: z.string().optional(), personId: z.string().optional(), enrichedFields: z.record(z.string(), z.object({})), previousFields: z.record(z.string(), z.object({}))}); export const bulkJobEnrichmentPreviewResponse = z.object({previews: z.array(jobEnrichmentPreview)}); 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 }; /** * Preview the job code linked fields a bulk update will overwrite, without saving * * POST /v2/org/{orgId}/job/bulkupdate/preview */ export const createApiV2OrgOrgIdJobBulkupdatePreview = async (args: CreateApiV2OrgOrgIdJobBulkupdatePreviewArgs, options?: CreateApiV2OrgOrgIdJobBulkupdatePreviewOptions) => { 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.enrichLinkedFields != null) query.append("enrichLinkedFields", String(args.enrichLinkedFields)) if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/org/${encodeURIComponent(String(args.orgId))}/job/bulkupdate/preview${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(bulkJobEnrichmentPreviewResponse, await res.json()) };