diff --git a/packages/arcgis-rest-common-types/src/index.ts b/packages/arcgis-rest-common-types/src/index.ts index 36d3366be7..b5254fa5ad 100644 --- a/packages/arcgis-rest-common-types/src/index.ts +++ b/packages/arcgis-rest-common-types/src/index.ts @@ -465,3 +465,12 @@ export type esriUnits = | "esriSRUnit_Kilometer" | "esriSRUnit_NauticalMile" | "esriSRUnit_USNauticalMile"; + +export interface INotification { + id: string; + type: string; + target: string; + targetType: string; + received: number; + data: { [key: string]: any }; +} diff --git a/packages/arcgis-rest-users/src/users.ts b/packages/arcgis-rest-users/src/users.ts index 875211cf8a..0d88adadfd 100644 --- a/packages/arcgis-rest-users/src/users.ts +++ b/packages/arcgis-rest-users/src/users.ts @@ -7,7 +7,7 @@ import { getPortalUrl } from "@esri/arcgis-rest-request"; -import { IUser } from "@esri/arcgis-rest-common-types"; +import { IUser, INotification } from "@esri/arcgis-rest-common-types"; import { UserSession, IUserRequestOptions } from "@esri/arcgis-rest-auth"; export interface IGetUserRequestOptions extends IRequestOptions { @@ -68,3 +68,35 @@ export function getUser( // send the request return request(url, options); } + +export interface IGetUserNotificationRequestOptions extends IRequestOptions { + /** + * A session representing a logged in user. + */ + authentication?: UserSession; +} +export interface INotificationResult { + notifications: INotification[]; +} + +/** + * Get notifications for a user. + * + * @param requestOptions - options to pass through in the request + * @returns A Promise that will resolve with the user's notifications + */ +export function getUserNotifications( + requestOptions: IGetUserNotificationRequestOptions +): Promise { + let url; + let options = { httpMethod: "GET" } as IGetUserNotificationRequestOptions; + + const username = requestOptions.authentication.username; + url = `${getPortalUrl(requestOptions)}/community/users/${encodeURIComponent( + username + )}/notifications`; + options = { ...requestOptions, ...options }; + + // send the request + return request(url, options); +} diff --git a/packages/arcgis-rest-users/test/mocks/responses.ts b/packages/arcgis-rest-users/test/mocks/responses.ts index e31d3a6ccb..7b9d4020e7 100644 --- a/packages/arcgis-rest-users/test/mocks/responses.ts +++ b/packages/arcgis-rest-users/test/mocks/responses.ts @@ -2,6 +2,7 @@ * Apache-2.0 */ import { IUser } from "@esri/arcgis-rest-common-types"; +import { INotificationResult } from "../../src/users"; export const AnonUserResponse: IUser = { username: "jsmith", @@ -171,3 +172,31 @@ export const OrgAdminUserResponse = { } ] }; + +export const UserNotificationsResponse: INotificationResult = { + notifications: [ + { + id: "7eee83bb4bc94c1e82bb5b931ab9a818", + type: "message_received", + target: "c@sey", + targetType: "user", + received: 1534788621000, + data: { + fromUser: "adminuser", + subject: "this is the subject", + message: "this is the message" + } + }, + { + id: "e8c18248ee2f4eb298d443026982b59c", + type: "group_join", + target: "c@sey", + targetType: "user", + received: 1534788353000, + data: { + groupId: "0c943127d4a545e6874e4ee4e1f88fa8", + groupTitle: "This is Jupe's Test Event" + } + } + ] +}; diff --git a/packages/arcgis-rest-users/test/users.test.ts b/packages/arcgis-rest-users/test/users.test.ts index 3bd74dac90..c975fbc814 100644 --- a/packages/arcgis-rest-users/test/users.test.ts +++ b/packages/arcgis-rest-users/test/users.test.ts @@ -1,12 +1,13 @@ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc. * Apache-2.0 */ -import { getUser } from "../src/index"; +import { getUser, getUserNotifications } from "../src/index"; import { AnonUserResponse, GroupMemberUserResponse, - GroupAdminUserResponse + GroupAdminUserResponse, + UserNotificationsResponse } from "./mocks/responses"; import { encodeParam } from "@esri/arcgis-rest-request"; @@ -97,4 +98,42 @@ describe("users", () => { }); }); }); + + describe("getUserNotifications", () => { + const session = new UserSession({ + username: "c@sey", + password: "123456", + portal: "https://myorg.maps.arcgis.com/sharing/rest" + }); + + fetchMock.postOnce( + "https://myorg.maps.arcgis.com/sharing/rest/generateToken", + { + token: "token", + expires: TOMORROW.getTime(), + username: "c@sey" + } + ); + + session.refreshSession(); + + it("should make an authenticated request for user notifications", done => { + fetchMock.once("*", UserNotificationsResponse); + + getUserNotifications({ authentication: session }) + .then(response => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://myorg.maps.arcgis.com/sharing/rest/community/users/c%40sey/notifications?f=json&token=token" + ); + expect(options.method).toBe("GET"); + expect(response.notifications.length).toEqual(2); + done(); + }) + .catch(e => { + fail(e); + }); + }); + }); });