import {z} from 'zod' export type PricingTierDto = {tier: number, amount: number, amountType: 'PER_ITEM' | 'TOTAL_INVOICE', tierMinimum: number}; export type CreateBillingScheduleDto = {description?: string | undefined, startDate: string, duration?: number | undefined, isArrears?: boolean | undefined, invoiceDateStrategy?: ('FIRST_OF_PERIOD' | 'LAST_OF_PERIOD' | 'ARREARS' | 'ADVANCED_DUE_START') | undefined, isRecurring: boolean, includeInArr?: boolean | undefined, interval: 'NONE' | 'DAY' | 'MONTH' | 'HOUR' | 'YEAR' | 'QUARTER' | 'SEMI_MONTH' | 'WEEK', intervalFrequency: number, netPaymentTerms: number, quantity: number, billingType: 'UNIT' | 'FLAT', pricingType: 'SIMPLE' | 'TIERED' | 'VOLUME', eventTypeId?: string | undefined, itemId?: string | undefined, productId?: (string | null) | undefined, invoiceType: 'INVOICE' | 'BILL', pricing: Array, classId?: string | undefined, departmentId?: string | undefined, projectId?: string | undefined, name?: string | undefined}; export type DiscountDto = {type?: ('PERCENTAGE' | 'FIXED') | undefined, amount?: string | undefined, note?: string | undefined}; export type SeatBasedObligationConfigDto = {isMovingMinimum: boolean, minQuantity: number, defaultProrationPolicy: 'NONE' | 'DAY' | 'MONTH' | 'YEAR', defaultTrueUpTiming: 'IMMEDIATE' | 'CO_TERM'}; export type CreateObligationDto = {serviceStartDate: string, serviceEndDate: string, categoryId?: string | undefined, billingSchedule: CreateBillingScheduleDto, discount?: DiscountDto | undefined, seatConfig?: SeatBasedObligationConfigDto | undefined}; export type CreateApiV3ContractsIdObligationsArgs = {id: string, body: CreateObligationDto}; export type CreateApiV3ContractsIdObligationsOptions = {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 billingScheduleDto = z.object({name: z.string(), description: z.string().optional(), startDate: z.string(), endDate: z.string(), duration: z.number().optional(), isArrears: z.boolean().optional(), invoiceDateStrategy: z.enum(["FIRST_OF_PERIOD", "LAST_OF_PERIOD", "ARREARS", "ADVANCED_DUE_START"]).optional(), isRecurring: z.boolean(), includeInArr: z.boolean().optional(), interval: z.enum(["NONE", "DAY", "MONTH", "HOUR", "YEAR", "QUARTER", "SEMI_MONTH", "WEEK"]), intervalFrequency: z.number(), netPaymentTerms: z.number(), quantity: z.number(), billingType: z.enum(["UNIT", "FLAT"]), pricingType: z.enum(["SIMPLE", "TIERED", "VOLUME"]), eventTypeId: z.string().optional(), itemId: z.string().optional(), productId: z.string().nullable().optional(), invoiceType: z.enum(["INVOICE", "BILL"]), pricing: z.array(pricingTierDto), classId: z.string().optional(), departmentId: z.string().optional(), projectId: z.string().optional()}); export const recognizedRevenueDto = z.object({timeframe: z.string(), total: z.number(), booksClosed: z.boolean()}); export const revenueScheduleDto = z.object({categoryId: z.string().optional(), recognizedRevenue: z.array(recognizedRevenueDto)}); export const discountDto = z.object({type: z.enum(["PERCENTAGE", "FIXED"]).optional(), amount: z.string().optional(), note: z.string().optional()}); export const seatBasedObligationConfigDto = z.object({isMovingMinimum: z.boolean(), minQuantity: z.number(), defaultProrationPolicy: z.enum(["NONE", "DAY", "MONTH", "YEAR"]), defaultTrueUpTiming: z.enum(["IMMEDIATE", "CO_TERM"])}); export const obligationDto = z.object({id: z.string(), contractId: z.string(), serviceStartDate: z.string(), serviceEndDate: z.string(), categoryId: z.string().optional(), billingSchedule: billingScheduleDto, revenueSchedule: revenueScheduleDto, discount: discountDto.optional(), seatConfig: seatBasedObligationConfigDto.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 contract obligation * * POST /v3/contracts/{id}/obligations */ export const createApiV3ContractsIdObligations = async (args: CreateApiV3ContractsIdObligationsArgs, options?: CreateApiV3ContractsIdObligationsOptions) => { const {baseUrl = "https://integrators.prod.api.tabsplatform.com", fetch: fetchFn = fetch, customHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (customHeader !== undefined) headers.set("Authorization", customHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v3/contracts/${encodeURIComponent(String(args.id))}/obligations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(obligationDto, await res.json()) };