Commit 70414ae9 authored by Venkaiah Naidu Singamchetty's avatar Venkaiah Naidu Singamchetty

Merge branch 'apiserver' into 'master'

Apiserver

See merge request !6
parents a6392805 d6513c10
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.3", "express": "^4.18.3",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"mongodb": "^6.5.0", "mongodb": "^6.5.0",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
...@@ -12793,6 +12795,14 @@ ...@@ -12793,6 +12795,14 @@
"mkdirp": "bin/cmd.js" "mkdirp": "bin/cmd.js"
} }
}, },
"node_modules/moment": {
"version": "2.30.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz",
"integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==",
"engines": {
"node": "*"
}
},
"node_modules/mongodb": { "node_modules/mongodb": {
"version": "6.5.0", "version": "6.5.0",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.5.0.tgz", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.5.0.tgz",
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
"axios": "^1.6.7", "axios": "^1.6.7",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.3", "express": "^4.18.3",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"mongodb": "^6.5.0", "mongodb": "^6.5.0",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
......
...@@ -18,25 +18,36 @@ connectToDb((err) => { ...@@ -18,25 +18,36 @@ connectToDb((err) => {
} }
}) })
//to get all the employees data
app.get('/employees', (req, res) => { app.get('/employees', (req, res) => {
db.collection('employees').find().toArray() db.collection('employees').find().toArray()
.then(result => { res.send(result) }) .then(result => { res.send(result) })
.catch(error => res.status(401).send(error)) .catch(error => res.status(401).send(error))
}) })
app.get('/activities', (req, res) => { //to get only individual employee data
db.collection('activities_master').find().toArray() app.get('/employee/:id', (req, res) => {
let Id = parseInt(req.params.id);
db.collection('employees').findOne({empId:Id},{projection:{_id:false}})
.then(result => { res.send(result) }) .then(result => { res.send(result) })
.catch(error => res.status(401).send(error)) .catch(error => res.status(401).send(error))
}) })
app.get('/employee/:id', (req, res) => { //to get activities to display
let Id = parseInt(req.params.id); app.get('/activities', (req, res) => {
db.collection('employees').findOne({empId:Id},{projection:{_id:false}}) db.collection('activities_master').find().toArray()
.then(result => { res.send(result) }) .then(result => { res.send(result) })
.catch(error => res.status(401).send(error)) .catch(error => res.status(401).send(error))
}) })
/*
Example of post Data
{
"reportees":[41689,41716,41710,41750,41751,41714],
"sort":{"type":"empId","order":-1}
,"page":1,"perPage":10,
"searchText":"eng"
}*/
app.post('/getreportees', (req, res) => { app.post('/getreportees', (req, res) => {
let reporteesArray = req.body.reportees || []; let reporteesArray = req.body.reportees || [];
let sortBy=req.body.sort?req.body.sort.type || "_id" :"_id" let sortBy=req.body.sort?req.body.sort.type || "_id" :"_id"
...@@ -55,69 +66,84 @@ app.post('/getreportees', (req, res) => { ...@@ -55,69 +66,84 @@ app.post('/getreportees', (req, res) => {
{"designation":searchStr} {"designation":searchStr}
] ]
}); });
}
console.log(query);
db.collection('employees').aggregate([
{ $match: query },
{
$facet: {
// Perform the main query to get paginated data
paginatedData: [
{ $skip: skip },
{ $limit: limit },
{ $sort: { [sortBy]: sortByOrder } },
{ $project: { _id: false } }
],
// Perform a separate query to get the total count of documents
totalCount: [
{ $count: "total" }
]
}
} }
]) db.collection('employees').find(query, { projection: { _id: false } })
.skip(skip)
.sort({[sortBy]:sortByOrder})
.limit(limit)
.toArray() .toArray()
.then(result => { .then(result => {
// Extract paginated data and total count from the result // Get the total count of data
const paginatedData = result[0].paginatedData; db.collection('employees').countDocuments(query)
const totalCount = result[0].totalCount[0].total; .then(totalCount => {
// Send the response with pagination details
res.send({ res.send({
totalCount: totalCount, totalcount: totalCount,
currentPage: page, currentPage: page,
totalPages: Math.ceil(totalCount / limit), totalPages: Math.ceil(totalCount / limit),
data: paginatedData, data: result,
}); });
}) })
.catch(error => res.status(401).send(error)); .catch(error => res.status(401).send(error));
})
.catch(error => res.status(401).send(error));
});
//Example of post Data
/*
{
"empId":10000,
"data":{
"aName":"timesheet",
"aId":"D001",
"type":"default",
"recorded_date":"2024-03-12",
"score":3,
"comments":"very good"
}
}
*/
app.post('/create-performance',(req,res)=>{
const empId = req.body.empId || null;
if(!empId){
res.status(401).json({"message":"Employee id is missing"});
return
}else{
let {data} = req.body;
data = {...data, "recorded_date": new Date(data['recorded_date']) };
let query = {empId:empId };
db.collection('performance_master').findOne(query).then((result)=>{
if(result){
db.collection('performance_master').updateOne(query,{ $push: { "activities":data } })
.then((updateRes)=>{
res.json({"reuslt":updateRes});
})
.catch((error)=>{
res.json({"error":error});
});
}else{
//create ne one
let insertData = { empId:empId, activities:[]};
insertData.activities.push(data);
db.collection('performance_master').insertOne(insertData).then((result)=>{
res.json({"result":result});
}).catch((error)=>{
res.json({"message":error})
// db.collection('employees').find(query, { projection: { _id: false } }) })
// .skip(skip) }
// .sort({[sortBy]:sortByOrder}) }).catch((error)=>{
// .limit(limit) console.log(error)
// .toArray() res.send(query)
// .then(result => { })
// // Get the total count of data
// db.collection('employees').countDocuments(query)
// .then(totalCount => { }
// res.send({
// totalcount: totalCount,
// currentPage: page, })
// totalPages: Math.ceil(totalCount / limit),
// data: result,
// });
// })
// .catch(error => res.status(401).send(error));
// })
// .catch(error => res.status(401).send(error));
});
......
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