import {z} from 'zod' export type GetApiV3ReconciliationLedgerLinesArgs = {context: 'RESELLER_MANAGER', page?: number | undefined, size?: number | undefined, sortOrder?: ('ASC' | 'DESC') | undefined, fromDate?: number | undefined, toDate?: number | undefined, reconciliationType?: ('ITEM' | 'TAX' | 'DISCOUNT' | 'TERMINATION_FEE' | 'ADJUSTMENT' | 'RESELLER_MARKUP' | 'CHANNEL_MARKUP' | 'APPDIRECT_FEE') | undefined, operationType?: ('CREDIT' | 'DEBIT') | undefined}; export type GetApiV3ReconciliationLedgerLinesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), rel: z.string().optional()}).nullable(); export const pageMetadata = z.object({number: z.number(), size: z.number(), totalElements: z.number(), totalPages: z.number()}); export const currency = z.enum(["USD", "CAD", "EUR", "JPY", "GBP", "KRW", "CHF", "SEK", "SGD", "MYR", "AUD", "MXN", "INR", "BRL", "DKK", "NZD", "NOK", "ZAR", "PHP", "CNY", "SAR", "GTQ", "IDR", "ARS", "COP", "PEN"]); export const customerWS = z.object({id: z.number().nullable().optional(), uuid: z.string().nullable().optional(), username: z.string().nullable().optional(), email: z.string().nullable().optional()}).nullable(); export const partyWS = z.object({id: z.string().nullable().optional(), uuid: z.string().nullable().optional(), type: z.enum(["CUSTOMER", "RESELLER", "CHANNEL", "VENDOR", "APPDIRECT"]).nullable().optional(), name: z.string().nullable().optional(), email: z.string().nullable().optional()}).nullable(); export const ledgerLineOperationType = z.enum(["CREDIT", "DEBIT"]); export const transferStatus = z.enum(["OPEN", "PENDING", "RECONCILED"]); export const reconciliationType = z.enum(["ITEM", "TAX", "DISCOUNT", "TERMINATION_FEE", "ADJUSTMENT", "RESELLER_MARKUP", "CHANNEL_MARKUP", "APPDIRECT_FEE"]); export const paymentMethod = z.enum(["CREDIT_CARD", "PAYPAL", "MANUAL", "EXTERNAL_INVOICE", "MARKETPLACE_CREDITS"]); export const paymentIdAndTypeWS = z.object({id: z.number().nullable().optional(), type: paymentMethod.optional()}).nullable(); export const invoiceLineTypeWS = z.enum(["ITEM", "DISCOUNT", "TAX", "DELEGATED_INVOICE", "TERMINATION_FEE", "SUB_ACCOUNT"]).nullable(); export const ledgerLineDetailsWS = z.object({id: z.number().nullable().optional(), currency: currency.optional(), amount: z.number().nullable().optional(), customer: customerWS.optional(), issuerParty: partyWS.optional(), recipientParty: partyWS.optional(), type: ledgerLineOperationType.optional(), transferId: z.number().nullable().optional(), transferStatus: transferStatus.optional(), reconciliationType: reconciliationType.optional(), reconciliationDate: z.number().nullable().optional(), createdOn: z.number().nullable().optional(), lastModified: z.number().nullable().optional(), payments: z.array(paymentIdAndTypeWS).optional(), invoiceId: z.number().nullable().optional(), invoiceLineId: z.number().nullable().optional(), orderId: z.number().nullable().optional(), productId: z.number().nullable().optional(), productName: z.string().nullable().optional(), editionId: z.number().nullable().optional(), editionName: z.string().nullable().optional(), editionPricingItemId: z.number().nullable().optional(), lineItemDescription: z.string().nullable().optional(), lineItemType: invoiceLineTypeWS.optional()}).nullable(); export const pagedLedgerLineDetailsWS = z.object({links: z.array(link).optional(), page: pageMetadata.optional(), content: z.array(ledgerLineDetailsWS).optional()}).nullable(); 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 }; /** * Retrieve ledger lines * * GET /v3/reconciliation/ledgerLines */ export const getApiV3ReconciliationLedgerLines = async (args: GetApiV3ReconciliationLedgerLinesArgs, options?: GetApiV3ReconciliationLedgerLinesOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.context != null) query.append("context", String(args.context)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.reconciliationType != null) query.append("reconciliationType", String(args.reconciliationType)) if (args.operationType != null) query.append("operationType", String(args.operationType)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/reconciliation/ledgerLines${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(pagedLedgerLineDetailsWS, await res.json()) };