import {z} from 'zod' export type ChallengeType = 'HABITS'; export type ChallengeInUpdate = {name: string, type: ChallengeType, description?: string | undefined, startDate: string, endDate?: string | undefined, ownerId: number, entitlements?: Array | undefined, chatGroupLink?: string | undefined, maxPoints?: number | undefined, personalHabitChallenge?: boolean | undefined, finalGoal?: number | undefined, globalChallenge?: boolean | undefined, icon?: string | undefined, teamsNames?: Array | undefined, challengeId: number}; export type BodyChallengesAdminUpdateApiV5AdminChallengesPut = {challenge: ChallengeInUpdate}; export type UpdateApiApiV5AdminChallengesArgs = {body: BodyChallengesAdminUpdateApiV5AdminChallengesPut}; export type UpdateApiApiV5AdminChallengesOptions = {baseUrl?: string | undefined, RWAPIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const challenge = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), name: z.string(), type: z.string(), description: z.string().optional(), startDate: z.string(), endDate: z.string().optional(), ownerId: z.number().int(), subscriptionGroups: z.array(z.string()).optional(), entitlements: z.array(z.string()).optional(), chatGroupLink: z.string().optional(), maxPoints: z.number().int().optional(), personalHabitChallenge: z.boolean().optional(), finalGoal: z.number().int().optional(), globalChallenge: z.boolean().optional(), iconId: z.number().int().optional(), iconUrl: z.string().optional()}); export const challengeInResponse = z.object({challenge: challenge}); 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 }; /** * Update challenge * * PUT /api/v5/admin/challenges/ */ export const updateApiApiV5AdminChallenges = async (args: UpdateApiApiV5AdminChallengesArgs, options?: UpdateApiApiV5AdminChallengesOptions) => { const {baseUrl = "https://api.pavlok.com", fetch: fetchFn = fetch, RWAPIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (RWAPIKeyHeader !== undefined) headers.set("Authorization", RWAPIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v5/admin/challenges/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(challengeInResponse, await res.json()) };