import {z} from 'zod' export type GetApiAssistedSalesV1OpportunitiesArgs = {ownerId?: string | undefined, ownerCompanyId?: string | undefined, customerId?: string | undefined, customerCompanyId?: string | undefined, context?: ('RESELLER' | 'SSR') | undefined, status?: ('OPEN' | 'CLOSED' | 'PENDING_REVIEW') | undefined, purchaseEffectiveDate?: Array | undefined, createdOn?: Array | undefined, searchText?: string | undefined, number?: number | undefined, size?: number | undefined, sort?: string | undefined}; export type GetApiAssistedSalesV1OpportunitiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pageMetadata = z.object({number: z.number(), size: z.number(), totalElements: z.number(), totalPages: z.number()}); 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 opportunityStatus = z.enum(["OPEN", "CLOSED", "PENDING_REVIEW"]); export const opportunityOwnerType = z.enum(["PARTNER", "PLATFORM_OWNER"]); export const opportunityLight = z.object({id: z.string().optional(), name: z.string().optional(), ownerUser: user.optional(), customerUser: user.optional(), createdOn: z.number().optional(), purchaseEffectiveDate: z.number().optional(), status: opportunityStatus.optional(), ownerType: opportunityOwnerType.optional()}); export const opportunityPage = z.object({page: pageMetadata.optional(), content: z.array(opportunityLight).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 }; /** * List opportunities * * GET /assistedSales/v1/opportunities */ export const getApiAssistedSalesV1Opportunities = async (args: GetApiAssistedSalesV1OpportunitiesArgs, options?: GetApiAssistedSalesV1OpportunitiesOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ownerId != null) query.append("ownerId", String(args.ownerId)) if (args.ownerCompanyId != null) query.append("ownerCompanyId", String(args.ownerCompanyId)) if (args.customerId != null) query.append("customerId", String(args.customerId)) if (args.customerCompanyId != null) query.append("customerCompanyId", String(args.customerCompanyId)) if (args.context != null) query.append("context", String(args.context)) if (args.status != null) query.append("status", String(args.status)) if (args.purchaseEffectiveDate != null) query.append("purchaseEffectiveDate", args.purchaseEffectiveDate.join(',')) if (args.createdOn != null) query.append("createdOn", args.createdOn.join(',')) if (args.searchText != null) query.append("searchText", String(args.searchText)) if (args.number != null) query.append("number", String(args.number)) if (args.size != null) query.append("size", String(args.size)) if (args.sort != null) query.append("sort", String(args.sort)) const url = `${baseUrl.replace(/\/+$/, '')}/assistedSales/v1/opportunities${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(opportunityPage, await res.json()) };