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

Fix user last active updating #1105

Merged
merged 2 commits into from
Mar 9, 2021
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
16 changes: 15 additions & 1 deletion functions/src/userUpdates/backupUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { DBDoc, IDBDocChange } from '../models'

/** Helper function to check if the only field changed is lastActive
* (updates on login), in which case we will not want
* to backup the user again.
*/
const hasUserDataUpdated = (prevUser: DBDoc, updatedUser: DBDoc): boolean => {
return Object.keys(prevUser).some(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really like the implementation, efficient to just look for first key barring exclusions where data has changed. I also tested locally and working as intended.

key =>
key !== '_modified' &&
key !== '_lastActive' &&
prevUser[key] !== updatedUser[key],
)
}

/**
* Automatically create user revision on update
* Nests revision as subcollection of original document,
Expand All @@ -8,7 +21,8 @@ import { DBDoc, IDBDocChange } from '../models'
export const backupUser = (change: IDBDocChange) => {
const { before, after } = change
const rev = before.data() as DBDoc
if (rev && rev._modified) {
const updated = after.data() as DBDoc
if (rev && rev._modified && hasUserDataUpdated(rev, updated)) {
return before.ref
.collection('revisions')
.doc(rev._modified)
Expand Down
4 changes: 3 additions & 1 deletion functions/src/userUpdates/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as functions from 'firebase-functions'
import { db } from '../Firebase/firestoreDB'
import { IUserDB, IDBDocChange, DB_ENDPOINTS } from '../models'
import { DB_ENDPOINTS, IDBDocChange, IUserDB } from '../models'
import { backupUser } from './backupUser'

/*********************************************************************
* Side-effects to be carried out on various user updates, namely:
Expand All @@ -12,6 +13,7 @@ export const handleUserUpdates = functions.firestore
.document(`${DB_ENDPOINTS.users}/{id}`)
.onUpdate(async (change, context) => {
await processCountryUpdates(change)
await backupUser(change)
})

async function processCountryUpdates(change: IDBDocChange) {
Expand Down
6 changes: 6 additions & 0 deletions src/stores/User/user.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export class UserStore extends ModuleStore {
if (userMeta) {
this.updateUser(userMeta)
console.log('userMeta', userMeta)

// Update last active for user
await this.db
.collection<IUserPP>(COLLECTION_NAME)
.doc(userMeta._id)
.set({ ...userMeta, _lastActive: new Date().toISOString() })
} else {
// user profile not found, either it has been deleted or not migrated correctly from legacy format
// create a new profile to use for now and make a log to the error handler in case required
Expand Down