Skip to content

Commit

Permalink
Merge pull request #31 from tago-io/notification/refactor
Browse files Browse the repository at this point in the history
CORE-90 updated notification interfaces and new routes
  • Loading branch information
matheuslbenachio authored Jul 22, 2021
2 parents 37f8a73 + b3d2659 commit 366ef32
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tago-io/sdk",
"version": "10.3.7",
"version": "10.3.8",
"description": "TagoIO SDK for JavaScript in the browser and Node.js",
"author": "Tago LLC",
"homepage": "https://tago.io",
Expand Down
42 changes: 32 additions & 10 deletions src/modules/Account/Notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,55 @@ class Notifications extends TagoIOModule<GenericModuleParams> {
method: "PUT",
body: {
notification_ids: notificationIDS,
read: true,
},
});

return result;
}

/**
* Accept a notification
* @param notificationID Notification identification
* Mark notifications as unread
* @param notificationIDS An array of ids or a single id
*/
public async accept(notificationID: GenericID): Promise<string> {
public async markAsUnread(notificationIDS: GenericID[] | GenericID): Promise<string> {
if (!Array.isArray(notificationIDS)) {
notificationIDS = [notificationIDS];
}

const result = await this.doRequest<string>({
path: `/notification/accept/${notificationID}`,
method: "POST",
path: "/notification/read",
method: "PUT",
body: {
notification_ids: notificationIDS,
read: false,
},
});

return result;
}

/**
* Refuse a notification
* @param notificationID Notification identification
* Mark all notifications as read
*/
public async refuse(notificationID: GenericID): Promise<string> {
public async markAllAsRead(): Promise<string> {
const result = await this.doRequest<string>({
path: `/notification/refuse/${notificationID}`,
method: "POST",
path: "/notification/markallread",
method: "PUT",
});

return result;
}

/**
* Acknowledge notification button pressed
* @param notificationID ID of the notification
* @param buttonID ID of the button
*/
public async notificationButton(notificationID: GenericID, buttonID: string): Promise<string> {
const result = await this.doRequest<string>({
path: `/notification/${notificationID}/${buttonID}`,
method: "PUT",
});

return result;
Expand Down
10 changes: 5 additions & 5 deletions src/modules/Account/Run.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GenericID, RecursivePartial } from "../../common/common.types";
import TagoIOModule, { GenericModuleParams } from "../../common/TagoIOModule";
import dateParser from "../Utils/dateParser";
import { NotificationCreate, NotificationInfo } from "./notifications.types";
import {
LoginResponse,
NotificationCreateInfo,
RunInfo,
UserCreateInfo,
UserInfo,
Expand Down Expand Up @@ -113,16 +113,16 @@ class Run extends TagoIOModule<GenericModuleParams> {
return result;
}

public async notificationList(userID: GenericID): Promise<NotificationCreateInfo[]> {
const result = await this.doRequest<NotificationCreateInfo[]>({
public async notificationList(userID: GenericID): Promise<NotificationInfo[]> {
const result = await this.doRequest<NotificationInfo[]>({
path: `/run/notification/${userID}`,
method: "GET",
});

return result;
}

public async notificationCreate(userID: GenericID, data: NotificationCreateInfo): Promise<{ id: GenericID }> {
public async notificationCreate(userID: GenericID, data: NotificationCreate): Promise<{ id: GenericID }> {
const result = await this.doRequest<{ id: GenericID }>({
path: `/run/notification/`,
method: "POST",
Expand All @@ -135,7 +135,7 @@ class Run extends TagoIOModule<GenericModuleParams> {
return result;
}

public async notificationEdit(notificationID: GenericID, data: Partial<NotificationCreateInfo>): Promise<string> {
public async notificationEdit(notificationID: GenericID, data: Partial<NotificationCreate>): Promise<string> {
const result = await this.doRequest<string>({
path: `/run/notification/${notificationID}`,
method: "PUT",
Expand Down
67 changes: 48 additions & 19 deletions src/modules/Account/notifications.types.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
import { GenericID } from "../../common/common.types";
import { GenericID, Query } from "../../common/common.types";

type NotificationType = "dashboard" | "bucket" | "analysis" | "profile" | "tago" | "limit_alert";

type Condition = "None" | "Pending" | "Accepted" | "Refused";
interface NotificationTriggerAnalysis {
analysis_id: GenericID;
}
interface NotificationTriggerHTTP {
url: string;
method: "POST" | "GET" | "PUT" | "DELETE" | "REDIRECT";
body: { [key: string]: any };
}
interface NotificationTriggerProfile {
share_profile: "accept" | "refuse";
}
interface NotificationButton {
id: string;
label: string;
color?: string;
triggers: (NotificationTriggerAnalysis | NotificationTriggerHTTP | NotificationTriggerProfile)[];
}

interface NotificationQuery {
type: NotificationType;
start_date: Date;
end_date: Date;
ref_id: GenericID;
interface NotificationIconImage {
image_url: string;
bg_color?: HexColor;
fit?: "fill" | "contain" | "cover";
}

interface NotificationInfo {
id: GenericID;
ref_id: GenericID | null;
ref_from: { id: GenericID; name: string };
type: NotificationType;
sub_type: string;
type HexColor = string;
interface NotificationIconSVG {
svg_url: string;
svg_color?: HexColor;
bg_color?: HexColor;
}
interface NotificationCreate {
title: string;
message: string;
read: boolean;
condition: Condition;
created_at: Date;
read?: boolean;
icon?: NotificationIconSVG | NotificationIconImage;
buttons?: NotificationButton[];
buttons_enabled?: boolean;
buttons_autodisable?: boolean;
}
type NotificationQuery = Query<{ read: boolean }, "created_at">;
type NotificationInfo = { id: GenericID; created_at: Date } & Required<NotificationCreate>;

export { NotificationQuery, NotificationInfo, NotificationType };
export {
NotificationCreate,
NotificationQuery,
NotificationInfo,
NotificationButton,
NotificationTriggerProfile,
NotificationTriggerHTTP,
NotificationTriggerAnalysis,
NotificationIconImage,
NotificationIconSVG,
};
21 changes: 1 addition & 20 deletions src/modules/Account/run.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExpireTimeOption, GenericID, GenericToken, Query, TagsObj } from "../../common/common.types";
import { NotificationButton, NotificationCreate } from "./notifications.types";

interface RunInfo {
profile: GenericID;
Expand Down Expand Up @@ -89,25 +90,6 @@ interface LoginResponse {
expire_date: ExpireTimeOption;
}

interface NotificationCreateInfo {
title: string;
message: string;
buttons?: NotificationButton[];
/** Auto disable all buttons when one is clicked. */
button_autodisable?: boolean;
}

interface NotificationButton {
label: string;
/** ID is sent back to analysis if any analysis is set to run */
id: string;
color?: string;
/** Analysis to run when button is clicked. */
analysis?: GenericID;
/** URL to redirect to when button is clicked. */
url?: string;
}

interface LoginAsUserOptions {
/**
* Date to expire the login token.
Expand Down Expand Up @@ -289,7 +271,6 @@ export {
UserCreateInfo,
UserInfo,
LoginResponse,
NotificationCreateInfo,
UserQuery,
LoginAsUserOptions,
RunSAMLInfo,
Expand Down
55 changes: 42 additions & 13 deletions src/modules/RunUser/RunUser.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { GenericID, GenericToken } from "../../common/common.types";
import TagoIOModule, { doRequestParams, GenericModuleParams } from "../../common/TagoIOModule";
import { Regions } from "../../regions";
import { NotificationInfo, NotificationQuery } from "../Account/notifications.types";
import dateParser from "../Utils/dateParser";
import {
RunNotificationInfo,
RunUserCreateInfo,
RunUserCreate,
RunUserInfo,
RunUserLogin,
RunUserLoginResponse,
} from "./runUser.types";
import { RunUserCreateInfo, RunUserCreate, RunUserInfo, RunUserLogin, RunUserLoginResponse } from "./runUser.types";
import SDB from "./SDB";

class RunUser extends TagoIOModule<GenericModuleParams> {
Expand Down Expand Up @@ -144,13 +138,13 @@ class RunUser extends TagoIOModule<GenericModuleParams> {
* List notifications.
* @param tagoIORunURL TagoIO Run url without http
*/
public async notificationList(tagoIORunURL: string): Promise<RunNotificationInfo[]> {
let result = await this.doRequest<RunNotificationInfo[]>({
public async notificationList(tagoIORunURL: string, queryObj?: NotificationQuery): Promise<NotificationInfo[]> {
let result = await this.doRequest<NotificationInfo[]>({
path: `/run/${tagoIORunURL}/notification`,
method: "GET",
params: queryObj,
});

result = result.map((data) => dateParser(data, ["created_at", "updated_at"]));
result = result.map((data) => dateParser(data, ["created_at"]));

return result;
}
Expand All @@ -170,12 +164,47 @@ class RunUser extends TagoIOModule<GenericModuleParams> {
method: "PUT",
body: {
notification_ids: notificationIDs,
read: true,
},
});

return result;
}

/**
* Mark notification as unread
* @param tagoIORunURL TagoIO Run url without http
* @param notificationIDs array of notification ids or a single id
*/
public async notificationMarkUnread(tagoIORunURL: string, notificationIDs: GenericID | GenericID[]): Promise<string> {
if (!Array.isArray(notificationIDs)) {
notificationIDs = [notificationIDs];
}

const result = await this.doRequest<string>({
path: `/run/${tagoIORunURL}/notification`,
method: "PUT",
body: {
notification_ids: notificationIDs,
read: false,
},
});

return result;
}

/**
* Mark all notifications as read
* @param tagoIORunURL TagoIO Run url without http
*/
public async notificationMarkAllRead(tagoIORunURL: string): Promise<string> {
const result = await this.doRequest<string>({
path: `/run/${tagoIORunURL}/notification/markallread`,
method: "PUT",
});

return result;
}
/**
* Trigger notification button
* @param tagoIORunURL TagoIO Run url without http
Expand All @@ -184,7 +213,7 @@ class RunUser extends TagoIOModule<GenericModuleParams> {
*/
public async notificationButton(tagoIORunURL: string, notificationID: GenericID, buttonID: GenericID): Promise<any> {
const result = await this.doRequest<any>({
path: `/run/${tagoIORunURL}/notification${notificationID}/${buttonID}`,
path: `/run/${tagoIORunURL}/notification/${notificationID}/${buttonID}`,
method: "PUT",
});

Expand Down
18 changes: 2 additions & 16 deletions src/modules/Services/Notification.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import TagoIOModule, { GenericModuleParams } from "../../common/TagoIOModule";
import { GenericID } from "../../common/common.types";

interface NotificationData {
/**
* topic of the message
*/
title: string;
/**
* Message scope
*/
message: string;
/**
* Dashboard/Bucket ID for "Go To" button.
*/
ref_id?: GenericID;
}
import { NotificationCreate } from "../Account/notifications.types";

class Notification extends TagoIOModule<GenericModuleParams> {
/**
Expand All @@ -24,7 +10,7 @@ class Notification extends TagoIOModule<GenericModuleParams> {
* Any account with share of the dashboard/bucket will receive too.
* @param notification Notification Object
*/
public async send(notification: NotificationData): Promise<string> {
public async send(notification: NotificationCreate): Promise<string> {
const result = await this.doRequest<string>({
path: "/analysis/services/notification/send",
method: "POST",
Expand Down

0 comments on commit 366ef32

Please sign in to comment.