From 23d902f34dc85a0b1ea114d08b8e0dfb76a4e366 Mon Sep 17 00:00:00 2001 From: codewith-zach <92583144+codewith-zach@users.noreply.github.com> Date: Mon, 29 Aug 2022 17:31:36 +0100 Subject: [PATCH 1/2] clock-out endpoint --- api/users/user.controller.js | 26 ++++++++++++++++++++++++-- api/users/user.router.js | 6 +++++- api/users/user.service.js | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/api/users/user.controller.js b/api/users/user.controller.js index c81a31a..47fb32f 100644 --- a/api/users/user.controller.js +++ b/api/users/user.controller.js @@ -14,7 +14,8 @@ const { createUsers, getVisitorPurpose, getAllVisitors, - getVisitorByFullname + getVisitorByFullname, + updateVisitorClockout } = require('./user.service'); //we called the service @@ -75,7 +76,6 @@ module.exports = { }); }); }, - updateVisitors: (req, res) => { const body = req.body; console.log(body); @@ -97,6 +97,28 @@ module.exports = { message: 'updated successfully' }); }); + }, + updateVisitorClockout: (req, res) => { + const body = req.body; + console.log(body); + // const salt = genSaltSync(10); + // body.password = hashSync(body.password, salt); + updateVisitorClockout(body, (err, results) => { + if (err) { + console.log(err); + return; + } + if(!results){ + return res.json({ + success: 0, + message: 'failed to update visitor' + }); + } + return res.json({ + success: 1, + message: 'updated successfully' + }); + }); }, deleteVisitors: (req, res) => { const data = req.body; diff --git a/api/users/user.router.js b/api/users/user.router.js index f25c9b1..f402f6c 100644 --- a/api/users/user.router.js +++ b/api/users/user.router.js @@ -12,7 +12,8 @@ const { //We have to import all the controllers in the router createUsers, getVisitorPurpose, getAllVisitors, - getVisitorByFullname + getVisitorByFullname, + updateVisitorClockout } = require('./user.controller'); const router = require('express').Router(); @@ -35,5 +36,8 @@ router.get('/user/getUsers', getUsers); router.post('/user/createUsers', checkToken, createUsers); router.get('/visitor/getVisitorPurpose', getVisitorPurpose); router.get('/visitor/getVisitorByFullname', getVisitorByFullname); +router.patch('/visitor/updateVisitorClockout', updateVisitorClockout); + + module.exports = router; \ No newline at end of file diff --git a/api/users/user.service.js b/api/users/user.service.js index 030a7f9..0247322 100644 --- a/api/users/user.service.js +++ b/api/users/user.service.js @@ -95,6 +95,9 @@ module.exports = { } ); }, + + + getVisitorPurpose: callBack => { pool.query( `select id, purpose from purpose`, @@ -202,6 +205,20 @@ module.exports = { } ); }, + updateVisitorClockout: (data, callBack) => { + pool.query( + `update clock_in set time_out = now() where visitor_id=? `, + [ + data.visitor_id + ], + (error, results, fields) =>{ + if (error) { + callBack(error); + } + return callBack(null, results); + } + ); + }, deleteVisitors: (data, callBack) => { pool.query( `delete from visitors where id = ?`, From 66269808f01fdab88841b301ddcb2c98f1db7696 Mon Sep 17 00:00:00 2001 From: codewith-zach <92583144+codewith-zach@users.noreply.github.com> Date: Tue, 30 Aug 2022 17:32:50 +0100 Subject: [PATCH 2/2] sorted clockin table --- api/users/user.controller.js | 5 +---- api/users/user.service.js | 6 ++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/api/users/user.controller.js b/api/users/user.controller.js index 47fb32f..aa8cb1d 100644 --- a/api/users/user.controller.js +++ b/api/users/user.controller.js @@ -98,11 +98,8 @@ module.exports = { }); }); }, - updateVisitorClockout: (req, res) => { +updateVisitorClockout: (req, res) => { const body = req.body; - console.log(body); - // const salt = genSaltSync(10); - // body.password = hashSync(body.password, salt); updateVisitorClockout(body, (err, results) => { if (err) { console.log(err); diff --git a/api/users/user.service.js b/api/users/user.service.js index 5431616..0dd285f 100644 --- a/api/users/user.service.js +++ b/api/users/user.service.js @@ -67,6 +67,7 @@ module.exports = { join visitors v on v.id = c.visitor_id join users u on u.id = v.user_id where date(c.time_in) = current_date + order by id desc `, [], (error, results, fields) => { @@ -207,9 +208,10 @@ module.exports = { }, updateVisitorClockout: (data, callBack) => { pool.query( - `update clock_in set time_out = now() where visitor_id=? `, + `update clock_in set time_out = now() where visitor_id=? and time_out=?`, [ - data.visitor_id + data.id, + '2000-08-02 00:00:00' ], (error, results, fields) =>{ if (error) {