|
| 1 | +/** |
| 2 | + * NotificationService — manages the current user's notification inbox. |
| 3 | + */ |
| 4 | + |
| 5 | +import { track } from '../../core/telemetry'; |
| 6 | +import { BaseService } from '../base'; |
| 7 | + |
| 8 | +import type { |
| 9 | + NotificationGetAllOptions, |
| 10 | + NotificationGetResponse, |
| 11 | +} from '../../models/notification/notifications.types'; |
| 12 | +import type { |
| 13 | + NotificationServiceModel, |
| 14 | +} from '../../models/notification/notifications.models'; |
| 15 | +import { |
| 16 | + INTERNAL_NOTIFICATION_FIELDS, |
| 17 | + type RawNotificationEntry, |
| 18 | +} from '../../models/notification/notifications.internal-types'; |
| 19 | + |
| 20 | +import { ODATA_OFFSET_PARAMS, ODATA_PAGINATION } from '../../utils/constants/common'; |
| 21 | +import { NOTIFICATION_ENDPOINTS } from '../../utils/constants/endpoints'; |
| 22 | +import { TENANT_ID } from '../../utils/constants/headers'; |
| 23 | +import { createHeaders } from '../../utils/http/headers'; |
| 24 | +import { |
| 25 | + HasPaginationOptions, |
| 26 | + NonPaginatedResponse, |
| 27 | + PaginatedResponse, |
| 28 | +} from '../../utils/pagination/types'; |
| 29 | +import { PaginationHelpers } from '../../utils/pagination/helpers'; |
| 30 | +import { PaginationType } from '../../utils/pagination/internal-types'; |
| 31 | + |
| 32 | +/** |
| 33 | + * Service for interacting with the UiPath Notification inbox. |
| 34 | + * |
| 35 | + * Provides list operations against the current user's notifications (the |
| 36 | + * `/odata/v1/NotificationEntry` API). Further inbox operations (mark-read, |
| 37 | + * delete) land in follow-up PRs. |
| 38 | + * |
| 39 | + * Every public method takes the acting tenant GUID as the first argument — the |
| 40 | + * notification API identifies the tenant via the `X-UIPATH-Internal-TenantId` |
| 41 | + * header and the SDK forwards `tenantId` into that header on each call. |
| 42 | + */ |
| 43 | +export class NotificationService extends BaseService implements NotificationServiceModel { |
| 44 | + /** |
| 45 | + * Lists notifications from the current user's inbox. |
| 46 | + * |
| 47 | + * Returns the full list when no pagination params are provided, or a paginated cursor result |
| 48 | + * when any of `pageSize`/`cursor`/`jumpToPage` is supplied. Supports OData `filter` and |
| 49 | + * `orderby` query options. |
| 50 | + * |
| 51 | + * @param tenantId - Tenant GUID (sent via `X-UIPATH-Internal-TenantId`) |
| 52 | + * @param options - Optional OData query and pagination options |
| 53 | + * @returns Array of notifications, or a paginated result when pagination params are supplied |
| 54 | + * {@link NotificationGetResponse} |
| 55 | + * |
| 56 | + * @example Basic usage |
| 57 | + * ```typescript |
| 58 | + * import { Notifications } from '@uipath/uipath-typescript/notifications'; |
| 59 | + * |
| 60 | + * const notifications = new Notifications(sdk); |
| 61 | + * const all = await notifications.getAll('<tenantId>'); |
| 62 | + * ``` |
| 63 | + * |
| 64 | + * @example Filter unread, most recent first |
| 65 | + * ```typescript |
| 66 | + * const unread = await notifications.getAll('<tenantId>', { |
| 67 | + * filter: 'isRead eq false', |
| 68 | + * orderby: 'publishedOn desc', |
| 69 | + * }); |
| 70 | + * ``` |
| 71 | + * |
| 72 | + * @example First page with pagination |
| 73 | + * ```typescript |
| 74 | + * const page1 = await notifications.getAll('<tenantId>', { pageSize: 20 }); |
| 75 | + * if (page1.hasNextPage) { |
| 76 | + * const page2 = await notifications.getAll('<tenantId>', { cursor: page1.nextCursor }); |
| 77 | + * } |
| 78 | + * ``` |
| 79 | + * @internal |
| 80 | + */ |
| 81 | + @track('Notifications.GetAll') |
| 82 | + async getAll<T extends NotificationGetAllOptions = NotificationGetAllOptions>( |
| 83 | + tenantId: string, |
| 84 | + options?: T |
| 85 | + ): Promise< |
| 86 | + T extends HasPaginationOptions<T> |
| 87 | + ? PaginatedResponse<NotificationGetResponse> |
| 88 | + : NonPaginatedResponse<NotificationGetResponse> |
| 89 | + > { |
| 90 | + return PaginationHelpers.getAll({ |
| 91 | + serviceAccess: this.createPaginationServiceAccess(), |
| 92 | + getEndpoint: () => NOTIFICATION_ENDPOINTS.GET_ALL, |
| 93 | + headers: createHeaders({ [TENANT_ID]: tenantId }), |
| 94 | + transformFn: stripInternalNotificationFields, |
| 95 | + pagination: { |
| 96 | + paginationType: PaginationType.OFFSET, |
| 97 | + itemsField: ODATA_PAGINATION.ITEMS_FIELD, |
| 98 | + totalCountField: ODATA_PAGINATION.TOTAL_COUNT_FIELD, |
| 99 | + paginationParams: { |
| 100 | + pageSizeParam: ODATA_OFFSET_PARAMS.PAGE_SIZE_PARAM, |
| 101 | + offsetParam: ODATA_OFFSET_PARAMS.OFFSET_PARAM, |
| 102 | + countParam: ODATA_OFFSET_PARAMS.COUNT_PARAM, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, options) as Promise< |
| 106 | + T extends HasPaginationOptions<T> |
| 107 | + ? PaginatedResponse<NotificationGetResponse> |
| 108 | + : NonPaginatedResponse<NotificationGetResponse> |
| 109 | + >; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Drops internal/transport-layer fields from a raw notification entry before |
| 115 | + * returning it to the SDK consumer. Exported as module-level for testability. |
| 116 | + * |
| 117 | + * @internal |
| 118 | + */ |
| 119 | +export function stripInternalNotificationFields(item: RawNotificationEntry): NotificationGetResponse { |
| 120 | + const result: RawNotificationEntry = { ...item }; |
| 121 | + for (const field of INTERNAL_NOTIFICATION_FIELDS) { |
| 122 | + delete result[field]; |
| 123 | + } |
| 124 | + return result; |
| 125 | +} |
0 commit comments