import {z} from 'zod' export type PutV2InsightsSmartNotificationRulesRuleId = {name?: string | undefined, frequency?: ('hourly' | 'daily' | 'weekly' | 'monthly') | undefined, metric_type?: ('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') | undefined, condition?: ('above' | 'below') | undefined, condition_value?: number | undefined, target?: ({all_members?: boolean | undefined, team_ids?: Array | undefined, user_ids?: Array | undefined}) | undefined, recipients?: ({roles?: Array | undefined, user_ids?: Array | undefined}) | undefined, deliver_by?: (Array<'email' | 'slack' | 'insights'>) | undefined, time_unit?: ('per_hour' | 'per_day' | 'per_week' | 'per_month') | undefined}; export type UpdateApiV2InsightsSmartNotificationRulesRuleIdArgs = {rule_id: number, body: PutV2InsightsSmartNotificationRulesRuleId}; export type UpdateApiV2InsightsSmartNotificationRulesRuleIdOptions = {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 updateApiV2InsightsSmartNotificationRulesRuleIdResponse = 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 }; /** * Update smart notification rule * * PUT /v2/insights/smart_notification_rules/{rule_id} */ export const updateApiV2InsightsSmartNotificationRulesRuleId = async (args: UpdateApiV2InsightsSmartNotificationRulesRuleIdArgs, options?: UpdateApiV2InsightsSmartNotificationRulesRuleIdOptions) => { 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))}${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(updateApiV2InsightsSmartNotificationRulesRuleIdResponse, await res.json()) };