import {z} from 'zod' export type GetApiOrganisationsOrganisationIdPayRunsPayRunIdReconciliationArgs = {organisationId: string, payRunId: string, 'Crezco-Version'?: string | undefined}; export type GetApiOrganisationsOrganisationIdPayRunsPayRunIdReconciliationOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const payableFundingAmountResourceRepresentationVersion4 = z.object({currencyCode: z.string(), amountInMinorUnits: z.number().int(), isEstimated: z.boolean()}); export const payableIdentifiersResourceRepresentationVersion4 = z.object({payableId: z.string().optional(), partnerEntityId: z.string().min(1).max(50)}); export const payRunReconciliationItemRepresentationVersion4 = z.object({reference: z.string().nullable().optional(), fundingAmount: payableFundingAmountResourceRepresentationVersion4, targetBankAccountRoutingName: z.string().nullable().optional(), payables: z.array(payableIdentifiersResourceRepresentationVersion4)}); export const payRunReconciliationRepresentationVersion4 = z.object({items: z.array(payRunReconciliationItemRepresentationVersion4).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 }; /** * Get pay run reconciliation * * GET /organisations/{organisationId}/pay-runs/{payRunId}/reconciliation */ export const getApiOrganisationsOrganisationIdPayRunsPayRunIdReconciliation = async (args: GetApiOrganisationsOrganisationIdPayRunsPayRunIdReconciliationArgs, options?: GetApiOrganisationsOrganisationIdPayRunsPayRunIdReconciliationOptions) => { const {baseUrl = "https://api.partners.uat.crezco.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Crezco-Version'] != null) headers.set("Crezco-Version", String(args['Crezco-Version'])) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/organisations/${encodeURIComponent(String(args.organisationId))}/pay-runs/${encodeURIComponent(String(args.payRunId))}/reconciliation${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(payRunReconciliationRepresentationVersion4, await res.json()) };