-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (153 loc) Β· 4.65 KB
/
index.js
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
require("dotenv").config();
const functions = require("@google-cloud/functions-framework");
const { createClient } = require("@supabase/supabase-js");
const fetch = require("node-fetch");
const { WebClient } = require("@slack/web-api");
const SUPABASE_URL = process.env.SUPABASE_URL;
const SUPABASE_KEY = process.env.SUPABASE_KEY;
const USER_OAUTH_TOKEN_SLACK = process.env.USER_OAUTH_TOKEN_SLACK;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const slackWebClient = new WebClient(USER_OAUTH_TOKEN_SLACK);
function isWeekend(date) {
const dayOfWeek = date.getDay();
return dayOfWeek === 0 || dayOfWeek === 6;
}
function getTaskPointMessage(point) {
switch (point) {
case 1:
return "[Task Point: 1 π]";
case 2:
return "[Task Point: 2 π]";
case 3:
return "[Task Point: 3 π]";
case 5:
return "[Task Point: 5 π ]";
case 7:
return "[Task Point: 7 π€―]";
default:
return "";
}
}
async function sendMessageToSlack(req, res) {
let doing_content_block = "";
let done_content_block = "";
//* For in progress tasks
const response = await fetch(
`https://api.clickup.com/api/v2/team/${process.env.CLICK_UP_TEAM_ID}/task?statuses=in%20progress&statuses=in%20progress&assignees=${process.env.CLICK_UP_USER_ID}&assignees=${process.env.CLICK_UP_USER_ID}`,
{
method: "GET",
headers: {
"Content-Type": '"application/json"',
Authorization: process.env.CLICK_UP_API_KEY,
},
}
);
const inprogress_tasks = await response.json();
if (!inprogress_tasks.err && inprogress_tasks.tasks.length > 0) {
for (const task of inprogress_tasks.tasks) {
doing_content_block +=
"β’ " +
getTaskPointMessage(task.points) +
" " +
task.name +
"\n\n";
}
}
//* For done tasks
const today = new Date();
let past_day = new Date();
past_day.setDate(past_day.getDate() - 1);
const { data, error } = await supabase
.from("clickup-task-update-webhook")
.select("*")
.lt("created_at", today.toISOString())
.gt("created_at", past_day.toISOString());
for (const each of data) {
const response = await fetch(
`https://api.clickup.com/api/v2/task/${each.task_id}`,
{
method: "GET",
headers: {
"Content-Type": '"application/json"',
Authorization: process.env.CLICK_UP_API_KEY,
},
}
);
const task = await response.json();
if (!task.err && task.status.status === "done") {
done_content_block +=
"β’ " +
getTaskPointMessage(task.points) +
" " +
task.name +
"\n\n";
}
}
//* Create blocks message for Slack
const blocks = [
{
type: "section",
text: {
type: "mrkdwn",
text:
"β
DONE\n\n" +
done_content_block +
"\n\n\n ποΈ DOING\n\n" +
doing_content_block,
},
},
];
slackWebClient.chat.postMessage({
channel: "#daily-meeting",
blocks,
});
res.send("OK");
}
async function manageClickUpWebhook(req, res) {
if (req.method === "POST") {
const data = req.body;
if (data) {
const { event, history_items, task_id, webhook_id } = data;
if (event !== "taskStatusUpdated") {
console.log("This event does not support");
res.send("OK");
}
const { user, before, after } = history_items[0];
if (user.id === 3665453 && user.email === "[email protected]") {
if (after.status === "todo" || after.status === "done") {
const { data, error } = await supabase
.from("clickup-task-update-webhook")
.select("*")
.eq("task_id", task_id);
if (data.length > 0) {
if (after.status === "todo") {
await supabase
.from("clickup-task-update-webhook")
.delete()
.eq("task_id", task_id);
} else {
await supabase
.from("clickup-task-update-webhook")
.update({ status: after.status })
.eq("task_id", task_id);
}
} else if (after.status === "done") {
await supabase
.from("clickup-task-update-webhook")
.insert({ task_id, event, webhook_id, status: after.status });
}
} else {
console.log("This status we don't care about");
res.send("OK");
}
}
}
}
res.send("OK");
}
functions.http("sendMessageToSlack", sendMessageToSlack);
functions.http("manageClickUpWebhook", manageClickUpWebhook);
module.exports = {
sendMessageToSlack,
manageClickUpWebhook,
};