import {z} from 'zod' export type GetApiV2SurveysWithActivitySinceDateArgs = {date: string, assignedToAddressBookOnly?: boolean | undefined, select?: number | undefined, skip?: number | undefined}; export type GetApiV2SurveysWithActivitySinceDateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiSurveyState = z.enum(["Active", "Inactive", "Pending", "Expired", "SurveyFeatureDisabled", "NotAvailableInThisVersion"]); export const apiSurveyConfirmationMode = z.enum(["Text", "Url", "NotAvailableInThisVersion"]); export const apiSurveySubmissionMode = z.enum(["Single", "Multiple", "NotAvailableInThisVersion"]); export const apiRespondentNotificationType = z.enum(["None", "Basic", "Campaign", "NotAvailableInThisVersion"]); export const apiSurveyAssignedAddressBookTarget = z.enum(["AllContacts", "SpecificAddressBook", "NotAvailableInThisVersion"]); export const apiSurvey = z.object({id: z.number().int().optional(), name: z.string().nullable().optional(), url: z.string().nullable().optional(), dateSurveyCreated: z.string().optional(), dateSurveyModified: z.string().optional(), state: apiSurveyState.optional(), firstActiveDate: z.string().nullable().optional(), lastInactiveDate: z.string().nullable().optional(), scheduledStartDate: z.string().nullable().optional(), scheduledEndDate: z.string().nullable().optional(), confirmationMode: apiSurveyConfirmationMode.optional(), submissionMode: apiSurveySubmissionMode.optional(), fieldCount: z.number().int().optional(), notifyCreatorOnResponse: z.boolean().optional(), respondentNotificationType: apiRespondentNotificationType.optional(), respondentNotificationCampaignId: z.number().int().nullable().optional(), isAssignedToAddressBook: z.boolean().optional(), assignedAddressBookTarget: apiSurveyAssignedAddressBookTarget.optional(), assignedSpecificAddressBookId: z.number().int().nullable().optional(), firstResponseDate: z.string().nullable().optional(), lastResponseDate: z.string().nullable().optional(), totalCompleteResponses: z.number().int().optional(), totalCompleteResponsesInLastDay: z.number().int().optional(), totalCompleteResponsesInLastWeek: z.number().int().optional(), totalIncompleteResponses: z.number().int().optional(), totalViews: z.number().int().optional(), totalBounces: z.number().int().optional(), timeToCompleteMax: z.number().int().nullable().optional(), timeToCompleteMin: z.number().int().nullable().optional(), timeToCompleteTotal: z.number().int().nullable().optional(), sourceDirectTotal: z.number().int().optional(), sourceEmailTotal: z.number().int().optional(), sourceEmbeddedTotal: z.number().int().optional(), sourcePopoverTotal: z.number().int().optional(), sourceFacebookTotal: z.number().int().optional(), sourceTwitterTotal: z.number().int().optional(), sourceGooglePlusTotal: z.number().int().optional(), sourceOtherTotal: z.number().int().optional()}); export const getApiV2SurveysWithActivitySinceDateResponse = z.array(apiSurvey); 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 }; /** * Gets a list of surveys in the account that have changed since the specified date. * * GET /v2/surveys/with-activity-since/{date} */ export const getApiV2SurveysWithActivitySinceDate = async (args: GetApiV2SurveysWithActivitySinceDateArgs, options?: GetApiV2SurveysWithActivitySinceDateOptions) => { const {baseUrl = "https://api.dotdigital.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.assignedToAddressBookOnly != null) query.append("assignedToAddressBookOnly", String(args.assignedToAddressBookOnly)) if (args.select != null) query.append("select", String(args.select)) if (args.skip != null) query.append("skip", String(args.skip)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/surveys/with-activity-since/${encodeURIComponent(String(args.date))}${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(getApiV2SurveysWithActivitySinceDateResponse, await res.json()) };