import {z} from 'zod' export type Currency = {code: unknown, decimals?: number | undefined, enabled?: boolean | undefined, id?: number | undefined, name: unknown, symbol?: unknown | undefined}; export type UserSimple = {'avatar-thumb-url'?: unknown | undefined, 'created-at'?: unknown | undefined, email: unknown, 'employee-number'?: unknown | undefined, firstname: unknown, fullname?: unknown | undefined, id?: number | undefined, lastname: unknown, login: unknown, 'salesforce-id': unknown, 'updated-at'?: unknown | undefined}; export type BusinessGroup = {'created-at'?: unknown | undefined, 'created-by'?: UserSimple | undefined, description?: unknown | undefined, id?: number | undefined, name: unknown, 'updated-at'?: unknown | undefined, 'updated-by'?: UserSimple | undefined}; export type PaymentTerm = {active?: boolean | undefined, 'active?'?: unknown | undefined, code: unknown, 'content-groups'?: Array | undefined, 'created-at'?: unknown | undefined, 'created-by'?: UserSimple | undefined, 'days-for-discount-payment': number, 'days-for-net-payment': number, description?: unknown | undefined, 'discount-cutoff-day'?: number | undefined, 'discount-due-day': number, 'discount-due-month': number, 'discount-rate'?: number | undefined, id?: number | undefined, 'net-cutoff-day'?: number | undefined, 'net-due-day': number, 'net-due-month': number, type: 'DaysAfterNetPaymentTerm' | 'SpecificDayPaymentTerm' | 'BaseEomToDaysPaymentTerm' | 'DaysToBaseEomPaymentTerm', 'updated-at'?: unknown | undefined, 'updated-by'?: UserSimple | undefined}; export type PayablesAllocation = {'bypass-strict-validations'?: boolean | undefined, 'created-at'?: unknown | undefined, 'created-by'?: UserSimple | undefined, 'exported?'?: boolean | undefined, id?: number | undefined, 'last-exported-at'?: unknown | undefined, 'payable-from-amount': number, 'payable-from-currency': Currency, 'payable-from-id'?: number | undefined, 'payable-from-type'?: unknown | undefined, 'payable-to-amount': number, 'payable-to-currency': Currency, 'payable-to-id'?: number | undefined, 'payable-to-type'?: unknown | undefined, 'payment-reference-id'?: number | undefined, 'payment-reference-type'?: unknown | undefined, 'reason-code'?: ('payment' | 'auto_payment' | 'epr' | 'epr_rejected' | 'manual') | undefined, source?: ('coupa_pay' | 'erp' | 'ui' | 'api' | 'csv' | 'early_pay_discount') | undefined, 'source-transaction-from-id'?: unknown | undefined, 'source-transaction-from-reference'?: unknown | undefined, 'source-transaction-from-type'?: unknown | undefined, 'source-transaction-to-id'?: unknown | undefined, 'source-transaction-to-reference'?: unknown | undefined, 'source-transaction-to-type'?: unknown | undefined, status?: unknown | undefined, 'updated-at'?: unknown | undefined, 'updated-by'?: UserSimple | undefined}; export type InvoiceReconciliationLine = {'adjustment-date'?: unknown | undefined, amount?: number | undefined, 'amount-paid'?: number | undefined, category?: unknown | undefined, 'created-at'?: unknown | undefined, 'created-by'?: UserSimple | undefined, id?: number | undefined, note?: unknown | undefined, notes?: unknown | undefined, 'payable-id'?: number | undefined, 'payable-type'?: unknown | undefined, 'payment-date'?: unknown | undefined, 'updated-at'?: unknown | undefined, 'updated-by'?: UserSimple | undefined}; export type PayablesInvoice = {'accounting-amount'?: number | undefined, 'accounting-currency'?: Currency | undefined, 'created-at'?: unknown | undefined, currency?: Currency | undefined, 'discount-due-date'?: unknown | undefined, 'document-id'?: unknown | undefined, 'document-type'?: unknown | undefined, 'exported-to-tradefin-at'?: unknown | undefined, id?: number | undefined, 'last-exported-at'?: unknown | undefined, 'maturity-date'?: unknown | undefined, 'paid-amount'?: number | undefined, 'paid-date'?: unknown | undefined, 'payable-amount'?: number | undefined, 'payment-term'?: PaymentTerm | undefined, 'received-allocations'?: Array | undefined, 'reconciliation-lines'?: Array | undefined, 'remaining-amount'?: number | undefined, 'remittance-amount'?: number | undefined, 'reporting-total'?: number | undefined, 'sent-allocations'?: Array | undefined, status?: unknown | undefined, 'updated-at'?: unknown | undefined, 'wc-eligibility'?: unknown | undefined}; export type PatchApiPayablesInvoicesIdTrackInCoupaArgs = {id: number, return_object?: ('limited' | 'shallow' | 'none') | undefined, filter?: string | undefined, body: PayablesInvoice}; export type PatchApiPayablesInvoicesIdTrackInCoupaOptions = {baseUrl?: string | undefined, OAuth2AccessCode?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.object({code: z.string(), decimals: z.number().int().optional(), enabled: z.boolean().optional(), id: z.number().int().optional(), name: z.string(), symbol: z.string().optional()}); export const userSimple = z.object({"avatar-thumb-url": z.string().optional(), "created-at": z.string().optional(), email: z.string(), "employee-number": z.string().optional(), firstname: z.string(), fullname: z.string().optional(), id: z.number().int().optional(), lastname: z.string(), login: z.string(), "salesforce-id": z.string(), "updated-at": z.string().optional()}); export const businessGroup = z.object({"created-at": z.string().optional(), "created-by": userSimple.optional(), description: z.string().optional(), id: z.number().int().optional(), name: z.string(), "updated-at": z.string().optional(), "updated-by": userSimple.optional()}); export const paymentTerm = z.object({active: z.boolean().optional(), "active?": z.string().optional(), code: z.string(), "content-groups": z.array(businessGroup).optional(), "created-at": z.string().optional(), "created-by": userSimple.optional(), "days-for-discount-payment": z.number().int(), "days-for-net-payment": z.number().int(), description: z.string().optional(), "discount-cutoff-day": z.number().int().optional(), "discount-due-day": z.number().int(), "discount-due-month": z.number().int(), "discount-rate": z.number().optional(), id: z.number().int().optional(), "net-cutoff-day": z.number().int().optional(), "net-due-day": z.number().int(), "net-due-month": z.number().int(), type: z.enum(["DaysAfterNetPaymentTerm", "SpecificDayPaymentTerm", "BaseEomToDaysPaymentTerm", "DaysToBaseEomPaymentTerm"]), "updated-at": z.string().optional(), "updated-by": userSimple.optional()}); export const payablesAllocation = z.object({"bypass-strict-validations": z.boolean().optional(), "created-at": z.string().optional(), "created-by": userSimple.optional(), "exported?": z.boolean().optional(), id: z.number().int().optional(), "last-exported-at": z.string().optional(), "payable-from-amount": z.number(), "payable-from-currency": currency, "payable-from-id": z.number().int().optional(), "payable-from-type": z.string().optional(), "payable-to-amount": z.number(), "payable-to-currency": currency, "payable-to-id": z.number().int().optional(), "payable-to-type": z.string().optional(), "payment-reference-id": z.number().int().optional(), "payment-reference-type": z.string().optional(), "reason-code": z.enum(["payment", "auto_payment", "epr", "epr_rejected", "manual"]).optional(), source: z.enum(["coupa_pay", "erp", "ui", "api", "csv", "early_pay_discount"]).optional(), "source-transaction-from-id": z.string().optional(), "source-transaction-from-reference": z.string().optional(), "source-transaction-from-type": z.string().optional(), "source-transaction-to-id": z.string().optional(), "source-transaction-to-reference": z.string().optional(), "source-transaction-to-type": z.string().optional(), status: z.string().optional(), "updated-at": z.string().optional(), "updated-by": userSimple.optional()}); export const invoiceReconciliationLine = z.object({"adjustment-date": z.string().optional(), amount: z.number().optional(), "amount-paid": z.number().optional(), category: z.string().optional(), "created-at": z.string().optional(), "created-by": userSimple.optional(), id: z.number().int().optional(), note: z.string().optional(), notes: z.string().optional(), "payable-id": z.number().int().optional(), "payable-type": z.string().optional(), "payment-date": z.string().optional(), "updated-at": z.string().optional(), "updated-by": userSimple.optional()}); export const payablesInvoice = z.object({"accounting-amount": z.number().optional(), "accounting-currency": currency.optional(), "created-at": z.string().optional(), currency: currency.optional(), "discount-due-date": z.string().optional(), "document-id": z.string().optional(), "document-type": z.string().optional(), "exported-to-tradefin-at": z.string().optional(), id: z.number().int().optional(), "last-exported-at": z.string().optional(), "maturity-date": z.string().optional(), "paid-amount": z.number().optional(), "paid-date": z.string().optional(), "payable-amount": z.number().optional(), "payment-term": paymentTerm.optional(), "received-allocations": z.array(payablesAllocation).optional(), "reconciliation-lines": z.array(invoiceReconciliationLine).optional(), "remaining-amount": z.number().optional(), "remittance-amount": z.number().optional(), "reporting-total": z.number().optional(), "sent-allocations": z.array(payablesAllocation).optional(), status: z.string().optional(), "updated-at": z.string().optional(), "wc-eligibility": z.string().optional()}); export const patchApiPayablesInvoicesIdTrackInCoupaResponse = z.array(payablesInvoice); 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 }; /** * Start Tracking Invoice Payable in Coupa * * PATCH /payables/invoices/{id}/track_in_coupa * * Also accepts auth: OAuth2ClientCredential */ export const patchApiPayablesInvoicesIdTrackInCoupa = async (args: PatchApiPayablesInvoicesIdTrackInCoupaArgs, options?: PatchApiPayablesInvoicesIdTrackInCoupaOptions) => { const {baseUrl = "https://r38.coupadev.com/api", fetch: fetchFn = fetch, OAuth2AccessCode} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.return_object != null) query.append("return_object", String(args.return_object)) if (args.filter != null) query.append("filter", String(args.filter)) if (OAuth2AccessCode !== undefined) headers.set('authorization', `Bearer ${OAuth2AccessCode}`) const url = `${baseUrl.replace(/\/+$/, '')}/payables/invoices/${encodeURIComponent(String(args.id))}/track_in_coupa${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiPayablesInvoicesIdTrackInCoupaResponse, await res.json()) };