Skip to content

Commit

Permalink
Merge pull request #65 from zarathustra323/last-seen
Browse files Browse the repository at this point in the history
Add last seen field to app users
  • Loading branch information
zarathustra323 authored May 1, 2020
2 parents 30c5b88 + 1ce3212 commit 294a0f2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 12 additions & 0 deletions services/application/src/actions/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
findById,
updateFieldWithApp,
} = require('@identity-x/utils').actions;
const { createRequiredParamError } = require('@base-cms/micro').service;
const create = require('./create');
const findByEmail = require('./find-by-email');
const login = require('./login');
Expand All @@ -30,4 +31,15 @@ module.exports = {
updateFieldWithApp: params => updateFieldWithApp(AppUser, params),
updateOne,
verifyAuth,
setLastSeen: async ({ id }) => {
if (!id) throw createRequiredParamError('id');
const doc = await AppUser.findById(id);
if (!doc) {
const err = new Error(`No user found for ID ${id}.`);
err.statusCode = 404;
throw err;
}
doc.set('lastSeen', new Date());
return doc.save();
},
};
3 changes: 3 additions & 0 deletions services/application/src/mongodb/schema/app-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const schema = new Schema({
lastLoggedIn: {
type: Date,
},
lastSeen: {
type: Date,
},
givenName: {
type: String,
trim: true,
Expand Down
11 changes: 8 additions & 3 deletions services/graphql/src/graphql/resolvers/app-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ module.exports = {
if (user.hasValidUser('AppUser') && applicationId !== user.getAppId()) {
throw new UserInputError('The provided application context does not match the app for the user.');
}
return applicationService.request('loadContext', { applicationId, email, ipAddress });
const context = await applicationService.request('loadContext', { applicationId, email, ipAddress });
// set last seen (do not await)
if (context.user) applicationService.request('user.setLastSeen', { id: context.user._id });
return context;
},

checkContentAccess: async (_, { input }, { user, app, req }) => {
Expand Down Expand Up @@ -78,13 +81,15 @@ module.exports = {
/**
*
*/
activeAppUser: (_, args, { user }) => {
activeAppUser: async (_, args, { user }) => {
const email = user.get('email');
const applicationId = user.getAppId();
return applicationService.request('user.findByEmail', {
const userDoc = await applicationService.request('user.findByEmail', {
applicationId,
email,
});
applicationService.request('user.setLastSeen', { id: userDoc._id });
return userDoc;
},

/**
Expand Down

0 comments on commit 294a0f2

Please sign in to comment.