Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/DEVSU-2128 #278

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading