slack.ctrl.js
2.14 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
const User = require("../../models/user");
const sendSlack = require("../../util/sendSlack");
const problem_set = require("../../data/problem_set");
const compareBJ = require("../../util/compareBJ");
/*
POST api/notify/slack/goal
{
username: "username"
}
*/
exports.slackGoal = async (ctx) => {
try {
const { username } = ctx.request.body;
const profile = await User.findByUsername(username);
if (!profile) {
ctx.status = 401;
return;
}
let slackURL = profile.getslackURL();
if (!slackURL) {
ctx.status = 401;
return;
}
let goalNum = profile.getgoalNum();
let todayNum = profile.getTodaySovled();
let message = "";
if (goalNum < todayNum) {
message =
"오늘의 목표 " +
goalNum +
"문제 중 " +
todayNum +
"문제를 풀었습니다." +
"\n" +
"잘하셨습니다!";
} else {
message =
"오늘의 목표 " +
goalNum +
"문제 중 " +
todayNum +
"문제를 풀었습니다." +
"\n" +
"분발하세요!";
}
sendSlack.send(message, slackURL);
} catch (e) {
ctx.throw(500, e);
}
};
/*
POST api/notify/slack/recommend
{
username: "username"
}
*/
exports.slackRecommend = async (ctx) => {
try {
console.log("1");
const { username } = ctx.request.body;
const profile = await User.findByUsername(username);
if (!profile) {
ctx.status = 401;
return;
}
let slackURL = profile.getslackURL();
if (!slackURL) {
ctx.status = 401;
return;
}
let unsolved_data = compareBJ.compareBJ(
profile.getBJdata(),
problem_set.problem_set
);
let recommendData = compareBJ.randomItem(unsolved_data);
if (!recommendData) {
ctx.status = 401;
return;
}
let message =
"오늘의 추천 문제는 " +
recommendData.problem_number +
"번 " +
" <https://www.boj.kr/" +
recommendData.problem_number +
"|" +
recommendData.problem_title +
">" +
" 입니다.";
sendSlack.send(message, slackURL);
} catch (e) {
ctx.throw(500, e);
}
};