import {z} from 'zod' export type GetApiPublicApiSubscriptionsChurnArgs = {limit?: number | undefined, offset?: number | undefined, status?: string | undefined, current_period?: number | undefined, current_period__gt?: number | undefined, current_period__lt?: number | undefined, createdAt__gte?: string | undefined, createdAt__lte?: string | undefined, canceledAt__gte?: string | undefined, canceledAt__lte?: string | undefined}; export type GetApiPublicApiSubscriptionsChurnOptions = {baseUrl?: string | undefined, OAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionChurnCustomer = z.object({name: z.string()}); export const subscriptionChurn = z.object({id: z.string(), status: z.string(), current_period: z.number().int().optional(), recurrence_period: z.number().int().optional(), quantity_recurrences: z.number().int().optional(), trial_days: z.number().int().optional(), max_retries: z.number().int().optional(), amount: z.string(), retry_interval: z.number().int().optional(), paid_payments_quantity: z.number().int().optional(), retention: z.string().optional(), paymentMethod: z.string().optional(), customer: subscriptionChurnCustomer, product: z.string().optional(), offer: z.string().optional(), orders: z.array(z.string()).optional(), next_payment_date: z.string().nullable().optional(), canceledAt: z.string().nullable().optional()}); export const subscriptionChurnMetrics = z.object({at_risk_value: z.number(), at_risk_count: z.number().int(), churned_value: z.number(), churned_count: z.number().int(), affected_customers_count: z.number().int(), total_at_risk_value: z.number()}); export const paginatedSubscriptionChurnList = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(subscriptionChurn), metrics: subscriptionChurnMetrics}); 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 }; /** * Lista assinaturas canceladas e inativas com métricas agregadas * * GET /public_api/subscriptions/churn/ */ export const getApiPublicApiSubscriptionsChurn = async (args: GetApiPublicApiSubscriptionsChurnArgs, options?: GetApiPublicApiSubscriptionsChurnOptions) => { const {baseUrl = "https://api.cakto.com.br", fetch: fetchFn = fetch, OAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.status != null) query.append("status", String(args.status)) if (args.current_period != null) query.append("current_period", String(args.current_period)) if (args.current_period__gt != null) query.append("current_period__gt", String(args.current_period__gt)) if (args.current_period__lt != null) query.append("current_period__lt", String(args.current_period__lt)) if (args.createdAt__gte != null) query.append("createdAt__gte", String(args.createdAt__gte)) if (args.createdAt__lte != null) query.append("createdAt__lte", String(args.createdAt__lte)) if (args.canceledAt__gte != null) query.append("canceledAt__gte", String(args.canceledAt__gte)) if (args.canceledAt__lte != null) query.append("canceledAt__lte", String(args.canceledAt__lte)) if (OAuthToken !== undefined) headers.set('authorization', `Bearer ${OAuthToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/public_api/subscriptions/churn/${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(paginatedSubscriptionChurnList, await res.json()) };