import {z} from 'zod' export type GetApiV3ContractsIdBillingTermsArgs = {id: string, billingTermGroupId?: string | undefined, page: number, limit: number}; export type GetApiV3ContractsIdBillingTermsOptions = {baseUrl?: string | undefined, customHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const pricingTierDto = z.object({tier: z.number(), amount: z.number(), amountType: z.enum(["PER_ITEM", "TOTAL_INVOICE"]), tierMinimum: z.number()}); export const discountDto = z.object({type: z.enum(["PERCENTAGE", "FIXED"]).optional(), amount: z.string().optional(), note: z.string().optional()}); export const getBillingTermItemDto = z.object({billingTermId: z.string(), contractId: z.string(), name: z.string(), description: z.string().nullable(), itemId: z.string().nullable(), productId: z.string().nullable(), billingStartDate: z.string(), billingEndDate: z.string(), quantity: z.number(), duration: z.number(), intervalFrequency: z.number(), interval: z.enum(["NONE", "DAY", "MONTH", "HOUR", "YEAR", "QUARTER", "SEMI_MONTH", "WEEK"]).nullable(), invoiceDateStrategy: z.enum(["FIRST_OF_PERIOD", "LAST_OF_PERIOD", "ARREARS", "ADVANCED_DUE_START"]), netPaymentTerms: z.number().nullable(), billingType: z.enum(["UNIT", "FLAT"]), pricingType: z.enum(["SIMPLE", "TIERED", "VOLUME"]), isRecurring: z.boolean(), includeInArr: z.boolean(), isArrears: z.boolean(), eventTypeId: z.string().nullable(), classId: z.string().nullable(), departmentId: z.string().nullable(), projectId: z.string().nullable(), pricing: z.array(pricingTierDto), billingTermGroupId: z.string().nullable(), commitmentId: z.string().nullable(), commitmentBillingTermType: z.string().nullable(), discounts: z.array(discountDto)}); export const integratorsApiError = z.object({code: z.number(), message: z.string(), details: z.object({}).optional()}); export const getApiV3ContractsIdBillingTermsResponse = z.object({payload: z.object({data: z.array(getBillingTermItemDto).optional(), limit: z.number().optional(), totalItems: z.number().optional(), currentPage: z.number().optional()}), success: z.boolean(), message: z.string(), error: integratorsApiError}); 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 }; /** * List contract billing terms * * GET /v3/contracts/{id}/billing-terms */ export const getApiV3ContractsIdBillingTerms = async (args: GetApiV3ContractsIdBillingTermsArgs, options?: GetApiV3ContractsIdBillingTermsOptions) => { const {baseUrl = "https://integrators.prod.api.tabsplatform.com", fetch: fetchFn = fetch, customHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.billingTermGroupId != null) query.append("billingTermGroupId", String(args.billingTermGroupId)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (customHeader !== undefined) headers.set("Authorization", customHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v3/contracts/${encodeURIComponent(String(args.id))}/billing-terms${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(getApiV3ContractsIdBillingTermsResponse, await res.json()) };