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

Merge branch 'apiserver' into 'master'

deployement-done

See merge request !30
parents da49cde9 8901c101
...@@ -19,15 +19,15 @@ connectToDb((err) => { ...@@ -19,15 +19,15 @@ connectToDb((err) => {
}); });
//to get all the employees data //to get all the employees data
// app.get("/employees", (req, res) => { app.get("/employees", (req, res) => {
// db.collection("employees") db.collection("employees")
// .find() .find()
// .toArray() .toArray()
// .then((result) => { .then((result) => {
// res.send(result); res.send(result);
// }) })
// .catch((error) => res.status(401).send(error)); .catch((error) => res.status(401).send(error));
// }); });
//to get only individual employee data //to get only individual employee data
app.get("/employee/:id", (req, res) => { app.get("/employee/:id", (req, res) => {
...@@ -44,6 +44,8 @@ app.get("/employee/:id", (req, res) => { ...@@ -44,6 +44,8 @@ app.get("/employee/:id", (req, res) => {
}); });
//login Check //login Check
//require empId
//example {empId:41689}
app.post('/login', async (req, res) => { app.post('/login', async (req, res) => {
const { empId } = req.body; const { empId } = req.body;
try { try {
...@@ -58,7 +60,7 @@ app.post('/login', async (req, res) => { ...@@ -58,7 +60,7 @@ app.post('/login', async (req, res) => {
} }
} }
catch (error) { catch (error) {
res.status(500).json({ error: 'Internal server error', details: error.message }); res.status(401).json({ error: 'Internal server error', details: error.message });
} }
}); });
...@@ -130,7 +132,7 @@ app.post("/getreportees", (req, res) => { ...@@ -130,7 +132,7 @@ app.post("/getreportees", (req, res) => {
//Example of post Data //Example of post Data
/* /*
{ {
"empId":10000, "empId":41689,
"data":{ "data":{
"aName":"Approval of timesheet", "aName":"Approval of timesheet",
"aId":"D001", "aId":"D001",
...@@ -140,61 +142,128 @@ app.post("/getreportees", (req, res) => { ...@@ -140,61 +142,128 @@ app.post("/getreportees", (req, res) => {
} }
} }
*/ */
app.post('/createActivity', (req, res) => { // app.post('/createActivity',async (req, res) => {
// const empId = req.body.empId;
// if (!empId) {
// res.status(401).json({ "message": "Employee id is missing" });
// return
// } else {
// let { data } = req.body;
// //data validation
// if (!_.get(data, "aName", "") || !_.get(data, "aId", "") || !_.get(data, "type", "") || !_.get(data, "score", "")) {
// res.status(401).json({ "error": "Invalid Activity data" });
// return;
// }
// if (data.score === (0 || -0) || data.score > 5 || data.score < -5) {
// res.status(401).json({ "message": "Score Should be between 1 to 5 or -1 to -5 only" });
// return
// }
// if(data["comments"]===undefined){
// res.status(401).json({ "message": "need comments field" });
// return
// }
// data = { ...data, "recorded_date": new Date() };
// data = Object.assign(data, { "_id": new ObjectId() })
// let query = { empId: empId };
// await db.collection('performance_master').findOne(query).then(async(result) => {
// if (result) {
// await db.collection('performance_master').updateOne(query, { $push: { "activities": data } })
// .then(async (updateRes) => {
// await calculateAverage(query);
// res.status(201).json({ "reuslt": updateRes });
// })
// .catch((error) => {
// res.json({ "error": error });
// });
// } else {
// let insertData = { empId: empId, activities: [] };
// insertData.activities.push(data);
// await db.collection('performance_master').insertOne(insertData).then(async (result) => {
// await calculateAverage(query);
// res.status(201).json({ "result": result });
// }).catch((error) => {
// res.json({ "message": error })
// })
// }
// }).catch((error) => {
// console.log(error)
// res.send(query)
// })
// }
// })
app.post('/createActivity', async (req, res) => {
const empId = req.body.empId; const empId = req.body.empId;
if (!empId) { if (!empId) {
res.status(401).json({ "message": "Employee id is missing" }); res.status(401).json({ "message": "Employee id is missing" });
return return;
} else { } else {
let { data } = req.body; let { data } = req.body;
//data validation // Data validation
if (!_.get(data, "aName", "") || !_.get(data, "aId", "") || !_.get(data, "type", "") || !_.get(data, "score", "",) || !_.get(data, "comments", "")) { if (
data === undefined ||
typeof data.aName !== 'string' || data.aName.trim() === '' ||
typeof data.aId !== 'string' || data.aId.trim() === '' ||
typeof data.type !== 'string' || data.type.trim() === '' ||
typeof data.score !== 'number'
) {
res.status(401).json({ "error": "Invalid Activity data" }); res.status(401).json({ "error": "Invalid Activity data" });
return; return;
} }
if (data.score === (0 || -0) || data.score > 5 || data.score < -5) { if (data.score === 0 || data.score === -0 || data.score > 5 || data.score < -5) {
res.status(401).json({ "message": "Score Should be between 1 to 5 or -1 to -5 only" }); res.status(401).json({ "message": "Score should be between 1 to 5 or -1 to -5 only" });
return return;
}
if (data.comments === undefined) {
res.status(401).json({ "message": "Need comments field" });
return;
} }
data = { ...data, "recorded_date": new Date() }; data = { ...data, "recorded_date": new Date() };
data = Object.assign(data, { "_id": new ObjectId() }) data = Object.assign(data, { "_id": new ObjectId() });
let query = { empId: empId }; let query = { empId: empId };
db.collection('performance_master').findOne(query).then((result) => { await db.collection('performance_master').findOne(query).then(async (result) => {
if (result) { if (result) {
db.collection('performance_master').updateOne(query, { $push: { "activities": data } }) await db.collection('performance_master').updateOne(query, { $push: { "activities": data } })
.then(async (updateRes) => { .then(async (updateRes) => {
await calculateAverage(query); await calculateAverage(result); // Pass result instead of query
res.status(201).json({ "reuslt": updateRes }); res.status(201).json({ "result": updateRes });
}) })
.catch((error) => { .catch((error) => {
res.json({ "error": error }); res.json({ "error": error });
}); });
} else { } else {
let insertData = { empId: empId, activities: [] }; let insertData = { empId: empId, activities: [] };
insertData.activities.push(data); insertData.activities.push(data);
db.collection('performance_master').insertOne(insertData).then(async (result) => { await db.collection('performance_master').insertOne(insertData).then(async (result) => {
await calculateAverage(query); await calculateAverage(result); // Pass result instead of query
res.status(201).json({ "result": result }); res.status(201).json({ "result": result });
}).catch((error) => { }).catch((error) => {
res.json({ "message": error }) res.json({ "message": error });
});
})
} }
}).catch((error) => { }).catch((error) => {
console.log(error) console.log(error);
res.send(query) res.send(error);
}) });
}
});
}
})
//calculating average score and updating into employees data //calculating average score and updating into employees data
const calculateAverage = (query) => { const calculateAverage = (query) => {
......
...@@ -64,16 +64,16 @@ function Reports() { ...@@ -64,16 +64,16 @@ function Reports() {
} }
const handleAddActivity = (activityData) => { const handleAddActivity = async(activityData) => {
if (id) { if (id) {
let newData = { let newData = {
"empId": empId, "empId": empId,
"data": activityData "data": activityData
} }
axios.post(`${base_url}/createActivity`, newData) await axios.post(`${base_url}/createActivity`, newData)
.then((result) => { .then(async(result) => {
getReports() await getReports()
fetchLatestReporteesData() await fetchLatestReporteesData()
}) })
} else { } else {
alert("Please login") alert("Please login")
......
export const base_url = 'http://localhost:4000' // export const base_url = 'http://localhost:4000'
\ No newline at end of file export const base_url = 'https://nisumscorecardserverdev.netlify.app/.netlify/functions/api'
\ 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