Skip to content

Instantly share code, notes, and snippets.

@leovarmak
Created August 16, 2019 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leovarmak/2430adb334dbd63685e82ed5a5895346 to your computer and use it in GitHub Desktop.
Save leovarmak/2430adb334dbd63685e82ed5a5895346 to your computer and use it in GitHub Desktop.
else if (Obj.diff == true) {
// creating a promise to insert two transaction records
let promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve("done!"), 1000);
})
.then((firstIteration, error) => {
let firstMonthFromDate = Obj.monthFrom;
let firstMonthToDate = Obj.monthTo;
Transaction.create({
fromDate: firstMonthFromDate,
toDate: firstMonthToDate,
reason: reason,
typeOfLeave: typeOfLeave,
year: year,
totalDays: totalDays,
fromDatePartOfDay: fromDatePartOfDay,
toDatePartOfDay: toDatePartOfDay,
description: description,
flagged: flagged,
flagDescription: flagDescription,
employeeId: employeeId
}).then(data => {
LeaveStore.findAll({
where: {
employeeId: employeeId
}
}).then(leavestore => {
Transaction.findAll({
where: {
employeeId: employeeId
}
}).then(gg => {
let temp = gg[0].typeOfLeave;
temp = temp.toLowerCase() + "Consumption";
tempAcc = temp.toLowerCase() + "Accural";
LeaveStore.update(
{
[temp]: parseInt(leavestore[0][temp]) + parseInt(totalDays),
[tempAcc]:
parseInt(leavestore[0][tempAcc]) -
parseInt(leavestore[0][temp]),
netLeaveBalance:
parseInt(leavestore[0].netLeaveBalance) -
parseInt(totalDays)
},
{
where: {
employeeId: employeeId
}
}
);
Transaction.update(
{
balanceAfterTransaction:
parseInt(leavestore[0].netLeaveBalance) -
parseInt(totalDays)
},
{
where: {
employeeId: employeeId
}
}
);
// .then(success => {
// res.jsonp({
// success: true,
// data: `Leave successfully applied`
// });
// });
});
});
});
error => console.log(error, "error in firstIteration");
})
.finally(secondIteration => {
let secondMonthFromDate = Obj.newMonthFrom;
let secondMonthToDate = Obj.newMonthTo;
Transaction.create({
fromDate: secondMonthFromDate,
toDate: secondMonthToDate,
reason: reason,
typeOfLeave: typeOfLeave,
year: year,
totalDays: totalDays,
fromDatePartOfDay: fromDatePartOfDay,
toDatePartOfDay: toDatePartOfDay,
description: description,
flagged: flagged,
flagDescription: flagDescription,
employeeId: employeeId
}).then(data => {
LeaveStore.findAll({
where: {
employeeId: employeeId
}
}).then(leavestore => {
Transaction.findAll({
where: {
employeeId: employeeId
}
}).then(gg => {
let temp = gg[0].typeOfLeave;
temp = temp.toLowerCase() + "Consumption";
tempAcc = temp.toLowerCase() + "Accural";
LeaveStore.update(
{
[temp]: parseInt(leavestore[0][temp]) + parseInt(totalDays),
[tempAcc]:
parseInt(leavestore[0][tempAcc]) -
parseInt(leavestore[0][temp]),
netLeaveBalance:
parseInt(leavestore[0].netLeaveBalance) -
parseInt(totalDays)
},
{
where: {
employeeId: employeeId
}
}
);
Transaction.update(
{
balanceAfterTransaction:
parseInt(leavestore[0].netLeaveBalance) -
parseInt(totalDays)
},
{
where: {
employeeId: employeeId
}
}
).then(success => {
res.jsonp({
success: true,
data: `Leave successfully applied`
});
});
});
});
});
error => console.log(error, "error in secondIteration");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment