Skip to content

Commit

Permalink
pr revision #1105
Browse files Browse the repository at this point in the history
  • Loading branch information
danitrod committed Mar 9, 2021
1 parent 6f4c36f commit af7369e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
12 changes: 4 additions & 8 deletions functions/src/userUpdates/backupUser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { IDBDocChange } from '../models'
import { IUserDB } from 'src/models/user.models'
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: IUserDB,
updatedUser: IUserDB,
): boolean => {
const hasUserDataUpdated = (prevUser: DBDoc, updatedUser: DBDoc): boolean => {
return Object.keys(prevUser).some(
key =>
key !== '_modified' &&
Expand All @@ -24,8 +20,8 @@ const hasUserDataUpdated = (
*/
export const backupUser = (change: IDBDocChange) => {
const { before, after } = change
const rev = before.data() as IUserDB
const updated = after.data() as IUserDB
const rev = before.data() as DBDoc
const updated = after.data() as DBDoc
if (rev && rev._modified && hasUserDataUpdated(rev, updated)) {
return before.ref
.collection('revisions')
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

0 comments on commit af7369e

Please sign in to comment.