diff --git a/packages/arcgis-rest-users/src/users.ts b/packages/arcgis-rest-users/src/users.ts index 875211cf8a..6edd178074 100644 --- a/packages/arcgis-rest-users/src/users.ts +++ b/packages/arcgis-rest-users/src/users.ts @@ -68,3 +68,60 @@ export function getUser( // send the request return request(url, options); } + +export interface INotification { + id: string; + type: string; + target: string; + targetType: string; + received: number; + data: { [key: string]: any }; +} + +export interface INotificationIdRequestOptions extends IUserRequestOptions { + /** + * Unique identifier of the item. + */ + id: string; +} +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: IUserRequestOptions +): Promise { + let options = { httpMethod: "GET" } as IUserRequestOptions; + + const username = encodeURIComponent(requestOptions.authentication.username); + const portalUrl = getPortalUrl(requestOptions); + const url = `${portalUrl}/community/users/${username}/notifications`; + options = { ...requestOptions, ...options }; + + // send the request + return request(url, options); +} + +/** + * Delete a notification. + * + * @param requestOptions - Options for the request + * @returns A Promise that will resolve with the success/failure status of the request + */ +export function removeNotification( + requestOptions: INotificationIdRequestOptions +): Promise { + const username = encodeURIComponent(requestOptions.authentication.username); + const portalUrl = getPortalUrl(requestOptions); + const url = `${portalUrl}/community/users/${username}/notifications/${ + requestOptions.id + }/delete`; + + return request(url, requestOptions); +} diff --git a/packages/arcgis-rest-users/test/mocks/responses.ts b/packages/arcgis-rest-users/test/mocks/responses.ts index e31d3a6ccb..8cebbb092a 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,36 @@ 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" + } + } + ] +}; + +export const IDeleteSuccessResponse: any = { + success: true, + notificationId: "3ef" +}; diff --git a/packages/arcgis-rest-users/test/users.test.ts b/packages/arcgis-rest-users/test/users.test.ts index 3bd74dac90..f57a23980b 100644 --- a/packages/arcgis-rest-users/test/users.test.ts +++ b/packages/arcgis-rest-users/test/users.test.ts @@ -1,12 +1,18 @@ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc. * Apache-2.0 */ -import { getUser } from "../src/index"; +import { + getUser, + getUserNotifications, + removeNotification +} from "../src/index"; import { AnonUserResponse, GroupMemberUserResponse, - GroupAdminUserResponse + GroupAdminUserResponse, + UserNotificationsResponse, + IDeleteSuccessResponse } from "./mocks/responses"; import { encodeParam } from "@esri/arcgis-rest-request"; @@ -97,4 +103,64 @@ 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: "fake-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=fake-token" + ); + expect(options.method).toBe("GET"); + expect(response.notifications.length).toEqual(2); + done(); + }) + .catch(e => { + fail(e); + }); + }); + + it("should remove a notification", done => { + fetchMock.once("*", IDeleteSuccessResponse); + + removeNotification({ id: "3ef", ...{ 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/3ef/delete" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain(encodeParam("f", "json")); + expect(options.body).toContain(encodeParam("token", "fake-token")); + expect(response.success).toEqual(true); + expect(response.notificationId).toBe("3ef"); + done(); + }) + .catch(e => { + fail(e); + }); + }); + }); });