Skip to content

Commit 294a0f2

Browse files
Merge pull request #65 from zarathustra323/last-seen
Add last seen field to app users
2 parents 30c5b88 + 1ce3212 commit 294a0f2

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

services/application/src/actions/user/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
findById,
66
updateFieldWithApp,
77
} = require('@identity-x/utils').actions;
8+
const { createRequiredParamError } = require('@base-cms/micro').service;
89
const create = require('./create');
910
const findByEmail = require('./find-by-email');
1011
const login = require('./login');
@@ -30,4 +31,15 @@ module.exports = {
3031
updateFieldWithApp: params => updateFieldWithApp(AppUser, params),
3132
updateOne,
3233
verifyAuth,
34+
setLastSeen: async ({ id }) => {
35+
if (!id) throw createRequiredParamError('id');
36+
const doc = await AppUser.findById(id);
37+
if (!doc) {
38+
const err = new Error(`No user found for ID ${id}.`);
39+
err.statusCode = 404;
40+
throw err;
41+
}
42+
doc.set('lastSeen', new Date());
43+
return doc.save();
44+
},
3345
};

services/application/src/mongodb/schema/app-user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ const schema = new Schema({
6161
lastLoggedIn: {
6262
type: Date,
6363
},
64+
lastSeen: {
65+
type: Date,
66+
},
6467
givenName: {
6568
type: String,
6669
trim: true,

services/graphql/src/graphql/resolvers/app-user.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ module.exports = {
4444
if (user.hasValidUser('AppUser') && applicationId !== user.getAppId()) {
4545
throw new UserInputError('The provided application context does not match the app for the user.');
4646
}
47-
return applicationService.request('loadContext', { applicationId, email, ipAddress });
47+
const context = await applicationService.request('loadContext', { applicationId, email, ipAddress });
48+
// set last seen (do not await)
49+
if (context.user) applicationService.request('user.setLastSeen', { id: context.user._id });
50+
return context;
4851
},
4952

5053
checkContentAccess: async (_, { input }, { user, app, req }) => {
@@ -78,13 +81,15 @@ module.exports = {
7881
/**
7982
*
8083
*/
81-
activeAppUser: (_, args, { user }) => {
84+
activeAppUser: async (_, args, { user }) => {
8285
const email = user.get('email');
8386
const applicationId = user.getAppId();
84-
return applicationService.request('user.findByEmail', {
87+
const userDoc = await applicationService.request('user.findByEmail', {
8588
applicationId,
8689
email,
8790
});
91+
applicationService.request('user.setLastSeen', { id: userDoc._id });
92+
return userDoc;
8893
},
8994

9095
/**

0 commit comments

Comments
 (0)