import {z} from 'zod' export type LeadType = 'MANUAL' | 'PRODUCT' | 'COMPANY'; export type LeadCustomerCompany = {name?: string | undefined, street1?: string | undefined, street2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, country?: string | undefined, size?: ('RANGE_1_9' | 'RANGE_10_24' | 'RANGE_25_49' | 'RANGE_50_249' | 'RANGE_250_499' | 'RANGE_500_999' | 'RANGE_1000_4999' | 'RANGE_5000_PLUS' | 'UNKNOWN') | undefined}; export type LeadCustomerContact = {firstName?: string | undefined, lastName?: string | undefined, email?: string | undefined, phone?: string | undefined, phoneExtension?: string | undefined}; export type LeadCustomer = {company: LeadCustomerCompany, contact: LeadCustomerContact}; export type LeadUser = {id: string, companyId: string, companyName?: string | undefined, role?: string | undefined}; export type LeadCompany = {id: string, name?: string | undefined}; export type LeadProduct = {id: string, name: string, provider?: string | undefined}; export type LeadProductEdition = {id: string, name: string}; export type LeadCreationRequest = {partner: string, leadType: LeadType, customer: LeadCustomer, customAttributes?: Record | undefined, reseller?: LeadUser | undefined, notes?: string | undefined, creator?: LeadUser | undefined, profileCompany?: LeadCompany | undefined, profileProduct?: LeadProduct | undefined, profileProductEdition?: LeadProductEdition | undefined}; export type CreateApiLeadV2LeadsArgs = {context: 'RESELLER' | 'REFERRAL' | 'DEVELOPER' | 'RESELLER_MANAGER' | 'PARTNER' | 'ANONYMOUS' | 'CHANNEL_ADMIN', companyId: string, userId: string, body: LeadCreationRequest}; export type CreateApiLeadV2LeadsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const leadType = z.enum(["MANUAL", "PRODUCT", "COMPANY"]); export const leadStatus = z.enum(["CONVERTED", "DISQUALIFIED", "DELETED", "PENDING_CONVERSION_APPROVAL", "CONVERSION_APPROVED", "PENDING_SUBMISSION", "SUBMITTED", "ASSIGNED", "LEGACY", "PENDING_FOR_MANAGERS", "ACCEPTED_FOR_MANAGERS"]); export const leadCustomerCompany = z.object({name: z.string().optional(), street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional(), country: z.string().optional(), size: z.enum(["RANGE_1_9", "RANGE_10_24", "RANGE_25_49", "RANGE_50_249", "RANGE_250_499", "RANGE_500_999", "RANGE_1000_4999", "RANGE_5000_PLUS", "UNKNOWN"]).optional()}); export const leadCustomerContact = z.object({firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), phoneExtension: z.string().optional()}); export const leadCustomer = z.object({company: leadCustomerCompany, contact: leadCustomerContact}); export const leadParticipant = z.object({participantType: z.string(), partner: z.string().optional(), companyId: z.string().optional(), companyName: z.string().optional()}); export const leadUser = z.object({id: z.string(), companyId: z.string(), companyName: z.string().optional(), role: z.string().optional()}); export const leadCompany = z.object({id: z.string(), name: z.string().optional()}); export const leadResult = z.enum(["OPEN", "LOST", "WON"]); export const leadProduct = z.object({id: z.string(), name: z.string(), provider: z.string().optional()}); export const leadProductEdition = z.object({id: z.string(), name: z.string()}); export const leadResponse = z.object({id: z.string(), partner: z.string(), leadType: leadType, status: leadStatus, customer: leadCustomer, createdOn: z.number(), lastModified: z.number(), customAttributes: z.object({}).optional(), referrer: leadParticipant, recipient: leadParticipant, reseller: leadUser.optional(), notes: z.string().optional(), creator: leadUser.optional(), convertedUser: leadUser.optional(), convertedCompany: leadCompany.optional(), result: leadResult.optional(), profileCompany: leadCompany.optional(), profileProduct: leadProduct.optional(), profileProductEdition: leadProductEdition.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 lead * * POST /lead/v2/leads */ export const createApiLeadV2Leads = async (args: CreateApiLeadV2LeadsArgs, options?: CreateApiLeadV2LeadsOptions) => { 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") if (args.context != null) query.append("context", String(args.context)) if (args.companyId != null) query.append("companyId", String(args.companyId)) if (args.userId != null) query.append("userId", String(args.userId)) const url = `${baseUrl.replace(/\/+$/, '')}/lead/v2/leads${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(leadResponse, await res.json()) };