Skip to content

Commit

Permalink
feat(users): adds removeNotification function
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-users
  • Loading branch information
mjuniper committed Sep 6, 2018
1 parent 741d3fe commit b4a55d0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
26 changes: 21 additions & 5 deletions packages/arcgis-rest-users/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export interface INotification {
data: { [key: string]: any };
}

export interface INotificationIdRequestOptions extends IUserRequestOptions {
/**
* Unique identifier of the item.
*/
id: string;
}
export interface INotificationResult {
notifications: INotification[];
}
Expand All @@ -91,15 +97,25 @@ export interface INotificationResult {
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`;
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);
}

export function removeNotification(
requestOptions: INotificationIdRequestOptions
): Promise<any> {
const username = encodeURIComponent(requestOptions.authentication.username);
const portalUrl = getPortalUrl(requestOptions);
const url = `${portalUrl}/content/users/${username}/notifications/${
requestOptions.id
}/delete`;

return request(url, requestOptions);
}
5 changes: 5 additions & 0 deletions packages/arcgis-rest-users/test/mocks/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,8 @@ export const UserNotificationsResponse: INotificationResult = {
}
]
};

export const IDeleteSuccessResponse: any = {
success: true,
notificationId: "3ef"
};
35 changes: 31 additions & 4 deletions packages/arcgis-rest-users/test/users.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { getUser, getUserNotifications } from "../src/index";
import {
getUser,
getUserNotifications,
removeNotification
} from "../src/index";

import {
AnonUserResponse,
GroupMemberUserResponse,
GroupAdminUserResponse,
UserNotificationsResponse
UserNotificationsResponse,
IDeleteSuccessResponse
} from "./mocks/responses";

import { encodeParam } from "@esri/arcgis-rest-request";
Expand Down Expand Up @@ -109,7 +114,7 @@ describe("users", () => {
fetchMock.postOnce(
"https://myorg.maps.arcgis.com/sharing/rest/generateToken",
{
token: "token",
token: "fake-token",
expires: TOMORROW.getTime(),
username: "c@sey"
}
Expand All @@ -125,7 +130,7 @@ describe("users", () => {
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"
"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);
Expand All @@ -135,5 +140,27 @@ describe("users", () => {
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/content/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);
});
});
});
});

0 comments on commit b4a55d0

Please sign in to comment.