diff --git a/server/index.js b/server/index.js index a40663e..795d1db 100644 --- a/server/index.js +++ b/server/index.js @@ -6,42 +6,10 @@ const express = require('express'); const app = express(); const swaggerUi = require('swagger-ui-express'); const swaggerJsdoc = require("swagger-jsdoc"); +const swaggerUtils = require("./utils/swagger") var cors = require('cors'); -// Swagger set up -const options = { - swaggerDefinition: { - openapi: "3.0.0", - info: { - title: "Time to document that Express API you built", - version: "1.0.0", - description: - "A test project to understand how easy it is to document and Express API", - license: { - name: "MIT", - url: "https://choosealicense.com/licenses/mit/" - }, - contact: { - name: "Swagger", - url: "https://swagger.io", - email: "Info@SmartBear.com" - } - }, - servers: [ - { - url: "http://localhost:5000/api" - }, - { - url: "http://10.9.8.150:5000/api" - }, - { - url: "https://mg8n2x0r67.execute-api.ap-south-1.amazonaws.com/dev/api" - }, - ] - }, - apis: ["./models/employee.js", "./models/leaveType.js", "./models/leave.js", "./routes/employee.js", "./routes/leave.js", "./routes/auth.js"] -}; -const specs = swaggerJsdoc(options); +const specs = swaggerJsdoc(swaggerUtils.options); app.use(cors()); app.use(express.json()); diff --git a/server/models/leave.js b/server/models/leave.js index 20d84b0..30743c9 100644 --- a/server/models/leave.js +++ b/server/models/leave.js @@ -11,6 +11,7 @@ * - lastName * - startDate * - endDate + * - daysCount * - leaveType * - halfDay * - status @@ -27,6 +28,8 @@ * type: string * endDate: * type: string + * daysCount: + * type: Number * leaveType: * type: string * halfDay: @@ -69,6 +72,10 @@ const mongoose = require('mongoose'); type: Date, required: true }, + daysCount: { + type: Number, + required: true + }, leaveType: { type: String, required: true diff --git a/server/routes/leave.js b/server/routes/leave.js index 1748385..40fe069 100644 --- a/server/routes/leave.js +++ b/server/routes/leave.js @@ -152,7 +152,10 @@ function sendValidationError(error, res) { return res.status(400).send(error.details[0].message); } -function getLeaveDuration(start, end) { +function getLeaveDuration(start, end, halfDay) { + if(halfDay) + return 0.5; + let startDate = new Date(start) let endDate = new Date(end) let count = 0; @@ -182,38 +185,36 @@ function invalidEmployeeName(employee, names) { return false; } -router.post('/', async (req, res) => { - let validationResult = validate(req.body); +router.post('/', async (req, res) => { + let leaveRequest = _.pick(req.body, ["employeeId", "approverId", "firstName", "lastName", "startDate", "endDate", "leaveType", "halfDay", "description", "status"]) + let validationResult = validate(leaveRequest); if(validationResult.error) return sendValidationError(validationResult.error, res); - if(invalidDays(req.body.startDate, req.body.endDate, req.body.halfDay)) return res.status(400).send('for half day start date and end date should be same') + if(invalidDays(leaveRequest.startDate, leaveRequest.endDate, leaveRequest.halfDay)) return res.status(400).send('for half day start date and end date should be same') - let employee = await Employee.findById(mongoose.Types.ObjectId(req.body.employeeId)); + let employee = await Employee.findById(mongoose.Types.ObjectId(leaveRequest.employeeId)); if(!employee) return res.status(400).send('Invalid Employee Id') - if(invalidEmployeeName(employee, _.pick(req.body, ["firstName", "lastName"]))) return res.status(400).send('employee names do not match the emp id') + if(invalidEmployeeName(employee, _.pick(leaveRequest, ["firstName", "lastName"]))) return res.status(400).send('employee names do not match the emp id') - let approver = await Employee.findById(mongoose.Types.ObjectId(req.body.approverId)); + let approver = await Employee.findById(mongoose.Types.ObjectId(leaveRequest.approverId)); if(!approver) return res.status(400).send('Invalid Approver Id') - if(employee.approver !== approver._id.toString()) return res.status(400).send(`${req.body.approverId} is not the approver of ${req.body.employeeId}`) + if(employee.approver !== approver._id.toString()) return res.status(400).send(`${leaveRequest.approverId} is not the approver of ${leaveRequest.employeeId}`) - let leaveDuration = getLeaveDuration(req.body.startDate, req.body.endDate); - if(employee.available[req.body.leaveType] < leaveDuration) return res.status(400).send('Employee Does not have that many leaves') + let daysCount = getLeaveDuration(leaveRequest.startDate, leaveRequest.endDate, leaveRequest.halfDay); + if(employee.available[leaveRequest.leaveType] < daysCount) return res.status(400).send('Employee Does not have that many leaves') + leaveRequest.daysCount = daysCount + + employee.available[req.body.leaveType] -= leaveRequest.daysCount + employee.availed[req.body.leaveType] += leaveRequest.daysCount - if(req.body.halfDay) { - employee.available[req.body.leaveType] -= 0.5 - employee.availed[req.body.leaveType] += 0.5 - } else { - employee.available[req.body.leaveType] -= leaveDuration - employee.availed[req.body.leaveType] += leaveDuration - } await employee.save() - let leaveSchema = _.pick(req.body, ["employeeId", "approverId", "firstName", "lastName", "startDate", "endDate", "leaveType", "halfDay", "description", "status"]) - leaveSchema.status = "pending" - leave = new Leave(leaveSchema); + leaveRequest.status = "pending" + leave = new Leave(leaveRequest); await leave.save(); + var d1 =new Date(req.body.startDate); var myStartDate = d1.getDate() + "/" + (d1.getMonth() + 1) + "/" + d1.getFullYear() var d2 =new Date(req.body.endDate); @@ -289,6 +290,7 @@ router.get('/approver/:employeeId/:status', async (req, res) => { }) router.put('/', async (req, res) => { + let leaveUpdateRequest = _.pick(req.body, ["employeeId", "approverId", "startDate", "endDate", "leaveType", "halfDay", "description", "firstName", "lastName"]) let validationResult = validate(_.pick(req.body, ["employeeId", "approverId", "startDate", "endDate", "leaveType", "halfDay", "description", "firstName", "lastName"])); if(validationResult.error) return sendValidationError(validationResult.error, res); @@ -306,13 +308,9 @@ router.put('/', async (req, res) => { if(req.body.status === "rejected" && leave.status === "pending") { const employee = await Employee.findById(req.body.employeeId) if(!employee) return res.status(400).send('Invalid Employee Id') - if(leave.halfDay) { - employee.available[req.body.leaveType] += 0.5; - employee.availed[req.body.leaveType] -= 0.5; - } else { - employee.available[req.body.leaveType] += getLeaveDuration(req.body.startDate, req.body.endDate); - employee.availed[req.body.leaveType] -= getLeaveDuration(req.body.startDate, req.body.endDate); - } + + employee.available[req.body.leaveType] += leave.daysCount; + employee.availed[req.body.leaveType] -= leave.daysCount; await employee.save(); leave.status = "rejected"; await leave.save(); diff --git a/server/utils/swagger.js b/server/utils/swagger.js new file mode 100644 index 0000000..d2cc4da --- /dev/null +++ b/server/utils/swagger.js @@ -0,0 +1,34 @@ +const swaggerOptions = { + swaggerDefinition: { + openapi: "3.0.0", + info: { + title: "Time to document that Express API you built", + version: "1.0.0", + description: + "A test project to understand how easy it is to document and Express API", + license: { + name: "MIT", + url: "https://choosealicense.com/licenses/mit/" + }, + contact: { + name: "Swagger", + url: "https://swagger.io", + email: "Info@SmartBear.com" + } + }, + servers: [ + { + url: "http://localhost:5000/api" + }, + { + url: "http://10.9.8.150:5000/api" + }, + { + url: "https://mg8n2x0r67.execute-api.ap-south-1.amazonaws.com/dev/api" + }, + ] + }, + apis: ["./models/employee.js", "./models/leaveType.js", "./models/leave.js", "./routes/employee.js", "./routes/leave.js", "./routes/auth.js"] +}; + +module.exports.options = swaggerOptions \ No newline at end of file