import {z} from 'zod' export type LeadCustomerDetails = {description?: string | undefined, lastName: string, firstName: string, salutation?: string | undefined, middleName?: string | undefined, company: string, street?: string | undefined, city?: string | undefined, state?: string | undefined, postalCode?: string | undefined, country?: string | undefined, latitude?: string | undefined, longitude?: string | undefined, phone?: string | undefined, mobileNumber: string, email: string, website?: string | undefined, industry?: string | undefined, annualRevenue?: number | undefined, numberofEmployees?: number | undefined, businessOrganizationType?: string | undefined, companyRegistration?: string | undefined, companySize?: string | undefined, preferredCommunication?: string | undefined, domainName?: string | undefined, externalChannel?: string | undefined, notes?: string | undefined, preferredContactSlot?: string | undefined, feasibilityDetails?: string | undefined, feasibilityStatus?: string | undefined, utmSource?: string | undefined, networkFeasibility?: string | undefined, solutionType?: string | undefined, sales?: string | undefined, segment?: string | undefined, region?: string | undefined, opco: string, id?: string | undefined, idType?: string | undefined}; export type LeadProductDetails = {productServiceDescription?: string | undefined, serviceRequiredPeriod?: string | undefined, typeofProduct?: string | undefined, productCode?: string | undefined}; export type LeadCampaignDetails = {campaignName?: string | undefined, campaignType?: string | undefined, campaignOwner?: string | undefined, campaignPromotionId?: string | undefined}; export type LeadLeadDetails = {leadSource?: string | undefined, leadOrigin?: string | undefined, interactionReason?: string | undefined, leadReference?: string | undefined, subSegment?: string | undefined, partnerName?: string | undefined, createdDate?: string | undefined}; export type SalesLeadCreate = {customerDetails: LeadCustomerDetails, productDetails?: LeadProductDetails | undefined, campaignDetails?: LeadCampaignDetails | undefined, leadDetails?: LeadLeadDetails | undefined}; export type CreateApiLeadArgs = {transactionId?: string | undefined, 'x-origin-channelId'?: string | undefined, body: SalesLeadCreate}; export type CreateApiLeadOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const createSalesLeadResponse = z.object({statusCode: z.string().optional(), statusMessage: z.string().optional(), supportMessage: z.string().optional(), transactionId: z.string().optional(), id: z.string().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 }; /** * Creates a SalesLead * * POST /lead */ export const createApiLead = async (args: CreateApiLeadArgs, options?: CreateApiLeadOptions) => { const {baseUrl = "https://za.api.mtn.com/sales/v1", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json;charset=utf-8") headers.set('accept', "application/json;charset=utf-8") if (args.transactionId != null) headers.set("transactionId", String(args.transactionId)) if (args['x-origin-channelId'] != null) headers.set("x-origin-channelId", String(args['x-origin-channelId'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/lead${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(createSalesLeadResponse, await res.json()) };