Commit d635a841 authored by Venkaiah Naidu Singamchetty's avatar Venkaiah Naidu Singamchetty

Merge branch 'feture/get-activities' into 'master'

Feture/get activities

See merge request !66
parents 9e79fdc2 4dda8c5c
...@@ -110,7 +110,7 @@ app.post("/getreportees",async (req, res) => { ...@@ -110,7 +110,7 @@ app.post("/getreportees",async (req, res) => {
], ],
}; };
aggre.push({ $match: orCondation }); aggre.push({ $match: orCondation });
aggre.push({$unset:"empIdString"});
query = Object.assign(query); query = Object.assign(query);
} }
aggre.push({ $sort: { [sortBy]: sortByOrder } }); aggre.push({ $sort: { [sortBy]: sortByOrder } });
...@@ -244,42 +244,58 @@ const calculateAverage = async(query) => { ...@@ -244,42 +244,58 @@ const calculateAverage = async(query) => {
{ {
"empId":41689, "empId":41689,
"fromDate":"2024-03-10", "fromDate":"2024-03-10",
"toDate":"2024-03-14" "toDate":"2024-03-14",
page:0
perPage:10,
} }
*/ */
app.post("/getActivities", async(req, res) => { app.post("/getActivities", async(req, res) => {
let { empId, fromDate, toDate, today } = req.body; let { empId,today } = req.body;
if (!empId || typeof empId == "string") { if (!empId || typeof empId == "string") {
res.status(401).json({ message: "Employee id is missing / EmpId should be string only" }); res.status(401).json({ message: "Employee id is missing / EmpId should be string only" });
return; return;
} else { } else {
let query = { let page = req.body.page ? parseInt(req.body.page) || 1 : 1;
empId: empId, let limit = req.body.perPage ? parseInt(req.body.perPage) || 10 : 10;
}; let skip = (page - 1) * limit || 0;
if (fromDate && toDate) {
fromDate = new Date(fromDate) //let query = { empId: empId};
toDate = new Date(toDate); let aggreGate = [ { $match:{empId: empId} } ];
let fromDate = moment().subtract(90, "days").toDate();
let toDate = moment().toDate()
if (req.body.fromDate && req.body.toDate) {
fromDate = new Date(req.body.fromDate);
toDate = new Date(req.body.toDate);
}
toDate.setHours(23); toDate.setHours(23);
toDate.setMinutes(59); toDate.setMinutes(59);
toDate.setSeconds(59); toDate.setSeconds(59);
query["activities.recorded_date"] = { // query["activities.recorded_date"] = {$gte: new Date(fromDate),$lte: new Date(toDate) };
$gte: new Date(fromDate), aggreGate.push({$match:{"activities.recorded_date": {$gte: new Date(fromDate),$lte: new Date(toDate) } } });
$lte: new Date(toDate), aggreGate.push({$unwind:"$activities" });
aggreGate.push({ $sort: { "activities.recorded_date": -1 } });
let facet = {
data: [{ $skip: skip }, { $limit: limit }],
totalCount: [{ $count: "count" }],
}; };
aggreGate.push({ $facet: facet });
aggreGate.push({ $unwind: { path: "$totalCount" } });
db.collection("performance_master")
.aggregate(aggreGate)
.toArray()
.then((result) => {
if (result && result.length) {
res.status(201).json({ ...result[0] });
} else { } else {
// If fromDate and toDate are not provided, fetch data for the last 90 days res.status(201).json({ data: [], totalCount: { count: 0 } });
query["activities.recorded_date"] = {
$gte: moment().subtract(90, "days").toDate(),
$lte: moment().toDate(),
};
} }
await db.collection("performance_master")
.findOne(query)
.then((results) => {
res.status(201).json(results);
}) })
.catch((error) => { .catch((error) => res.status(401).send(error));
res.status(401).json({ message: "Error fetching data" }, error);
});
} }
}); });
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment