Skip to content

Commit

Permalink
Merge pull request #12 from codewith-zach/master
Browse files Browse the repository at this point in the history
added clockout by tag endpoint
  • Loading branch information
mrvincentoti authored Sep 7, 2022
2 parents 6124310 + c7a4f09 commit 591e89d
Show file tree
Hide file tree
Showing 4 changed files with 605 additions and 142 deletions.
55 changes: 47 additions & 8 deletions api/users/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const {
createUsers,
getVisitorPurpose,
getAllVisitors,
getVisitorByFullname,
updateVisitorClockout,
getVisitorsNumber
getVisitorByFullname,
updateVisitorClockout,
signedInVisitors,
clockoutTagNumber


} = require('./user.service'); //we called the service

Expand Down Expand Up @@ -99,7 +101,7 @@ module.exports = {
});
});
},
updateVisitorClockout: (req, res) => {
updateVisitorClockout: (req, res) => {
const body = req.body;
updateVisitorClockout(body, (err, results) => {
if (err) {
Expand All @@ -118,6 +120,25 @@ updateVisitorClockout: (req, res) => {
});
});
},
clockoutTagNumber: (req, res) => {
const body = req.body;
clockoutTagNumber(body, (err, results) => {
if (err) {
console.log(err);
return;
}
if(!results){
return res.json({
success: 0,
message: 'Invalid Tag Number'
});
}
return res.json({
success: 1,
message: 'updated successfully'
});
});
},
deleteVisitors: (req, res) => {
const data = req.body;
deleteVisitors(data, (err, results) =>{
Expand Down Expand Up @@ -168,7 +189,7 @@ updateVisitorClockout: (req, res) => {
});
});
},
getAllVisitors: (req, res) => {
getAllVisitors: (req, res) => {
getAllVisitors((err, results) => {
if (err) {
console.log(err);
Expand All @@ -179,7 +200,8 @@ updateVisitorClockout: (req, res) => {
data: results
});
});
},
},

getVisitorsNumber: (req, res) => {
getVisitorsNumber((err, results) => {
if (err) {
Expand All @@ -192,6 +214,7 @@ getVisitorsNumber: (req, res) => {
});
});
},

getVisitorPurpose: (req, res) => {
getVisitorPurpose((err, results) => {
if (err) {
Expand All @@ -205,7 +228,7 @@ getVisitorsNumber: (req, res) => {
});
},
getVisitorsByVisitorId: (req, res) => {
const id = req.params.id;
const id = req.query.id;
getVisitorsByVisitorId(id, (err, results) => {
if (err) {
console.log(err);
Expand Down Expand Up @@ -318,7 +341,23 @@ getVisitorsNumber: (req, res) => {
});
} //Now we are calling this service
});
}
},
signedInVisitors: (req, res) => {
const fullname = req.query.fullname;
signedInVisitors(fullname, (err, results) => {
if (err) {
console.log(err);
return;
}
return res.json({
success: 1,
data: results
});
});
},
geTagNumber: (req, res) => {

}
};

//We have definded all the controllers, now it's time to define the route
16 changes: 11 additions & 5 deletions api/users/user.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ const { //We have to import all the controllers in the router
getVisitorPurpose,
getAllVisitors,
getVisitorByFullname,
getVisitorsNumber,
updateVisitorClockout
updateVisitorClockout,
signedInVisitors,
clockoutTagNumber,
getVisitorsNumber
} = require('./user.controller');

const router = require('express').Router();
Expand All @@ -25,20 +27,24 @@ const { checkToken } = require('../../auth/token_validation');
router.post('/visitor/createVisitors', checkToken, createVisitors); //we are passing the request to 'creatUser' in the user.controller
router.get('/visitor/getVisitors', checkToken, getVisitors);
//router.get('/visitor/getVisitors', checkToken, getVisitors);
router.get('/visitor/getAllVisitors', checkToken, getAllVisitors);
router.get('/visitor/getVisitorsByVisitorId:id', checkToken, getVisitorsByVisitorId);
router.get('/visitor/getAllVisitors', getAllVisitors);
router.get('/visitor/getVisitorsByVisitorId', getVisitorsByVisitorId);
router.patch('/visitor/updateVisitors', checkToken, updateVisitors);
router.delete('/visitor/deleteVisitors', checkToken, deleteVisitors);
router.get('/user/getUserByUserId:id', checkToken, getUserByUserId);
router.patch('/user/updateUsers', checkToken, updateUser);
router.delete('/user/deleteUser', checkToken, deleteUser);
router.post('/user/login', login); //used for login
router.get('/user/getUsers', getUsers);
router.post('/user/createUsers', checkToken, createUsers);
router.post('/user/createUsers', createUsers);
router.get('/visitor/getVisitorPurpose', getVisitorPurpose);
router.get('/visitor/getVisitorByFullname', getVisitorByFullname);
router.get('/visitor/getVisitorsNumber', checkToken, getVisitorsNumber);
router.patch('/visitor/updateVisitorClockout', updateVisitorClockout);
router.get('/visitor/getVisitorByFullname', getVisitorByFullname);
router.get('/visitor/signedInVisitors', signedInVisitors);
router.patch('/visitor/clockoutTagNumber', clockoutTagNumber);




Expand Down
119 changes: 112 additions & 7 deletions api/users/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ module.exports = {
return callBack(error);
}
signInVisitor(results);
addVisitorTag(results.insertId);
updateVisitorStatusFalse();
return callBack(null, results);
}

Expand All @@ -62,10 +64,11 @@ module.exports = {
pool.query(
`
select v.id as id, v.fullname as fullname, v.purpose_id as purpose, v.date_added as date_added, v.address as address, v.phone_number as phone_number,
c.time_in as time_in, c.time_out as time_out,u.first_name as first_name, u.last_name as last_name
c.time_in as time_in, c.time_out as time_out,u.first_name as first_name, u.last_name as last_name, t.tag as tag
from clock_in c
join visitors v on v.id = c.visitor_id
join users u on u.id = v.user_id
join visitor_tag t on t.visitor_id = c.visitor_id
where date(c.time_in) = current_date
order by id desc
`,
Expand Down Expand Up @@ -174,13 +177,20 @@ module.exports = {
},
getVisitorsByVisitorId: (id, callBack) => {
pool.query(
`select fullname, user_id, purpose_id, date_added, address, phone_number from visitors where id=?`,
`select v.id as id, v.fullname as fullname, v.purpose_id as purpose, v.date_added as date_added, v.address as address, v.phone_number as phone_number,
c.time_in as time_in, c.time_out as time_out,u.first_name as first_name, u.last_name as last_name, t.tag as tag
from visitors v
join visitor_tag t on t.visitor_id = v.id
join clock_in c on v.id = c.visitor_id
join users u on u.id = v.user_id
where v.id like'%${id}%'
`,
[id],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
return callBack(null, results);
}
);
},
Expand Down Expand Up @@ -224,6 +234,7 @@ module.exports = {
);
},
updateVisitorClockout: (data, callBack) => {
updateVisitorStatusTrue(data)
pool.query(
`update clock_in set time_out = now() where visitor_id=? and time_out=?`,
[
Expand All @@ -238,6 +249,30 @@ module.exports = {
}
);
},
clockoutTagNumber: (data,callBack) => {
if (data.tag === data.tag_no) {
updateVisitorStatusTrue(data);
pool.query(
`update clock_in set time_out = now() where visitor_id=? and time_out=?`,
[
data.id,
'2000-08-02 00:00:00'
],
(error, results, fields) =>{
if (error) {
callBack(error);
}
return callBack(null, results);

}
);
}else{
const test = (error, results) =>{
return callBack(null, results);
}
test();
}
},
deleteVisitors: (data, callBack) => {
pool.query(
`delete from visitors where id = ?`,
Expand All @@ -261,7 +296,28 @@ module.exports = {
return callBack(null, results[0]);
}
);
}
},
signedInVisitors: (fullname, callBack) => {
pool.query(
`
select v.id as id, v.fullname as fullname, v.purpose_id as purpose, v.date_added as date_added, v.address as address, v.phone_number as phone_number,
c.time_in as time_in, c.time_out as time_out,u.first_name as first_name, u.last_name as last_name
from clock_in c
join visitors v on v.id = c.visitor_id
join users u on u.id = v.user_id
where date(c.time_out) != current_date and date(c.time_in) = current_date and v.fullname like '%${fullname}%'
order by id desc
`,
[fullname],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results);
}
);
},

};


Expand All @@ -273,11 +329,60 @@ signInVisitor = (results) => {
],
(error, results, fields) => {
if (error) {
console.log(error);
return error;
}
console.log(results);
return results;
}
)
}
}

const addVisitorTag = (visitorId) =>{
pool.query(
`SELECT tag_number FROM tag WHERE tag_status IS TRUE LIMIT 1`,
[],
(error, results, fields) => {
if (error) {
return error;
}
const tagNumb = JSON.parse(JSON.stringify(results));
numb = tagNumb[0].tag_number;
const val = visitorId + " " + numb;
console.log(val);
pool.query(
`insert into visitor_tag(visitor_id,tag) values(?,?)`,
[visitorId,numb]
)
}
)
}

const updateVisitorStatusFalse = () =>{
pool.query(
`SELECT tag_number FROM tag WHERE tag_status IS TRUE LIMIT 1`,
[],
(error, results, fields) => {
if (error) {
return error;
}
const tagNumb = JSON.parse(JSON.stringify(results));
numb = tagNumb[0].tag_number;
pool.query(
`update tag set tag_status = false where tag_number = ${numb}`,
[]
)
}
)
}
const updateVisitorStatusTrue = (data) =>{
const tag = data.tag
pool.query(
`update tag set tag_status = true where tag_number = ${tag}`,
[],
(error, results, fields) => {
if (error) {
return error;
}
}
)
}

Loading

0 comments on commit 591e89d

Please sign in to comment.