Skip to content

Commit

Permalink
Merge pull request #278 from bcgsc/feature/DEVSU-2128-lastloginat
Browse files Browse the repository at this point in the history
feature/DEVSU-2128
  • Loading branch information
Nithriel authored Nov 17, 2023
2 parents bc1e897 + f0f5a25 commit 1f30bce
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/middleware/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ const SPECIAL_CASES = [
];

module.exports = async (req, res, next) => {
// Update last time the user logged in, limit to once a day
const currentDate = new Date().toDateString();
const userLastLogin = req.user.lastLoginAt
? new Date(req.user.lastLoginAt).toDateString()
: '';

if (userLastLogin !== currentDate) {
await req.user.update({lastLoginAt: new Date()});
}

// Check if user is an admin
if (isAdmin(req.user)) {
return next();
Expand Down
6 changes: 6 additions & 0 deletions app/models/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ module.exports = (sequelize, Sq) => {
schema: {type: 'string', format: 'email'},
},
},
lastLoginAt: {
name: 'lastLoginAt',
field: 'last_login_at',
type: Sq.DATE,
defaultValue: null,
},
}, {
...DEFAULT_OPTIONS,
indexes: [
Expand Down
20 changes: 20 additions & 0 deletions migrations/20231117185810-DEVSU-2128-add-lastloginat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const TABLE = 'users';

module.exports = {
up: async (queryInterface, Sq) => {
return queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.addColumn(
TABLE,
'last_login_at',
{
type: Sq.DATE,
},
{transaction},
);
});
},

down: async () => {
throw new Error('Not Implemented!');
},
};
17 changes: 17 additions & 0 deletions test/routes/user/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ describe('/user', () => {
checkUser(res.body);
});

// Test that lastLoginAt is updated with a new request
test('/me - test if requests upload last_login_at 200 Success', async () => {
const res = await request
.get('/api/user/me')
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);

const loginDate = new Date(res.body.lastLoginAt);
const currentDate = new Date();

checkUser(res.body);
expect(
loginDate.toDateString() === currentDate.toDateString(),
).toBe(true);
});

// Test for GET /user/search 200 endpoint
test('/search - 200 Success', async () => {
// Create unique first name
Expand Down

0 comments on commit 1f30bce

Please sign in to comment.