-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCommandUtility.ts
More file actions
186 lines (178 loc) · 5.53 KB
/
Copy pathCommandUtility.ts
File metadata and controls
186 lines (178 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { NotionApp } from "../../NotionApp";
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
import {
IHttp,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import {
ICommandUtility,
ICommandUtilityParams,
} from "../../definition/command/ICommandUtility";
import { CommandParam, SubCommandParam } from "../../enum/CommandParam";
import { Handler } from "../handlers/Handler";
import { sendHelperNotification } from "../helper/message";
export class CommandUtility implements ICommandUtility {
public app: NotionApp;
public params: Array<string>;
public sender: IUser;
public room: IRoom;
public read: IRead;
public modify: IModify;
public http: IHttp;
public persis: IPersistence;
public triggerId?: string;
public threadId?: string;
constructor(props: ICommandUtilityParams) {
this.app = props.app;
this.params = props.params;
this.sender = props.sender;
this.room = props.room;
this.read = props.read;
this.modify = props.modify;
this.http = props.http;
this.persis = props.persis;
this.triggerId = props.triggerId;
this.threadId = props.threadId;
}
public async resolveCommand(): Promise<void> {
const handler = new Handler({
app: this.app,
sender: this.sender,
room: this.room,
read: this.read,
modify: this.modify,
http: this.http,
persis: this.persis,
triggerId: this.triggerId,
threadId: this.threadId,
});
switch (this.params.length) {
case 0: {
await sendHelperNotification(
this.read,
this.modify,
this.sender,
this.room
);
break;
}
case 1: {
await this.handleSingleParam(handler);
break;
}
case 2: {
await this.handleDualParam(handler);
break;
}
default: {
await sendHelperNotification(
this.read,
this.modify,
this.sender,
this.room
);
}
}
}
private async handleSingleParam(handler: Handler): Promise<void> {
const oAuth2ClientInstance = await this.app.getOAuth2Client();
switch (this.params[0].toLowerCase()) {
case CommandParam.CONNECT: {
await oAuth2ClientInstance.connect(
this.room,
this.sender,
this.read,
this.modify,
this.http,
this.persis
);
break;
}
case CommandParam.DISCONNECT: {
await oAuth2ClientInstance.disconnect(
this.room,
this.sender,
this.read,
this.modify,
this.http,
this.persis
);
break;
}
case CommandParam.COMMENT: {
await handler.commentOnPages();
break;
}
case CommandParam.CREATE: {
await handler.createNotionPageOrRecord();
break;
}
case CommandParam.WS:
case CommandParam.WORKSPACE: {
await handler.changeNotionWorkspace();
break;
}
case CommandParam.SHARE: {
await handler.shareNotionPage();
break;
}
case CommandParam.VIEW: {
await handler.viewNotionTable();
break;
}
case CommandParam.HELP:
default: {
await sendHelperNotification(
this.read,
this.modify,
this.sender,
this.room
);
break;
}
}
}
private async handleDualParam(handler: Handler): Promise<void> {
const [param, subparam] = this.params;
switch (param.toLowerCase()) {
case CommandParam.CREATE: {
if (subparam.toLowerCase() === SubCommandParam.DATABASE) {
await handler.createNotionDatabase();
return;
}
await sendHelperNotification(
this.read,
this.modify,
this.sender,
this.room
);
break;
}
case CommandParam.UPDATE: {
if (subparam.toLowerCase() === SubCommandParam.RECORD) {
await handler.updateDatabaseRecord();
return;
}
await sendHelperNotification(
this.read,
this.modify,
this.sender,
this.room
);
break;
}
default: {
await sendHelperNotification(
this.read,
this.modify,
this.sender,
this.room
);
break;
}
}
}
}