import {z} from 'zod' export type CustomInvoiceLineItem = {id?: string | undefined, description: string, valuePerQuantity?: number | undefined, quantity?: number | undefined, value?: number | undefined}; export type UpdateInvoiceRequest = {startDate?: string | undefined, endDate?: string | undefined, status?: ('DUE' | 'PAID' | 'VOID' | 'UN_COLLECTIBLE' | 'REFUND_INITIATED') | undefined, lineItems?: Array | undefined, autoAdvance?: boolean | undefined}; export type PatchApiInvoicesInvoiceIdArgs = {invoice_id: string, body: UpdateInvoiceRequest}; export type PatchApiInvoicesInvoiceIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceLineItem = z.object({id: z.string().optional(), description: z.string(), type: z.enum(["TOTAL_USAGE", "USAGE_METER_USAGE", "GRAND_TOTAL_AMOUNT", "TOTAL_AMOUNT", "NET_AMOUNT", "GROSS_AMOUNT", "SUB_TOTAL_AMOUNT", "PRICE_PLAN_AMOUNT", "FIXED_FEE_RATE_CARD_AMOUNT", "CREDIT_GRANT_RATE_CARD_AMOUNT", "BILLING_ENTITLEMENT_RATE_CARD_AMOUNT", "ENTITLEMENT_OVERAGE_RATE_CARD_AMOUNT", "ENTITLEMENT_OVERAGE_RATE_CARD_SLAB_AMOUNT", "LICENSE_RATE_CARD_AMOUNT", "USAGE_CYCLE_AMOUNT", "LICENSE_RATE_CARD_SLAB_AMOUNT", "USAGE_RATE_CARD_AMOUNT", "USAGE_RATE_CARD_SLAB_AMOUNT", "TOTAL_CREDITS", "SUB_CREDITS", "TOTAL_ADVANCED_FEES", "ADVANCED_FIXED_FEE", "ADVANCED_LICENSE_RATE_CARD_AMOUNT", "ADVANCED_BILLING_ENTITLEMENT_RATE_CARD_AMOUNT", "ADVANCED_CREDIT_GRANT_RATE_CARD_AMOUNT", "TOTAL_MISCELLANEOUS_CHARGES", "MISCELLANEOUS_CHARGE", "TRUE_UP_AMOUNT", "TOTAL_PURCHASE_AMOUNT", "CUSTOM_AMOUNT", "CUSTOM_TAG", "TOTAL_PRICING_RULE_ADDITION_AMOUNT", "PRICING_RULE_ADDITION_AMOUNT", "PRICING_RULE_REVENUE_UPDATE_AMOUNT", "PRICING_RULE_USAGE_UPDATE_AMOUNT", "RATE_CONFIG_ADJUSTMENT_AMOUNT", "TOTAL_TAX_AMOUNT", "TAX_AMOUNT", "PROXY_AMOUNT"]), valuePerQuantity: z.number().optional(), quantity: z.number().optional(), units: z.string().optional(), value: z.number(), metadata: z.record(z.string(), z.unknown()).optional()}); export const address = z.object({phoneNumber: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), postalCode: z.string().optional(), city: z.string().optional(), state: z.string().optional(), country: z.string().optional()}); export const invoiceDetails = z.object({customer: z.object({name: z.string(), primaryEmail: z.string(), billingAddress: z.string().optional(), address: address.optional()}).optional(), account: z.object({name: z.string(), invoiceCurrency: z.string(), address: address, primaryEmail: z.string()}).optional(), pricePlanName: z.string().optional(), invoiceGroup: z.object({id: z.string(), name: z.string(), email: z.string().optional(), dailyInvoiceConsolidation: z.boolean(), invoiceCurrency: z.string(), address: address}).optional(), organization: z.object({businessName: z.string(), address: address, primaryEmail: z.string(), metadata: z.record(z.string(), z.string()).optional()}).optional(), logoUrl: z.string().optional()}); export const invoicesStatus = z.enum(["DRAFT", "DUE", "PAID", "VOID", "UN_COLLECTIBLE", "REFUND_INITIATED", "REFUND_COMPLETED", "MERGED", "PARTIALLY_PAID"]); export const invoicesClass = z.enum(["INVOICE", "ORDER", "BILLABLE"]); export const invoicesType = z.enum(["STANDARD", "AD_HOC", "CUSTOM", "COMPOSITE", "STANDARD_ADVANCED", "ADHOC_ADVANCED"]); export const invoice = z.object({id: z.string().max(50), customerId: z.string().optional(), ownerId: z.string().optional(), pricePlanId: z.string().optional(), usageInfo: z.array(invoiceLineItem).optional(), revenueInfo: invoiceLineItem.optional(), invoiceDetails: invoiceDetails.optional(), status: invoicesStatus, finalizingStatus: z.enum(["FINALIZING", "FINALIZED"]).optional(), invoiceClass: invoicesClass, invoiceType: invoicesType, autoAdvance: z.boolean().optional(), startDate: z.string().optional(), endDate: z.string().optional(), endDateInclusive: z.string().optional(), invoiceDate: z.string(), dueDate: z.string().optional(), generatedAt: z.string().optional(), updatedAt: z.string(), metadata: z.record(z.string(), z.unknown()).optional(), info: z.array(z.object({type: z.string(), value: z.string()})).optional(), sequenceId: z.string().optional(), pdfUrl: z.string().optional(), totalAmount: z.number(), paidAmount: z.number(), netTermDays: z.number().int()}); 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 }; /** * Update an invoice * * PATCH /invoices/{invoice_id} */ export const patchApiInvoicesInvoiceId = async (args: PatchApiInvoicesInvoiceIdArgs, options?: PatchApiInvoicesInvoiceIdOptions) => { const {baseUrl = "https://api.togai.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/invoices/${encodeURIComponent(String(args.invoice_id))}${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(invoice, await res.json()) };