Skip to content

Commit

Permalink
feat(users): add getUserNotifications function
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-common-types
@esri/arcgis-rest-users
  • Loading branch information
mjuniper committed Sep 6, 2018
1 parent da410d3 commit 9fbc5e2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/arcgis-rest-common-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
34 changes: 33 additions & 1 deletion packages/arcgis-rest-users/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<INotificationResult> {
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);
}
29 changes: 29 additions & 0 deletions packages/arcgis-rest-users/test/mocks/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
]
};
43 changes: 41 additions & 2 deletions packages/arcgis-rest-users/test/users.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
});
});
});
});

0 comments on commit 9fbc5e2

Please sign in to comment.