import {z} from 'zod' export type BeezUPCommonChannelId = unknown; export type ReportFilterPeriodType = 'Custom' | 'Yesterday' | 'Last7Days' | 'LastWeek' | 'ThisMonth' | 'Last30Days' | 'LastMonth' | 'Last60Days' | 'Last90Days' | 'Last3Months'; export type BeezUPCommonCatalogCategoryFilter = {categoryPath?: Array | undefined}; export type PerformanceIndicatorType = 'ClickCount' | 'Cost' | 'OrderCount' | 'ProductCount' | 'SoldProductCount' | 'DisabledProductCount' | 'EnabledProductCount' | 'TotalSales' | 'Margin' | 'ROI' | 'PerformanceIndicator'; export type PerformanceIndicatorFilterOperatorName = string; export type PerformanceIndicatorFilter = {performanceIndicator: PerformanceIndicatorType, operatorName: PerformanceIndicatorFilterOperatorName, value: number}; export type MarginType = 'Tracker' | 'Global'; export type LinkClickToOrderType = 'OnPurchaseDate' | 'OnClickDate'; export type PerformanceIndicatorFormulaParameterType = 'Cost' | 'TotalSales' | 'Margin' | 'TotalSalesMinusCost' | 'MarginMinusCost' | 'OrderCount' | 'ClickCount' | 'SoldProductCount' | 'One'; export type PerformanceIndicatorFormulaOperatorName = 'Multiply' | 'Divide'; export type PerformanceIndicatorFormula = {firstParameter?: PerformanceIndicatorFormulaParameterType | undefined, operatorName?: PerformanceIndicatorFormulaOperatorName | undefined, secondParameter?: PerformanceIndicatorFormulaParameterType | undefined, thirdParameter?: number | undefined}; export type ReportAdvancedFilters = {marginType: MarginType, globalMarginPercent?: number | undefined, linkClickToOrderType: LinkClickToOrderType, linkClickToOrderMaxDay?: number | undefined, onlyPaymentValidatedOrders: boolean, onlyDirectSales: boolean, performanceIndicatorFormula: PerformanceIndicatorFormula}; export type OrderByDirection = 'Ascending' | 'Descending'; export type BeezUPCommonPageSize = number; export type BeezUPCommonPageNumber = number; export type ReportByChannelRequest = {channelId?: BeezUPCommonChannelId | undefined, periodType: ReportFilterPeriodType, beginPeriodUtcDate?: string | undefined, endPeriodUtcDate?: string | undefined, categoryFilter?: BeezUPCommonCatalogCategoryFilter | undefined, performanceIndicatorFilters?: Array | undefined, advancedFilters: ReportAdvancedFilters, orderBy?: PerformanceIndicatorType | undefined, orderByDirection?: OrderByDirection | undefined, pageSize?: BeezUPCommonPageSize | undefined, pageNumber?: BeezUPCommonPageNumber | undefined}; export type CreateApiV2UserAnalyticsStoreIdReportsBychannelArgs = {storeId: unknown, body: ReportByChannelRequest}; export type CreateApiV2UserAnalyticsStoreIdReportsBychannelOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const beezUPCommonDocUrl = z.string(); export const beezUPCommonHref = z.string(); export const beezUPCommonOperationId = z.string(); export const beezUPCommonHttpMethod = z.enum(["GET", "POST", "PATCH", "DELETE", "PUT", "HEAD"]); export const beezUPCommonParameterIn = z.enum(["path", "header", "query", "body"]); export const beezUPCommonParameterType = z.enum(["string", "integer", "number", "boolean", "object", "array", "date", "date-time"]); export const beezUPCommonLOVLink3 = z.object({href: z.string(), method: beezUPCommonHttpMethod.optional()}); export const beezUPCommonLinkParameterProperty3 = z.object({label: z.string().optional(), value: z.object({}).optional(), required: z.boolean().optional(), type: beezUPCommonParameterType, lovLink: beezUPCommonLOVLink3.optional(), lovRequired: z.boolean().optional(), description: z.string().optional(), schema: z.string().optional()}); export const beezUPCommonLinkParameter3 = z.object({label: z.string().optional(), value: z.object({}).optional(), required: z.boolean().optional(), in: beezUPCommonParameterIn, type: beezUPCommonParameterType.optional(), lovLink: beezUPCommonLOVLink3.optional(), lovRequired: z.boolean().optional(), description: z.string().optional(), schema: z.string().optional(), properties: z.record(z.string(), beezUPCommonLinkParameterProperty3).optional()}); export const beezUPCommonSuccessSummary = z.object({successCode: z.string().optional(), successMessage: z.string().optional(), successArguments: z.record(z.string(), z.string()).optional(), propertyName: z.string().optional(), propertyValue: z.string().optional(), objectName: z.string().optional()}); export const beezUPCommonExceptionDetail = z.object({helpLink: z.string().optional(), message: z.string().optional(), stackTrace: z.string().optional(), type: z.string().optional()}); export const beezUPCommonErrorSummary = z.object({utcDate: z.string().optional(), errorGuid: z.string().optional(), errorCode: z.string().optional(), errorMessage: z.string().optional(), technicalErrorMessage: z.string().optional(), exceptionDetail: beezUPCommonExceptionDetail.optional(), errorArguments: z.record(z.string(), z.string()).optional(), propertyName: z.string().optional(), propertyValue: z.string().optional(), objectName: z.string().optional(), source: z.string().optional()}); export const beezUPCommonWarningSummary = z.object({technicalErrorMessage: z.string().optional(), warningMessage: z.string().optional(), warningCode: z.string().optional(), warningArguments: z.record(z.string(), z.string()).optional()}); export const beezUPCommonInfoSummary = z.object({informationCode: z.string().optional(), informationMessage: z.string().optional(), informationArguments: z.record(z.string(), z.string()).optional(), propertyName: z.string().optional(), propertyValue: z.string().optional(), objectName: z.string().optional()}); export const beezUPCommonInfoSummaries = z.object({successes: z.array(beezUPCommonSuccessSummary).optional(), errors: z.array(beezUPCommonErrorSummary).optional(), warnings: z.array(beezUPCommonWarningSummary).optional(), informations: z.array(beezUPCommonInfoSummary).optional()}); export const beezUPCommonLink3 = z.object({label: z.string().optional(), docUrl: beezUPCommonDocUrl.optional(), description: z.string().optional(), href: beezUPCommonHref, operationId: beezUPCommonOperationId.optional(), method: beezUPCommonHttpMethod.optional(), parameters: z.record(z.string(), beezUPCommonLinkParameter3).optional(), urlTemplated: z.boolean().optional(), allRequiredParamsProvided: z.boolean().optional(), allOptionalParamsProvided: z.boolean().optional(), info: beezUPCommonInfoSummaries.optional()}); export const beezUPCommonPaginationResultLinks = z.object({first: beezUPCommonLink3, last: beezUPCommonLink3, previous: beezUPCommonLink3.optional(), next: beezUPCommonLink3.optional()}); export const beezUPCommonPaginationResult = z.object({entryCount: z.number().int().gte(0), pageCount: z.number().int().gte(0), totalEntryCount: z.number().int().gte(0), links: beezUPCommonPaginationResultLinks}); export const beezUPCommonCurrencyCode = z.string(); export const beezUPCommonChannelId = z.string(); export const beezUPCommonChannelName = z.string(); export const beezUPCommonHttpUrl = z.string(); export const beezUPCommonChannelBasicInfo = z.object({channelId: beezUPCommonChannelId, channelName: beezUPCommonChannelName, channelImageUrl: beezUPCommonHttpUrl}); export const linksOptimiseByChannelLink = z.object({label: z.string().optional(), docUrl: beezUPCommonDocUrl.optional(), description: z.string().optional(), href: beezUPCommonHref, operationId: beezUPCommonOperationId.optional(), method: beezUPCommonHttpMethod.optional(), parameters: z.record(z.string(), beezUPCommonLinkParameter3).optional(), urlTemplated: z.boolean().optional(), allRequiredParamsProvided: z.boolean().optional(), allOptionalParamsProvided: z.boolean().optional(), info: beezUPCommonInfoSummaries.optional()}); export const reportByChannelLinks = z.object({enableProducts: linksOptimiseByChannelLink.optional(), disableProducts: linksOptimiseByChannelLink.optional()}); export const reportByChannel = z.object({channel: beezUPCommonChannelBasicInfo, catalogProductCount: z.number().int(), enabledProductCount: z.number().int(), clickCount: z.number().int(), cost: z.number(), orderCount: z.number().int(), roi: z.number().optional(), margin: z.number().optional(), performanceIndicator: z.number(), totalSales: z.number(), soldProductCount: z.number().int(), links: reportByChannelLinks}); export const linksOptimiseAllLink = z.object({label: z.string().optional(), docUrl: beezUPCommonDocUrl.optional(), description: z.string().optional(), href: beezUPCommonHref, operationId: beezUPCommonOperationId.optional(), method: beezUPCommonHttpMethod.optional(), parameters: z.record(z.string(), beezUPCommonLinkParameter3).optional(), urlTemplated: z.boolean().optional(), allRequiredParamsProvided: z.boolean().optional(), allOptionalParamsProvided: z.boolean().optional(), info: beezUPCommonInfoSummaries.optional()}); export const linksOptimiseLink = z.object({label: z.string().optional(), docUrl: beezUPCommonDocUrl.optional(), description: z.string().optional(), href: beezUPCommonHref, operationId: beezUPCommonOperationId.optional(), method: beezUPCommonHttpMethod.optional(), parameters: z.record(z.string(), beezUPCommonLinkParameter3).optional(), urlTemplated: z.boolean().optional(), allRequiredParamsProvided: z.boolean().optional(), allOptionalParamsProvided: z.boolean().optional(), info: beezUPCommonInfoSummaries.optional()}); export const reportByChannelResponseLinks = z.object({enableAllProducts: linksOptimiseAllLink.optional(), disableAllProducts: linksOptimiseAllLink.optional(), enableProducts: linksOptimiseLink.optional(), disableProducts: linksOptimiseLink.optional()}); export const reportByChannelResponse = z.object({paginationResult: beezUPCommonPaginationResult, currencyCode: beezUPCommonCurrencyCode, channels: z.array(reportByChannel), links: reportByChannelResponseLinks}); 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 }; /** * Get the report by channel * * POST /v2/user/analytics/{storeId}/reports/bychannel */ export const createApiV2UserAnalyticsStoreIdReportsBychannel = async (args: CreateApiV2UserAnalyticsStoreIdReportsBychannelArgs, options?: CreateApiV2UserAnalyticsStoreIdReportsBychannelOptions) => { const {baseUrl = "https://api.beezup.com", 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(/\/+$/, '')}/v2/user/analytics/${encodeURIComponent(String(args.storeId))}/reports/bychannel${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(reportByChannelResponse, await res.json()) };