Skip to content

Commit

Permalink
add no. of days to leave schema
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshayCHD committed Mar 4, 2020
1 parent f57d561 commit 61b9c5a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 61 deletions.
36 changes: 2 additions & 34 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]"
}
},
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());
Expand Down
7 changes: 7 additions & 0 deletions server/models/leave.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* - lastName
* - startDate
* - endDate
* - daysCount
* - leaveType
* - halfDay
* - status
Expand All @@ -27,6 +28,8 @@
* type: string
* endDate:
* type: string
* daysCount:
* type: Number
* leaveType:
* type: string
* halfDay:
Expand Down Expand Up @@ -69,6 +72,10 @@ const mongoose = require('mongoose');
type: Date,
required: true
},
daysCount: {
type: Number,
required: true
},
leaveType: {
type: String,
required: true
Expand Down
52 changes: 25 additions & 27 deletions server/routes/leave.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand Down
34 changes: 34 additions & 0 deletions server/utils/swagger.js
Original file line number Diff line number Diff line change
@@ -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: "[email protected]"
}
},
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

0 comments on commit 61b9c5a

Please sign in to comment.