import {z} from 'zod' export type GetApiOrganisationsOrganisationIdPayRunsPayRunIdArgs = {organisationId: string, payRunId: string, 'Crezco-Version'?: string | undefined}; export type GetApiOrganisationsOrganisationIdPayRunsPayRunIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentSourceTypeVersion4 = z.enum(["BankAccount", "Card"]); export const payableFundingAmountResourceRepresentationVersion4 = z.object({currencyCode: z.string(), amountInMinorUnits: z.number().int(), isEstimated: z.boolean()}); export const failureInsightsResolutionResourceRepresentationVersion4 = z.object({$type: z.string()}); export const failureInsightsSuggestionResourceRepresentationVersion4 = z.object({$type: z.string()}); export const payableReversedAmountResourceRepresentationVersion4 = z.object({currencyCode: z.string().optional(), amountInMinorUnits: z.number().int()}); export const externalPayableStatusVersion4 = z.enum(["Unknown", "Open", "UserInitiationInProgress", "AwaitingMultiAuth", "BankInitiating", "WaitingForCustomer", "TransfersInProgress", "Processing", "Cancelling", "Reversing", "Processed", "Cancelled", "Reversed", "Draft"]); export const payableResourceRepresentationVersion4 = z.object({$type: z.enum(["Payable", "FeePayable"]), payableId: z.string().optional(), fundingAmount: payableFundingAmountResourceRepresentationVersion4.optional(), reference: z.string().optional(), isLowPriority: z.boolean().optional(), failureInsights: z.union([failureInsightsResolutionResourceRepresentationVersion4, failureInsightsSuggestionResourceRepresentationVersion4]).nullable().optional(), reversedAmount: payableReversedAmountResourceRepresentationVersion4.optional(), status: externalPayableStatusVersion4.optional(), paymentSourceType: paymentSourceTypeVersion4.optional(), paymentSourceId: z.string().nullable().optional()}); export const feePayableResourceRepresentationVersion4 = z.object({$type: z.enum(["Payable", "FeePayable"]), payableId: z.string().optional(), fundingAmount: payableFundingAmountResourceRepresentationVersion4.optional(), reference: z.string().optional(), isLowPriority: z.boolean().optional(), failureInsights: z.union([failureInsightsResolutionResourceRepresentationVersion4, failureInsightsSuggestionResourceRepresentationVersion4]).nullable().optional(), reversedAmount: payableReversedAmountResourceRepresentationVersion4.optional(), status: externalPayableStatusVersion4.optional(), paymentSourceType: paymentSourceTypeVersion4.optional(), paymentSourceId: z.string().nullable().optional()}); export const externalPayableGroupStatusVersion4 = z.enum(["Unknown", "Open", "Processed", "UserInitiationInProgress", "Processing", "Cancelling", "Draft"]); export const payableIdentifiersResourceRepresentationVersion4 = z.object({payableId: z.string().optional(), partnerEntityId: z.string().min(1).max(50)}); export const payableGroupResourceRepresentationVersion4 = z.object({groupId: z.string().optional(), status: externalPayableGroupStatusVersion4.optional(), partnerEntityId: z.string().min(1).max(50), partnerPayableIds: z.array(payableIdentifiersResourceRepresentationVersion4), reference: z.string().min(1).max(18)}); export const externalPayableBatchStatusVersion4 = z.enum(["Unknown", "Open", "Processed", "UserInitiationInProgress", "Processing", "Cancelling", "Draft"]); export const groupIdentifiersResourceRepresentationVersion4 = z.object({groupId: z.string(), partnerEntityId: z.string().nullable()}); export const batchResourceRepresentationVersion4 = z.object({batchId: z.string(), paymentOrder: z.number().int(), status: externalPayableBatchStatusVersion4, groups: z.array(groupIdentifiersResourceRepresentationVersion4), payables: z.array(payableIdentifiersResourceRepresentationVersion4)}); export const externalPayRunStatusVersion4 = z.enum(["Unknown", "Open", "UserInitiationInProgress", "Processing", "Cancelling", "Processed", "Concluded", "Cancelled", "Draft"]); export const payRunMetadataResourceRepresentationVersion4 = z.object({key: z.string().min(1).max(100), value: z.string().min(1).max(100)}); export const payRunResourceRepresentationVersion4 = z.object({payRunId: z.string().optional(), organisationId: z.string().optional(), partnerEntityId: z.string().min(1).max(50), scheduledExecutionDate: z.string().nullable().optional(), payerBankAccountId: z.string().nullable().optional(), paymentSourceType: paymentSourceTypeVersion4.optional(), paymentSourceId: z.string().nullable().optional(), payables: z.array(z.union([payableResourceRepresentationVersion4, feePayableResourceRepresentationVersion4])), groups: z.array(payableGroupResourceRepresentationVersion4).nullable().optional(), batches: z.array(batchResourceRepresentationVersion4).optional(), status: externalPayRunStatusVersion4.optional(), checkoutUri: z.string().optional(), metadata: z.array(payRunMetadataResourceRepresentationVersion4).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 * * GET /organisations/{organisationId}/pay-runs/{payRunId} */ export const getApiOrganisationsOrganisationIdPayRunsPayRunId = async (args: GetApiOrganisationsOrganisationIdPayRunsPayRunIdArgs, options?: GetApiOrganisationsOrganisationIdPayRunsPayRunIdOptions) => { 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))}${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(payRunResourceRepresentationVersion4, await res.json()) };