import {z} from 'zod' export type TicketRequest = {draw_number: string, combination_count: number, rematch: boolean, second_rematch: boolean, total_amount: string}; export type BranchInfo = {name: string, street: string, city: string, neighborhood: string}; export type CreateLotteryTicketRequest = {payee_id: string, payer_reference: string, ticket: TicketRequest, branch: BranchInfo}; export type CreateApiApiV1LotteryTicketsArgs = {body: CreateLotteryTicketRequest}; export type CreateApiApiV1LotteryTicketsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ticketResponse = z.object({combination_numbers: z.string().optional(), draw_number: z.string().optional(), combination_count: z.number().int().optional(), rematch: z.boolean().optional(), second_rematch: z.boolean().optional(), total_amount: z.string().optional(), transaction_date: z.string().optional(), store_id: z.string().optional(), pos_id: z.string().optional(), foreign_pos_id: z.string().optional(), transaction_number: z.string().optional(), draw_date: z.string().optional(), reference_number: z.string().optional(), session_id: z.string().optional(), security_number: z.string().optional(), client_name: z.string().optional(), client_street: z.string().optional(), client_city: z.string().optional(), client_neighborhood: z.string().optional()}); export const branchInfo = z.object({name: z.string(), street: z.string(), city: z.string(), neighborhood: z.string()}); export const receiptResponse = z.object({combination_numbers: z.string(), transaction_date: z.string(), store_id: z.string(), pos_id: z.string(), foreign_pos_id: z.string(), transaction_number: z.string(), reference_number: z.string(), session_id: z.string(), security_number: z.string(), serial_number: z.string(), creation_date: z.number().int(), ticket_image: z.string()}); export const paymentResponse = z.object({id: z.string().optional(), payer_account: z.string().optional(), payee_id: z.string().optional(), amount: z.number().optional(), currency: z.string().optional(), status: z.enum(["pending", "completed", "failed"]).optional(), pay_type: z.string().optional()}); export const lotteryTicketResponse = z.object({id: z.string().optional(), client_id: z.number().int().optional(), ticket: ticketResponse.optional(), branch: branchInfo.optional(), receipt: receiptResponse.optional(), status: z.enum(["pending", "processed", "completed", "failed"]).optional(), payment: paymentResponse.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 }; /** * Purchase a lottery ticket * * POST /api/v1/lottery_tickets */ export const createApiApiV1LotteryTickets = async (args: CreateApiApiV1LotteryTicketsArgs, options?: CreateApiApiV1LotteryTicketsOptions) => { const {baseUrl = "https://dev-api.finco.lat", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/lottery_tickets${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(lotteryTicketResponse, await res.json()) };