Skip to content

Commit

Permalink
Merge 33cea77 into af61e3e
Browse files Browse the repository at this point in the history
  • Loading branch information
mjuniper committed Aug 23, 2018
2 parents af61e3e + 33cea77 commit 5b98a4f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
35 changes: 35 additions & 0 deletions packages/arcgis-rest-users/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,38 @@ 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 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<INotificationResult> {
let url;
let options = { httpMethod: "GET" } as IUserRequestOptions;

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
@@ -1,4 +1,5 @@
import { IUser } from "@esri/arcgis-rest-common-types";
import { INotificationResult } from "../../src/users";

export const AnonUserResponse: IUser = {
username: "jsmith",
Expand Down Expand Up @@ -168,3 +169,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,9 +1,10 @@
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 @@ -94,4 +95,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 5b98a4f

Please sign in to comment.