diff --git a/controllers/api/resultsRoute.js b/controllers/api/resultsRoute.js new file mode 100644 index 0000000..a87cfe3 --- /dev/null +++ b/controllers/api/resultsRoute.js @@ -0,0 +1,19 @@ +const overtimeCalc = require('./utils/calc') + +router.get('/results/:email', async (req, res) => { + try { + // Find the logged in user based on the session ID + const entryData = await Entry.findByPk(req.params.email, { + }); + + const entry = entryData.get({ plain: true }); + const X, Y, Z + overtimeCalc(entry.start, entry.end, entry.lunch, entry.salary); + res.render('results', { + X, Y, Z, + }); + + } catch (err) { + res.status(500).json(err); + } +}); \ No newline at end of file diff --git a/public/js/entry.js b/public/js/entry.js new file mode 100644 index 0000000..a63ffb1 --- /dev/null +++ b/public/js/entry.js @@ -0,0 +1,28 @@ +const entryFormHandler = async (event) => { + event.preventDefault(); + + const email = document.querySelector('#email-entry').value.trim(); + const industry = document.querySelector('#industry-entry').value.trim(); + const start = document.querySelector('#start-entry').value.trim(); + const end = document.querySelector('#end-entry').value.trim(); + const lunch = document.querySelector('#lunch-entry').value.trim(); + const salary = document.querySelector('#salary-gross').value.trim(); + + if (email && industry && start && end && lunch && salary) { + const response = await fetch('/api/entry', { + method: 'POST', + body: JSON.stringify({ mail, industry, start, end, lunch, salary }), + headers: { 'Content-Type': 'application/json' }, + }); + + if (response.ok) { + document.location.replace('/results'); + } else { + alert(response.statusText); + } + } +}; + +document + .querySelector('.entry-form') + .addEventListener('submit', entryFormHandler); \ No newline at end of file diff --git a/utils/calc.js b/utils/calc.js new file mode 100644 index 0000000..6bb748e --- /dev/null +++ b/utils/calc.js @@ -0,0 +1,27 @@ + +const moment = require('moment'); + +module.exports = { + overtimeCalc: (start, end, lunch, salary) => { + // const start = moment(document.querySelector('#start-entry').value.format("HH:MM")); + // const end = moment(document.querySelector('#end-entry').value.format("HH:MM")); + // const lunch = parseFloat(document.querySelector('#lunch-entry').value.trim()); + // const salary = parseFloat(document.querySelector('#salary-gross').value.trim()); + const duration = moment.duration(moment(end).diff(start)); + + if (lunch == "nolunch") { + var newDuration = moment(duration).add(30, 'm').toDate(); + } + + const X = newDuration.asHours(); + const Y = hoursWorkedDaily * 230 + const hourlyRate = (salary/260)/7.5 + const Z = hourlyRate * hoursWorkedAnnual + + if (hoursWorkedDaily >= 7.5) { + return { X, Y, Z } + } else { + return + } + } +} \ No newline at end of file diff --git a/views/homepage.handlebars b/views/homepage.handlebars index 08e9f76..8f31928 100644 --- a/views/homepage.handlebars +++ b/views/homepage.handlebars @@ -25,15 +25,15 @@