-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (141 loc) · 5 KB
/
index.js
File metadata and controls
178 lines (141 loc) · 5 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
import Fastify from "fastify";
import dotenv from "dotenv";
import {google} from "googleapis";
import fs from "fs-extra";
import FolderQueue from "./lib/drive.js";
import {getOrCreateChatRoom, addMemberToSpace, getSpace, findSpace } from "./lib/chat.js";
import {jigFetch} from "./lib/jig.js";
dotenv.config();
const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENTID,
process.env.GOOGLE_CLIENTSECRET,
"https://jig.bebot.chat/oauth2callback"
);
let stored = fs.readJSONSync("./bebot.tokens.json");
oauth2Client.setCredentials(stored);
oauth2Client.on('tokens', (tokens) => {
let stored = fs.readJSONSync("./bebot.tokens.json");
if (tokens.refresh_token) {
stored.refresh_token = tokens.refresh_token;
}
stored.access_token = tokens.access_token
fs.writeJSONSync("./bebot.tokens.json", stored);
});
const queue = new FolderQueue();
const fastify = Fastify({
logger: {
level: 'info',
file: './logs/request.log' // Will use pino.destination()
},
bodyLimit: 8388608,
http2SessionTimeout: 0
});
fastify.addHook('onRequest', (request, reply, done) => {
console.log("Request received");
done();
})
/** Establish Oauth2 BEBot user credentials
* https://developers.google.com/identity/protocols/oauth2/web-server#node.js
* https://developers.google.com/identity/protocols/oauth2/scopes#chat
* This is only for the BEBot user. We require user access to create chat rooms
* and add users to the rooms. Service accounts are not an option
*/
fastify.get("/auth", async function (request, reply) {
const url = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: ["https://www.googleapis.com/auth/chat.spaces https://www.googleapis.com/auth/chat.memberships https://www.googleapis.com/auth/drive"]
});
reply.header('Content-Type', 'text/html');
reply.send(`<a href="${url}">Authorize BeBot user</a>`);
});
fastify.get("/oauth2callback", async function (request, reply) {
const {tokens} = await oauth2Client.getToken(request.query.code);
oauth2Client.setCredentials(tokens);
reply.send("Success");
});
fastify.post("/activity/add", async function (request, reply) {
console.log(request.body.data);
// request.body.data.project.forEach(async project => {
// // Get or create the client and project folders
// queue.add(project);
// // Get or create chat room for the project
// getOrCreateChatRoom(project);
// });
reply.send("Success");
});
fastify.post("/project/add", async function (request, reply) {
request.body.data.project.forEach(async project => {
if (!project.projectFullName.startsWith("Oppty")) {
// Get or create the client and project folders
queue.add(project);
// Get or create chat room for the project
getOrCreateChatRoom(project);
}
});
reply.send("Success");
});
fastify.post("/project/status", async function (request, reply) {
request.body.data.project.forEach(async project => {
if (!project.projectFullName.startsWith("Oppty")) {
// Get or create the client and project folders
queue.add(project);
// Get or create chat room for the project
getOrCreateChatRoom(project);
}
});
reply.send("Success");
});
fastify.post("/task/user/add", async function (request, reply) {
console.log("Task user add");
let project = await jigFetch(`projects?projectKey=${request.body.data.task.projectKey}`);
let space = await findSpace(project.data.project[0].projectFullName);
if (space) {
let team = [];
// get the list of task tsers
request.body.data.task.taskUser.forEach(user => {
team.push(user.userID);
});
// Add the list of task detail users
request.body.data.task.details.forEach(detail => {
team.push(detail.userID);
});
// Remove duplicates
team = [...new Set(team)];
// Add each member to the space
// Could probably get a list of people in the room already and eliminate unnecessary calls
await asyncForEach(team, async (user) => {
if(user) {
await addMemberToSpace(space.name, user);
}
});
}
reply.send("Success");
});
fastify.listen({ port: process.env.PORT || 3001 }, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
// Server is now listening on ${address}
});
async function getActivityDetail(activityKey) {
let response = await fetch(`https://app21.workamajig.com/api/beta1/activities?activityKey=${activityKey}`, {
});
}
( async () => {
})();
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
// queue.add({
// "accountManagerKey": "U2E1SlhXeXFFNXU3YkpGKy9PN2FXZz090",
// "accountManagerName": "Jonathan Fisher",
// "clientName": "Pinnacle Engineering",
// "projectName": "PINN New Corporate Website",
// "projectFullName": "23-PINN-10023 PINN New Corporate Website",
// "accountManager": "Jonathan Fisher",
// "projectType": "Website",
// "projectTypeName": "Website"
// });