Skip to content

Commit

Permalink
feat: add function for pins after user updates
Browse files Browse the repository at this point in the history
  • Loading branch information
benfurber committed Sep 24, 2024
1 parent 4786734 commit d9b1c7b
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 118 deletions.
40 changes: 0 additions & 40 deletions functions/src/userUpdates/hasKeyDetailsChanged.ts

This file was deleted.

8 changes: 6 additions & 2 deletions functions/src/userUpdates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { db } from '../Firebase/firestoreDB'
import { DB_ENDPOINTS } from '../models'
import { backupUser } from './backupUser'
import { updateDiscussionComments } from './updateDiscussionComments'
import { updateMapPins } from './updateMapPins'

import type { IDBDocChange, IUserDB } from '../models'

Expand All @@ -17,12 +18,12 @@ import type { IDBDocChange, IUserDB } from '../models'
export const handleUserUpdates = functions
.runWith({ memory: '512MB' })
.firestore.document(`${DB_ENDPOINTS.users}/{id}`)
.onUpdate(async (change, context) => {
.onUpdate(async (change, _) => {
await backupUser(change)
await updateDocuments(change)
})

const isUserCountryDifferent = (prevInfo, info) => {
const isUserCountryDifferent = (prevInfo: IUserDB, info: IUserDB) => {
const prevCountryCode = prevInfo.location?.countryCode
const newCountryCode = info.location?.countryCode
const prevCountry = prevInfo.country
Expand All @@ -48,6 +49,7 @@ async function updateDocuments(change: IDBDocChange) {
}

await updateDiscussionComments(prevInfo, info)
await updateMapPins(prevInfo, info)

const didDelete = prevDeleted !== deleted && deleted
if (didDelete) {
Expand Down Expand Up @@ -142,6 +144,8 @@ async function updatePostsCountry(userId: string, country: IUserDB['country']) {
return false
}

// update _lastActive for any action --> content creation/editing

async function deleteMapPin(_id: string) {
const pin = await db.collection(DB_ENDPOINTS.mappins).doc(_id).get()

Expand Down
16 changes: 5 additions & 11 deletions functions/src/userUpdates/updateDiscussionComments.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { DB_ENDPOINTS } from 'oa-shared'

import { db } from '../Firebase/firestoreDB'
import { hasKeyDetailsChanged } from './hasKeyDetailsChanged'
import { getCreatorImage, hasDetailsForCommentsChanged } from './utils'

import type { IDiscussion, IUserDB } from '../models'

export const updateDiscussionComments = async (
prevUser: IUserDB,
user: IUserDB,
) => {
if (!hasKeyDetailsChanged(prevUser, user)) return
if (!hasDetailsForCommentsChanged(prevUser, user)) return

const snapshot = await db
.collection(DB_ENDPOINTS.discussions)
.where('contributorIds', 'array-contains', user._id)
.get()

if (!snapshot.empty) {
const { _id, badges, userImage, location } = user
const { _id, badges, country, userImage, location } = user
const creatorImage = getCreatorImage(userImage)
const creatorCountry = location?.countryCode || country || ''

const userDetails = {
creatorCountry: location?.countryCode || '',
creatorCountry,
creatorImage,
isUserVerified: !!badges?.verified,
isUserSupporter: !!badges?.supporter,
Expand All @@ -47,10 +48,3 @@ export const updateDiscussionComments = async (
return console.log(`Updated ${updatedCommentCount} discussion comments`)
}
}

const getCreatorImage = (userImage: IUserDB['userImage']) => {
if (userImage && userImage.downloadUrl) {
return userImage.downloadUrl
}
return null
}
62 changes: 62 additions & 0 deletions functions/src/userUpdates/updateMapPins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DB_ENDPOINTS } from 'oa-shared'

import { db } from '../Firebase/firestoreDB'
import {
getCreatorImage,
getFirstCoverImage,
hasDetailsForMapPinChanged,
} from './utils'

import type { IUserDB } from '../models'

export const updateMapPins = async (prevUser: IUserDB, user: IUserDB) => {
if (!hasDetailsForMapPinChanged(prevUser, user)) {
return
}

const snapshot = await db
.collection(DB_ENDPOINTS.mappins)
.where('_id', '==', user._id)
.get()

if (snapshot.empty) {
return
}

const {
_id,
_lastActive,
about,
badges,
country,
coverImages,
displayName,
isContactableByPublic,
profileType,
subType,
userImage,
location,
} = user
const creatorImage = getCreatorImage(userImage)
const coverImage = getFirstCoverImage(coverImages)
const countryCode = location?.countryCode || country || ''

const creator = {
_lastActive,
about,
badges,
countryCode,
coverImage,
displayName,
isContactableByPublic,
profileType,
subType,
userImage: creatorImage,
}

// Only one expected
for (const doc of snapshot.docs) {
await doc.ref.update({ creator })
}
return console.log(`Updated ${_id}'s mapPin`)
}
Loading

0 comments on commit d9b1c7b

Please sign in to comment.