import {z} from 'zod' export type CreateApiApiInvoicesTransTransactionIdArgs = {transactionId: string}; export type CreateApiApiInvoicesTransTransactionIdOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const legalIdentificationDocumentResource = z.union([z.object({type: z.literal("france_siren"), identifier: z.string()}), z.object({type: z.enum(["spain_tax_id", "origin_country_id"])})]); export const invoiceResource = z.object({_id: z.string(), type: z.enum(["invoice", "cancellation"]).optional(), sellerId: z.string(), recipient: z.object({company: z.string().optional(), name: z.string(), firstname: z.string().optional(), lastname: z.string().optional(), address: z.object({fullAddress: z.string().nullable().optional(), street: z.string().nullable().optional(), line2: z.string().nullable().optional(), postal: z.string().nullable().optional(), city: z.string().nullable().optional(), country: z.string().nullable().optional(), state: z.string().nullable().optional()}).optional()}).optional(), transactionId: z.string().optional(), customerId: z.string().optional(), cancellationId: z.string().optional(), cancellationReason: z.enum(["REQUESTED_BY_CUSTOMER", "CLAIM", "COMMUNICATION_ERROR", "TRAINING/TEST"]).optional(), refundTarget: z.enum(["origin", "voucher", "customer_balance"]).optional(), counter: z.number(), no: z.string(), items: z.array(z.object({type: z.enum(["product", "ticket", "fulfillment", "outerCharge", "discount", "voucher", "bundle", "additionalFee", "insurance"]).optional(), title: z.string().optional(), amount: z.number(), total: z.number(), regularTotal: z.number(), includedTax: z.number(), taxRate: z.number().gte(0), taxRates: z.array(z.number()).optional(), taxItems: z.array(z.object({name: z.string().nullable().optional(), taxTypeId: z.string().nullable().optional(), rate: z.number(), perUnit: z.number(), total: z.number(), netTotal: z.number().nullable().optional(), netPerUnit: z.number().nullable().optional(), taxPayableAccountId: z.string().nullable().optional(), taxPayableAccountCode: z.string().nullable().optional()})).optional(), localization: z.object({}).optional()})), components: z.array(z.object({name: z.string().optional(), total: z.number().optional()})).optional(), total: z.number().optional(), discountSum: z.number().optional(), detailsPerRate: z.array(z.object({taxRate: z.number().gte(0), includedTax: z.number(), reducedTax: z.number()})).optional(), taxItems: z.array(z.object({name: z.string().nullable().optional(), taxTypeId: z.string().nullable().optional(), rate: z.number(), perUnit: z.number(), total: z.number(), netTotal: z.number().nullable().optional(), netPerUnit: z.number().nullable().optional(), taxPayableAccountId: z.string().nullable().optional(), taxPayableAccountCode: z.string().nullable().optional()})).optional(), includedTax: z.number().optional(), taxRate: z.number().gte(0).optional(), includedOuterChargeTax: z.number().optional(), outerChargeTaxRate: z.number().gte(0).optional(), origin: z.enum(["onlinesale", "pos"]).optional(), currency: z.enum(["EUR", "USD", "GBP", "AUD", "CHF", "THB", "ILS", "COP", "MXN", "DKK", "NOK", "SEK", "QAR", "CAD", "ISK", "GTQ", "INR", "DOP", "SGD", "PLN", "SAR", "TTD", "ZAR", "KYD", "HKD", "CZK", "KRW", "JPY", "NZD", "AED", "MAD", "TWD", "BRL", "BWP", "NAD", "KES", "SCR", "TRY", "SZL", "LSL", "TZS", "UGX", "ZMW", "ZWG", "GHS", "NGN", "SLE", "LRD", "XOF", "XAF", "GEL", "IDR", "ARS", "CRC", "HUF", "EGP", "MYR", "VND", "PHP", "MWK"]), extraFields: z.object({}).optional(), identification: z.array(legalIdentificationDocumentResource).optional(), splits: z.array(z.object({type: z.enum(["deposit", "voucher", "payment", "balance"]), reference: z.string().optional(), amount: z.number(), voucherId: z.string().optional(), method: z.string().optional(), gateway: z.string().optional(), gatewayId: z.string().optional()})).optional(), seller: z.object({taxId: z.string().optional(), name: z.string().optional(), address: z.object({fullAddress: z.string().nullable().optional(), street: z.string().nullable().optional(), line2: z.string().nullable().optional(), postal: z.string().nullable().optional(), city: z.string().nullable().optional(), country: z.string().nullable().optional(), state: z.string().nullable().optional()}).optional()}).optional(), posInfo: z.object({posId: z.string(), no: z.string(), referenceInvoiceNo: z.string().optional(), sessionId: z.string(), billingNo: z.string(), taxId: z.string().optional(), paymentMethod: z.string().optional(), canceledAt: z.string().optional(), cancellationNo: z.string().optional()}).optional(), external: z.object({documentUrl: z.string().optional(), additionalDocumentUrl: z.string().optional()}).optional(), secret: z.string().optional(), referenceInvoiceId: z.string().optional(), footerText: z.array(z.string()).optional(), createdAt: z.string().optional(), updatedAt: z.string().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 }; /** * Create Invoice for Transaction (deprecated - use POST /invoices with query parameter transactionId instead) * * POST /api/invoices/trans/{transactionId} */ export const createApiApiInvoicesTransTransactionId = async (args: CreateApiApiInvoicesTransTransactionIdArgs, options?: CreateApiApiInvoicesTransTransactionIdOptions) => { const {baseUrl = "https://vivenu.com", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/invoices/trans/${encodeURIComponent(String(args.transactionId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(invoiceResource, await res.json()) };