import {z} from 'zod' export type GetApiV2OrgOrgIdGroupGroupIdCanPurgeArgs = {orgId: string, groupId: string}; export type GetApiV2OrgOrgIdGroupGroupIdCanPurgeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const deleteFromHistoryEligibility = z.object({canDeleteFromHistory: z.boolean(), reasons: z.array(z.enum(["NOT_FOUND", "HAS_JOBS", "HAS_CHILD_GROUPS", "REFERENCED_BY_GOAL", "REFERENCED_BY_BUDGET", "REFERENCED_BY_POLICY", "REFERENCED_BY_USER_ACCESS", "REFERENCED_BY_SCENARIO", "REFERENCED_BY_COMP_REVIEW", "REFERENCED_BY_TIMEOFF_POLICY"]))}); 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 }; /** * Check whether a group is eligible to be deleted from history * * GET /v2/org/{orgId}/group/{groupId}/can-purge */ export const getApiV2OrgOrgIdGroupGroupIdCanPurge = async (args: GetApiV2OrgOrgIdGroupGroupIdCanPurgeArgs, options?: GetApiV2OrgOrgIdGroupGroupIdCanPurgeOptions) => { const {baseUrl = "https://localhost", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/org/${encodeURIComponent(String(args.orgId))}/group/${encodeURIComponent(String(args.groupId))}/can-purge${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(deleteFromHistoryEligibility, await res.json()) };