Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Abzmoni committed Sep 1, 2022
2 parents 9e717d0 + fa301e1 commit 4331993
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 125 deletions.
21 changes: 20 additions & 1 deletion api/users/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
getVisitorPurpose,
getAllVisitors,
getVisitorByFullname,
updateVisitorClockout,
getVisitorsNumber

} = require('./user.service'); //we called the service
Expand Down Expand Up @@ -76,7 +77,6 @@ module.exports = {
});
});
},

updateVisitors: (req, res) => {
const body = req.body;
console.log(body);
Expand All @@ -99,6 +99,25 @@ module.exports = {
});
});
},
updateVisitorClockout: (req, res) => {
const body = req.body;
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;
deleteVisitors(data, (err, results) =>{
Expand Down
6 changes: 5 additions & 1 deletion api/users/user.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const { //We have to import all the controllers in the router
getVisitorPurpose,
getAllVisitors,
getVisitorByFullname,
getVisitorsNumber
getVisitorsNumber,
updateVisitorClockout
} = require('./user.controller');

const router = require('express').Router();
Expand All @@ -37,5 +38,8 @@ router.post('/user/createUsers', checkToken, createUsers);
router.get('/visitor/getVisitorPurpose', getVisitorPurpose);
router.get('/visitor/getVisitorByFullname', getVisitorByFullname);
router.get('/visitor/getVisitorsNumber', checkToken, getVisitorsNumber);
router.patch('/visitor/updateVisitorClockout', updateVisitorClockout);



module.exports = router;
266 changes: 143 additions & 123 deletions api/users/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -105,145 +106,164 @@ module.exports = {
join clock_in c on v.id = c.visitor_id
join users u on u.id = v.user_id
`,
[],
(error, results, fields) => {
if (error) {
return callBack(error);
[],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results);
}
return callBack(null, results);
}
);
},
getVisitorPurpose: callBack => {
pool.query(
`select id, purpose from purpose`,
[],
(error, results, fields) => {
if (error) {
return callBack(error);
);
},



getVisitorPurpose: callBack => {
pool.query(
`select id, purpose from purpose`,
[],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results);
}
return callBack(null, results);
}
);
},
getUserByUserId: (id, callBack) => {
pool.query(
`select username, email, first_name, last_name, phone_number, gender_id, department_id from users where id=?`,
[id],
(error, results, fields) => {
if (error) {
callBack(error);
);
},
getUserByUserId: (id, callBack) => {
pool.query(
`select username, email, first_name, last_name, phone_number, gender_id, department_id from users where id=?`,
[id],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
return callBack(null, results[0]);
}
);
},
updateUser: (data, callBack) => {
pool.query(
`update users set username=?, email=?, first_name=?, last_name=?, phone_number=?, gender_id=?, department_id=? where id = ?`,
[
data.username,
data.email,
data.first_name,
data.last_name,
data.phone_number,
data.gender_id,
data.department_id,
data.id
],
(error, results, fields) => {
if (error) {
callBack(error);
);
},
updateUser: (data, callBack) => {
pool.query(
`update users set username=?, email=?, first_name=?, last_name=?, phone_number=?, gender_id=?, department_id=? where id = ?`,
[
data.username,
data.email,
data.first_name,
data.last_name,
data.phone_number,
data.gender_id,
data.department_id,
data.id
],
(error, results, fields) =>{
if (error) {
callBack(error);
}
return callBack(null, results);
}
return callBack(null, results);
}
);
},
deleteUser: (data, callBack) => {
pool.query(
`delete from users where id = ?`,
[data.id],
(error, results, fields) => {
if (error) {
return callBack(error);
);
},
deleteUser: (data, callBack) => {
pool.query(
`delete from users where id = ?`,
[data.id],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results[0]);
}
return callBack(null, results[0]);
}
);
},
getVisitorsByVisitorId: (id, callBack) => {
pool.query(
`select fullname, user_id, purpose_id, date_added, address from visitors where id=?`,
[id],
(error, results, fields) => {
if (error) {
callBack(error);
);
},
getVisitorsByVisitorId: (id, callBack) => {
pool.query(
`select fullname, user_id, purpose_id, date_added, address from visitors where id=?`,
[id],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
return callBack(null, results[0]);
}
);
},
getVisitorByFullname: (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,
);
},
getVisitorByFullname: (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,
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 v.fullname like '%${fullname}%'
`,
[fullname],
(error, results, fields) => {
if (error) {
callBack(error);
[fullname],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results);
}
return callBack(null, results);
}
);
},
updateVisitors: (data, callBack) => {
pool.query(
`update visitors set fullname=?, user_id=?, purpose_id=?, date_added=?, address=? where id=?`,
[
data.fullname,
data.user_id,
data.purpose_id,
data.date_added,
data.address,
data.id
],
(error, results, fields) => {
if (error) {
callBack(error);
);
},
updateVisitors: (data, callBack) => {
pool.query(
`update visitors set fullname=?, user_id=?, purpose_id=?, date_added=?, address=? where id=?`,
[
data.fullname,
data.user_id,
data.purpose_id,
data.date_added,
data.address,
data.id
],
(error, results, fields) =>{
if (error) {
callBack(error);
}
return callBack(null, results);
}
return callBack(null, results);
}
);
},
deleteVisitors: (data, callBack) => {
pool.query(
`delete from visitors where id = ?`,
[data.id],
(error, results, fields) => {
if (error) {
return callBack(error);
);
},
updateVisitorClockout: (data, callBack) => {
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);
}
return callBack(null, results[0]);
}
);
},
getUserByUserEmail: (email, callBack) => {
pool.query(
`select * from users where email = ?`,
[email],
(error, results, fields) => {
if (error) {
callBack(error);
);
},
deleteVisitors: (data, callBack) => {
pool.query(
`delete from visitors where id = ?`,
[data.id],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results[0]);
}
return callBack(null, results[0]);
}
);
}
);
},
getUserByUserEmail: (email, callBack) => {
pool.query(
`select * from users where email = ?`,
[email],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
}
);
}
};


Expand Down

0 comments on commit 4331993

Please sign in to comment.