import {z} from 'zod' export type GetApiLocationsLocationIdReviewCampaignsArgs = {locationId: string, startDate?: string | undefined, endDate?: string | undefined, Authorization: string}; export type GetApiLocationsLocationIdReviewCampaignsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const emailTemplateV4 = z.object({content: z.string().optional(), fromEmail: z.string().optional(), heading: z.string().optional(), replyTo: z.string().optional(), reviewSiteHash: z.array(z.string()).optional(), subject: z.string().optional()}); export const smsTemplateV2 = z.object({content: z.string().optional(), reviewSiteHash: z.array(z.string()).optional()}); export const campaignStatV2 = z.object({name: z.enum(["emails_sent", "sms_sent", "invite_open_rate", "clicks_rate", "customer_exp_score"]).optional(), value: z.string().optional()}); export const reviewCampaignV2 = z.object({id: z.string().optional(), locationId: z.string().optional(), name: z.string().optional(), screening: z.boolean().optional(), createdAt: z.string().optional(), openingEmailTemplate: emailTemplateV4.optional(), smsTemplate: smsTemplateV2.optional(), firstFollowUpSchedule: z.enum(["DAYS_2", "DAYS_5"]).optional(), firstFollowUpTemplate: emailTemplateV4.optional(), secondFollowUpSchedule: z.enum(["DAYS_2", "DAYS_5"]).optional(), secondFollowUpTemplate: emailTemplateV4.optional(), stats: z.array(campaignStatV2).optional(), numberOfFeedbacks: z.number().int().optional()}); export const reviewCampaignsResponse = z.object({data: z.object({listReviewCampaigns: z.object({reviewCampaigns: z.array(reviewCampaignV2).optional()}).optional()}).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 }; /** * List review campaigns * * GET /locations/{locationId}/review-campaigns */ export const getApiLocationsLocationIdReviewCampaigns = async (args: GetApiLocationsLocationIdReviewCampaignsArgs, options?: GetApiLocationsLocationIdReviewCampaignsOptions) => { const {baseUrl = "https://api.synup.com/api/v4", fetch: fetchFn = fetch} = 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.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/locations/${encodeURIComponent(String(args.locationId))}/review-campaigns${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(reviewCampaignsResponse, await res.json()) };