Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(users): add getUserNotifications function #294

Merged
merged 6 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 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,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<INotificationResult> {
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<any> {
const username = encodeURIComponent(requestOptions.authentication.username);
const portalUrl = getPortalUrl(requestOptions);
const url = `${portalUrl}/community/users/${username}/notifications/${
requestOptions.id
}/delete`;

return request(url, requestOptions);
}
34 changes: 34 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,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"
};
70 changes: 68 additions & 2 deletions packages/arcgis-rest-users/test/users.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
});
});
});
});