-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
193 lines (175 loc) · 5.81 KB
/
node.js
File metadata and controls
193 lines (175 loc) · 5.81 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
187
188
189
190
191
192
193
const express = require("express");
const app = express();
const PORT = process.env.PORT || 23000;
const mongoose = require("mongoose");
const axios = require("axios");
const cron = require("node-cron");
require("dotenv").config();
const MONGO_URL =
process.env.MONGO_URI || "mongodb://localhost:27017/slack-leave-management";
mongoose
.connect(MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.error("Error connecting to MongoDB:", err));
const leaveSchema = new mongoose.Schema({
user: String,
dates: { type: [Date], required: true },
reason: String,
status: { type: String, default: "Pending" },
leaveType: { type: String, required: true },
leaveDay: { type: [String], required: true },
leaveTime: { type: [String], required: true },
});
const userSchema = new mongoose.Schema({
username: { type: String, required: true },
slackId: { type: String, required: true },
sickLeave: { type: Number, default: 0 },
restrictedHoliday: { type: Number, default: 0 },
burnout: { type: Number, default: 0 },
mensuralLeaves: { type: Number, default: 0 },
casualLeave: { type: Number, default: 0 },
maternityLeave: { type: Number, default: 0 },
unpaidLeave: { type: Number, default: 0 },
paternityLeave: { type: Number, default: 0 },
bereavementLeave: { type: Number, default: 0 },
wfhLeave: { type: Number, default: 0 },
internshipLeave: { type: Number, default: 0 },
});
const Leave = mongoose.model("Leave", leaveSchema);
const User = mongoose.model("User", userSchema);
app.use(express.json());
app.get("/leaves", async (req, res) => {
try {
const today = new Date();
const startOfToday = new Date(today);
startOfToday.setHours(0, 0, 0, 0);
const endOfToday = new Date(today);
endOfToday.setHours(23, 59, 59, 999);
const leaves = await Leave.find({
dates: { $gte: startOfToday, $lte: endOfToday },
status: "Approved",
})
.select("user dates reason status leaveType leaveDay leaveTime")
.exec();
if (leaves.length === 0) {
return res.status(404).json({
message: "No approved leaves found",
details:
"There are currently no approved leave records in the database for the next 7 days",
});
}
const formattedLeaves = await Promise.all(
leaves.map(async (leave) => {
const user = await User.findOne({ slackId: leave.user }).exec();
return {
...leave._doc,
user: user ? user.username : leave.user,
dates: leave.dates.map((date) => date.toISOString()),
};
})
);
res.status(200).json(formattedLeaves);
} catch (error) {
res
.status(500)
.json({ message: "Error fetching leaves", error: error.message });
}
});
async function sendSlackNotification(leaves) {
const numberOfPeople = leaves.length;
const headingText =
numberOfPeople === 1
? "1 person is on holiday"
: `${numberOfPeople} people are on Leave`;
const blocks = [
{
type: "section",
text: {
type: "mrkdwn",
text: `*${headingText}*`,
},
},
...(await Promise.all(
leaves.map(async (leave) => {
const user = await User.findOne({ slackId: leave.user }).exec();
const username = user ? user.username : leave.user;
let text;
if (
[
"Burnout_Leave",
"Bereavement_Leave",
"Maternity_Leave",
"Paternity_Leave",
"Compensatory_Leave",
"Work_from_Home",
].includes(leave.leaveType)
) {
const fromDate = new Date(leave.dates[0]);
const toDate = new Date(leave.dates[leave.dates.length - 1]);
text = `*${username}*:\n From date: ${fromDate.getDate()} ${fromDate
.toLocaleString("default", { month: "short" })
.toLowerCase()} \n To date: ${toDate.getDate()} ${toDate
.toLocaleString("default", { month: "short" })
.toLowerCase()}`;
} else {
text = `*${username}*:\n${leave.dates
.map((date, index) => {
const formattedDate = new Date(date);
return `${formattedDate.getDate()} ${formattedDate
.toLocaleString("default", { month: "short" })
.toLowerCase()} (${leave.leaveDay[index]})`;
})
.join("\n")}`;
}
return {
type: "section",
text: {
type: "mrkdwn",
text: text,
},
};
})
)),
];
try {
await axios.post(process.env.SLACK_API_URL, { blocks });
console.log("Slack notification sent.");
} catch (error) {
console.error("Error sending Slack message:", error.message);
}
}
cron.schedule("0 9 * * 1-5", async () => {
console.log("Running scheduled task to send Slack notifications...");
const today = new Date();
const startOfToday = new Date(today);
startOfToday.setHours(0, 0, 0, 0);
const endOfToday = new Date(today);
endOfToday.setHours(23, 59, 59, 999);
const leaves = await Leave.find({
dates: { $gte: startOfToday, $lte: endOfToday },
status: "Approved",
})
.select("user dates reason status leaveType leaveDay leaveTime")
.exec();
if (leaves.length > 0) {
const formattedLeaves = await Promise.all(
leaves.map(async (leave) => {
const user = await User.findOne({ slackId: leave.user }).exec();
return {
...leave._doc,
user: user ? user.username : leave.user,
dates: leave.dates.map((date) => date.toISOString()),
};
})
);
sendSlackNotification(formattedLeaves);
} else {
console.log("No approved leaves found for the next 7 days.");
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});