import {z} from 'zod' export type PatchV2InsightsSmartNotificationRulesRuleIdToggleStatus = {enabled: boolean}; export type PatchApiV2InsightsSmartNotificationRulesRuleIdToggleStatusArgs = {rule_id: number, body: PatchV2InsightsSmartNotificationRulesRuleIdToggleStatus}; export type PatchApiV2InsightsSmartNotificationRulesRuleIdToggleStatusOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const smartNotificationRule = z.object({id: z.number().int(), name: z.string(), enabled: z.boolean(), frequency: z.enum(["hourly", "daily", "weekly", "monthly"]), metric_type: z.enum(["work_time", "core_time", "non_core_time", "unproductive_time", "activity", "suspicious_activity", "unusual_activity", "time_spent_in_tool", "idle_time", "idle_time_percent", "manual_time", "manual_time_percent", "geo_ip_changes", "users_per_ip", "geo_ip_list_match"]), condition: z.enum(["above", "below"]), condition_value: z.number(), time_unit: z.enum(["per_hour", "per_day", "per_week", "per_month"]).optional(), target: z.object({}), recipients: z.object({}), deliver_by: z.array(z.enum(["email", "slack", "insights"])), created_at: z.string(), updated_at: z.string()}); export const patchApiV2InsightsSmartNotificationRulesRuleIdToggleStatusResponse = z.object({smart_notification_rule: smartNotificationRule.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 }; /** * Toggle smart notification rule * * PATCH /v2/insights/smart_notification_rules/{rule_id}/toggle_status */ export const patchApiV2InsightsSmartNotificationRulesRuleIdToggleStatus = async (args: PatchApiV2InsightsSmartNotificationRulesRuleIdToggleStatusArgs, options?: PatchApiV2InsightsSmartNotificationRulesRuleIdToggleStatusOptions) => { const {baseUrl = "https://api.hubstaff.com", 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(/\/+$/, '')}/v2/insights/smart_notification_rules/${encodeURIComponent(String(args.rule_id))}/toggle_status${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV2InsightsSmartNotificationRulesRuleIdToggleStatusResponse, await res.json()) };