import {z} from 'zod' export type GetApiMyeduzzV1SalesSummaryArgs = {startDate: string, endDate: string, contractId?: number | undefined, productId?: number | undefined, affiliateId?: number | undefined, status?: ('open' | 'processing' | 'paid' | 'canceled' | 'waitingRefund' | 'refunded' | 'duplicated' | 'expired' | 'recovering' | 'waitingPayment' | 'scheduled' | 'negotiated') | undefined}; export type GetApiMyeduzzV1SalesSummaryOptions = {baseUrl?: string | undefined, eduzzAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiMyeduzzV1SalesSummaryResponse = z.object({discount: z.object({currency: z.string(), value: z.number()}), grossGain: z.object({currency: z.string(), value: z.number()}), netGain: z.object({currency: z.string(), value: z.number()}), partnersGain: z.object({currency: z.string(), value: z.number()}), affiliatesGain: z.object({currency: z.string(), value: z.number()}), fee: z.object({currency: z.string(), value: z.number()}), installmentSettlementFee: z.object({currency: z.string(), value: z.number()}), others: z.object({currency: z.string(), value: z.number()}), total: z.object({currency: z.string(), value: z.number()})}); 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 }; /** * Retorna um relatório sumarizado com o total realizado em vendas com base nos filtros fornecidos. * * GET /myeduzz/v1/sales/summary */ export const getApiMyeduzzV1SalesSummary = async (args: GetApiMyeduzzV1SalesSummaryArgs, options?: GetApiMyeduzzV1SalesSummaryOptions) => { const {baseUrl = "https://api.eduzz.com", fetch: fetchFn = fetch, eduzzAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.contractId != null) query.append("contractId", String(args.contractId)) if (args.productId != null) query.append("productId", String(args.productId)) if (args.affiliateId != null) query.append("affiliateId", String(args.affiliateId)) if (args.status != null) query.append("status", String(args.status)) if (eduzzAuth !== undefined) headers.set('authorization', `Bearer ${eduzzAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/myeduzz/v1/sales/summary${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(getApiMyeduzzV1SalesSummaryResponse, await res.json()) };