import {z} from 'zod' export type CreatePromotion = {Name?: string | undefined, MenuItemPublicIds?: Array | undefined}; export type CreditNoteDetails = {RemainingCredit?: number | undefined}; export type LumpDiscountDetails = {DiscountAmount?: number | undefined}; export type PercentDiscountDetails = {PercentageDiscount?: number | undefined, MaxDiscountAmount?: (number | null) | undefined}; export type ValidityPeriod = {DayOfWeek?: ('Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday') | undefined, StartTime?: string | undefined, EndTime?: string | undefined}; export type CreateVoucher = {VoucherType?: ('PercentageDiscount' | 'LumpDiscount' | 'AddItem' | 'CreditNote' | 'FreeDelivery') | undefined, Promotion?: CreatePromotion | undefined, CreditNoteDetails?: CreditNoteDetails | undefined, LumpDiscountDetails?: LumpDiscountDetails | undefined, PercentDiscountDetails?: PercentDiscountDetails | undefined, Code?: string | undefined, Description?: string | undefined, Stores?: Array | undefined, ValidOnOrdersOver?: number | undefined, TakesPriority?: boolean | undefined, IsEnabled?: boolean | undefined, IsAutomaticallyApplied?: boolean | undefined, IncludeDeliveryFee?: boolean | undefined, IsValidForDeliveryOrders?: boolean | undefined, IsValidForPickupOrders?: boolean | undefined, IsValidForOrdersPayedOnline?: boolean | undefined, IsValidForOrdersPayedByCash?: boolean | undefined, IsValidForFirstOrderOnly?: boolean | undefined, IsValidOncePerCustomer?: boolean | undefined, IsValidOnlyOnce?: boolean | undefined, IsDiscoverable?: boolean | undefined, IsPromoted?: boolean | undefined, ForceDiscount?: boolean | undefined, StartDate?: string | undefined, ExpiryDate?: string | undefined, ChannelRestrictions?: (Array<'Ios' | 'Android' | 'Web' | 'Kiosk' | 'Pos' | 'Google'>) | undefined, ValidityPeriods?: Array | undefined, VoucherSubType?: ('None' | 'SignUp' | 'Loyalty' | 'Loyalty25' | 'Retention' | 'SecondaryRetention' | 'Custom') | undefined, CustomerId?: (number | null) | undefined, MaxRedemptions?: (number | null) | undefined}; export type CreateApiApiV10VouchersAppIdArgs = {appId: string, body: CreateVoucher}; export type CreateApiApiV10VouchersAppIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const promotionAward = z.object({PromotionAwardId: z.number().int().optional(), MenuItemPublicId: z.string().optional()}); export const promotionDetails = z.object({PromotionId: z.number().int().optional(), Name: z.string().optional(), PromotionAwards: z.array(promotionAward).optional()}); export const creditNoteDetails = z.object({RemainingCredit: z.number().optional()}); export const lumpDiscountDetails = z.object({DiscountAmount: z.number().optional()}); export const percentDiscountDetails = z.object({PercentageDiscount: z.number().int().optional(), MaxDiscountAmount: z.number().nullable().optional()}); export const validityPeriod = z.object({DayOfWeek: z.enum(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]).optional(), StartTime: z.string().optional(), EndTime: z.string().optional()}); export const voucherWithStats = z.object({TotalUsed: z.number().int().nullable().optional(), TotalCustomers: z.number().int().nullable().optional(), TotalAmountFromOrders: z.number().nullable().optional(), TotalDiscounted: z.number().nullable().optional(), AverageOrderSize: z.number().nullable().optional(), VoucherId: z.number().int().optional(), Status: z.enum(["Valid", "NotYetValid", "Expired", "Used", "Disabled"]).optional(), VoucherType: z.enum(["PercentageDiscount", "LumpDiscount", "AddItem", "CreditNote", "FreeDelivery"]).optional(), Currency: z.enum(["EUR", "USD", "GBP", "CAD", "AUD", "DJF", "ZAR", "ETB", "AED", "BHD", "DZD", "EGP", "IQD", "JOD", "KWD", "LBP", "LYD", "MAD", "OMR", "QAR", "SAR", "SYP", "TND", "YER", "CLP", "INR", "AZN", "RUB", "BYN", "BGN", "NGN", "BDT", "CNY", "BAM", "CZK", "DKK", "CHF", "MVR", "BTN", "XCD", "BZD", "HKD", "IDR", "JMD", "MYR", "NZD", "PHP", "SGD", "TTD", "XDR", "ARS", "BOB", "COP", "CRC", "CUP", "DOP", "GTQ", "HNL", "MXN", "NIO", "PAB", "PEN", "PYG", "UYU", "VEF", "IRR", "XOF", "CDF", "XAF", "HTG", "ILS", "HRK", "HUF", "AMD", "ISK", "JPY", "GEL", "KZT", "KHR", "KRW", "KGS", "LAK", "MKD", "MNT", "BND", "MMK", "NOK", "NPR", "PKR", "PLN", "AFN", "BRL", "MDL", "RON", "RWF", "SEK", "LKR", "SOS", "ALL", "RSD", "KES", "TJS", "THB", "ERN", "TMT", "BWP", "TRY", "UAH", "UZS", "VND", "MOP", "TWD", "BMD"]).optional(), StoreNames: z.array(z.string()).optional(), PromotionDetails: promotionDetails.optional(), CreditNoteDetails: creditNoteDetails.optional(), LumpDiscountDetails: lumpDiscountDetails.optional(), PercentDiscountDetails: percentDiscountDetails.optional(), Code: z.string().optional(), Description: z.string().optional(), Stores: z.array(z.number().int()).optional(), ValidOnOrdersOver: z.number().optional(), TakesPriority: z.boolean().optional(), IsEnabled: z.boolean().optional(), IsAutomaticallyApplied: z.boolean().optional(), IncludeDeliveryFee: z.boolean().optional(), IsValidForDeliveryOrders: z.boolean().optional(), IsValidForPickupOrders: z.boolean().optional(), IsValidForOrdersPayedOnline: z.boolean().optional(), IsValidForOrdersPayedByCash: z.boolean().optional(), IsValidForFirstOrderOnly: z.boolean().optional(), IsValidOncePerCustomer: z.boolean().optional(), IsValidOnlyOnce: z.boolean().optional(), IsDiscoverable: z.boolean().optional(), IsPromoted: z.boolean().optional(), ForceDiscount: z.boolean().optional(), StartDate: z.string().optional(), ExpiryDate: z.string().optional(), ChannelRestrictions: z.array(z.enum(["Ios", "Android", "Web", "Kiosk", "Pos", "Google"])).optional(), ValidityPeriods: z.array(validityPeriod).optional(), VoucherSubType: z.enum(["None", "SignUp", "Loyalty", "Loyalty25", "Retention", "SecondaryRetention", "Custom"]).optional(), CustomerId: z.number().int().nullable().optional(), MaxRedemptions: z.number().int().nullable().optional()}); export const restApiResultVoucherWithStats = z.object({Data: voucherWithStats}); 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 }; /** * POST /api/v1.0/vouchers/{appId} */ export const createApiApiV10VouchersAppId = async (args: CreateApiApiV10VouchersAppIdArgs, options?: CreateApiApiV10VouchersAppIdOptions) => { const {baseUrl = "https://api.flipdish.co", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1.0/vouchers/${encodeURIComponent(String(args.appId))}${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(restApiResultVoucherWithStats, await res.json()) };