import {z} from 'zod' export type GetApiV2OrganizationsOrganizationIdInsightsSmartNotificationsArgs = {organization_id: number, page_start_id?: number | undefined, page_limit?: number | undefined, user_ids?: Array | undefined, team_ids?: Array | undefined, 'created[start]': string, 'created[stop]': string, rule_id?: number | undefined, include?: Array<'users'> | undefined}; export type GetApiV2OrganizationsOrganizationIdInsightsSmartNotificationsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const smartNotificationMatch = z.object({user_id: z.number().int(), value: z.number()}); export const smartNotificationRuleSnapshot = z.object({id: z.number().int(), name: z.string(), frequency: z.string(), metric_type: z.string(), condition: z.string(), condition_value: z.number(), time_unit: z.string().optional()}); export const smartNotification = z.object({id: z.number().int(), matches: z.array(smartNotificationMatch), total_count: z.number().int(), rule_snapshot: smartNotificationRuleSnapshot, from: z.unknown(), to: z.unknown(), created_at: z.string()}); export const getApiV2OrganizationsOrganizationIdInsightsSmartNotificationsResponse = z.object({smart_notifications: z.array(smartNotification).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 }; /** * List smart notifications * * GET /v2/organizations/{organization_id}/insights/smart_notifications */ export const getApiV2OrganizationsOrganizationIdInsightsSmartNotifications = async (args: GetApiV2OrganizationsOrganizationIdInsightsSmartNotificationsArgs, options?: GetApiV2OrganizationsOrganizationIdInsightsSmartNotificationsOptions) => { const {baseUrl = "https://api.hubstaff.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_start_id != null) query.append("page_start_id", String(args.page_start_id)) if (args.page_limit != null) query.append("page_limit", String(args.page_limit)) for (const item of args.user_ids ?? []) query.append("user_ids", String(item)) for (const item of args.team_ids ?? []) query.append("team_ids", String(item)) if (args['created[start]'] != null) query.append("created[start]", String(args['created[start]'])) if (args['created[stop]'] != null) query.append("created[stop]", String(args['created[stop]'])) if (args.rule_id != null) query.append("rule_id", String(args.rule_id)) for (const item of args.include ?? []) query.append("include", String(item)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/organizations/${encodeURIComponent(String(args.organization_id))}/insights/smart_notifications${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(getApiV2OrganizationsOrganizationIdInsightsSmartNotificationsResponse, await res.json()) };