import {z} from 'zod' export type ExecuteActionRequest = {action: string}; export type CreateApiAssistedSalesV1QuotesQuoteIdExecuteActionArgs = {quoteId: string, body: ExecuteActionRequest}; export type CreateApiAssistedSalesV1QuotesQuoteIdExecuteActionOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const user = z.object({id: z.string().optional(), email: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), company: z.object({id: z.string().optional(), name: z.string().optional(), isReseller: z.boolean().optional()}).optional()}); export const currency = z.enum(["USD", "CAD", "EUR", "JPY", "GBP", "KRW", "CHF", "SEK", "SGD", "MYR", "AUD", "MXN", "INR", "BRL", "DKK", "NZD", "NOK", "ZAR", "PHP", "CNY", "SAR", "GTQ", "IDR", "ARS", "COP", "PEN"]); export const taxDetail = z.object({description: z.string().optional(), taxAmount: z.string().optional()}); export const pricingPeriod = z.enum(["ONE_TIME", "DAILY", "MONTHLY", "QUARTERLY", "SIX_MONTHS", "YEARLY", "TWO_YEARS", "THREE_YEARS"]); export const billingCycle = z.object({period: pricingPeriod.optional(), dayOfMonth: z.number().int().optional()}); export const quoteVersionTotalRecurring = z.object({billingCycle: billingCycle.optional(), totalSalePrice: z.string().optional(), nextInvoiceDate: z.string().optional()}); export const pricingTotals = z.object({amountDueBeforeTax: z.string().optional(), amountDueAfterTax: z.string().optional(), taxSummary: z.array(taxDetail).optional(), recurringTotals: z.array(quoteVersionTotalRecurring).optional()}); export const opportunityProduct = z.object({id: z.string().optional(), name: z.string().optional(), type: z.string().optional(), logo: z.string().optional()}); export const pricingPlan = z.object({id: z.string().optional(), name: z.string().optional()}); export const costTypeCategory = z.enum(["RECURRING", "ONE_TIME"]); export const costType = z.enum(["RECURRING_FLAT", "ONE_TIME_FLAT", "INCLUDED", "SETUP_PER_UNIT", "RECURRING_PER_UNIT", "ONE_TIME_PER_UNIT", "SETUP_FEE", "CONTRACT_FEE", "DISCOUNT_PER_UNIT", "DISCOUNT_FLAT", "METERED_USAGE"]); export const opportunityPricingStrategy = z.enum(["FREE", "FLAT", "UNIT", "VOLUME", "TIERED", "UNLIMITED", "CUSTOM"]); export const priceRangeItem = z.object({min: z.string().optional(), max: z.string().optional()}); export const opportunityPricing = z.object({costId: z.string().optional(), costTypeCategory: costTypeCategory, costType: costType, frequency: pricingPeriod.optional(), pricingStrategy: opportunityPricingStrategy, priceRanges: z.array(priceRangeItem).optional(), description: z.string().optional(), unit: z.string().optional(), quantity: z.string().optional(), salePrice: z.string(), wholesalePrice: z.string(), totalSalePrice: z.string()}); export const item = z.object({id: z.string().optional(), parentItemId: z.string().optional(), product: opportunityProduct.optional(), pricingPlan: pricingPlan.optional(), pricing: z.array(opportunityPricing).optional()}); export const quoteVersionSourceType = z.enum(["OPPORTUNITY", "QUOTE_V2", "QUOTE_V1"]); export const quoteVersionSource = z.object({id: z.string(), type: quoteVersionSourceType}); export const quoteVersion = z.object({id: z.string().optional(), displayId: z.string().optional(), name: z.string().optional(), createdOn: z.number().optional(), createdBy: user.optional(), status: z.string().optional(), ownerUser: user.optional(), customerUser: user.optional(), currency: currency.optional(), pricingTotals: pricingTotals.optional(), items: z.array(item).optional(), quoteSource: quoteVersionSource.optional(), availableActions: z.array(z.string()).optional()}); export const executeActionResponse = z.object({quote: quoteVersion.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 }; /** * Execute action on a quote version * * POST /assistedSales/v1/quotes/{quoteId}/executeAction */ export const createApiAssistedSalesV1QuotesQuoteIdExecuteAction = async (args: CreateApiAssistedSalesV1QuotesQuoteIdExecuteActionArgs, options?: CreateApiAssistedSalesV1QuotesQuoteIdExecuteActionOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/assistedSales/v1/quotes/${encodeURIComponent(String(args.quoteId))}/executeAction${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(executeActionResponse, await res.json()) };