Skip to content

Commit

Permalink
feat: set commentavatar on cover image change
Browse files Browse the repository at this point in the history
  • Loading branch information
benfurber committed May 29, 2024
1 parent 2f9cc11 commit b68abba
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 31 deletions.
44 changes: 35 additions & 9 deletions functions/src/userUpdates/hasKeyDetailsChanged.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { hasKeyDetailsChanged } from './hasKeyDetailsChanged'
import {
hasCoverImagesChanged,
hasKeyDetailsChanged,
} from './hasKeyDetailsChanged'

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

Expand All @@ -10,6 +13,11 @@ describe('hasKeyDetailsChanged', () => {
} as IUserDB
const user = {
displayName: 'new name',
coverImages: [
{
downloadUrl: 'http//etc.',
},
],
location: { countryCode: 'USA' },
badges: { verified: true },
} as IUserDB
Expand All @@ -18,23 +26,41 @@ describe('hasKeyDetailsChanged', () => {
})

it('returns false when details are the same', () => {
const prevUser = {
const user = {
displayName: 'same name',
location: { countryCode: 'USA' },
coverImages: [
{
downloadUrl: 'http://etc.',
},
],
badges: {
verified: true,
supporter: false,
},
} as IUserDB

expect(hasKeyDetailsChanged(user, user)).toEqual(false)
})
})

describe('hasKeyDetailsChanged', () => {
it('returns false when coverImage array is missing from both', () => {
const user = {} as IUserDB
expect(hasCoverImagesChanged(user, user)).toEqual(false)
})

it('returns false when coverImage array is empty for both', () => {
const user = {
displayName: 'same name',
location: { countryCode: 'USA' },
badges: {
verified: true,
supporter: false,
},
coverImages: [],
} as IUserDB
expect(hasCoverImagesChanged(user, user)).toEqual(false)
})

expect(hasKeyDetailsChanged(prevUser, user)).toEqual(false)
it('returns false when coverImage first item is the same for both', () => {
const user = {
coverImages: [{ downloadUrl: 'same' }],
} as IUserDB
expect(hasCoverImagesChanged(user, user)).toEqual(false)
})
})
15 changes: 15 additions & 0 deletions functions/src/userUpdates/hasKeyDetailsChanged.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { valuesAreDeepEqual } from '../Utils'

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

export const hasKeyDetailsChanged = (prevUser: IUserDB, user: IUserDB) => {
const detailsChanged = [
prevUser.displayName !== user.displayName,
prevUser.location?.countryCode !== user.location?.countryCode,
hasCoverImagesChanged(prevUser, user),
prevUser.badges?.verified !== user.badges?.verified,
prevUser.badges?.supporter !== user.badges?.supporter,
]
return !!detailsChanged.find((detail) => detail === true)
}

export const hasCoverImagesChanged = (prevUser: IUserDB, user: IUserDB) => {
if (!prevUser.coverImages && !user.coverImages) return false

if (prevUser.coverImages && user.coverImages) {
if (!prevUser.coverImages[0] && !user.coverImages[0]) return false

return !valuesAreDeepEqual(prevUser.coverImages, user.coverImages)
}

return false
}
14 changes: 14 additions & 0 deletions functions/src/userUpdates/updateDiscussionComments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import type { IUserDB } from '../models'
const prevUser = {
_id: 'hjg235z',
location: { countryCode: 'UK' },
coverImages: [
{
downloadUrl: 'https://avatars.githubusercontent.com/u/16688508',
},
],
badges: {
verified: true,
},
Expand All @@ -15,6 +20,7 @@ const userComment = {
_creatorId: prevUser._id,
creatorName: prevUser.displayName,
creatorCountry: prevUser.location.countryCode,
creatorImage: prevUser.coverImages[0].downloadUrl,
isUserVerified: prevUser.badges.verified,
comment: 'Great Post',
}
Expand Down Expand Up @@ -61,16 +67,24 @@ describe('updateDiscussionComments', () => {
const user = {
...prevUser,
location: { countryCode: 'New' },
coverImages: [
{
downloadUrl: 'https://avatars.githubusercontent.com/u/13672737?v=4',
},
],
badges: {
verified: false,
supporter: true,
},
} as IUserDB

getMock.mockResolvedValue(snapshot(discussion))

await updateDiscussionComments(prevUser, user)

const expectedUpdatedComment = expect.objectContaining({
creatorCountry: user.location.countryCode,
creatorImage: user.coverImages[0].downloadUrl,
isUserVerified: user.badges.verified,
isUserSupporter: user.badges.supporter,
})
Expand Down
56 changes: 34 additions & 22 deletions functions/src/userUpdates/updateDiscussionComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,46 @@ export const updateDiscussionComments = async (
) => {
if (!hasKeyDetailsChanged(prevUser, user)) return

let updatedCommentCount = 0

const { _id, badges, location } = user
const userDetails = {
creatorCountry: location?.countryCode || '',
isUserVerified: !!badges?.verified,
isUserSupporter: !!badges?.supporter,
}

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

for (const doc of snapshot.docs) {
const discussion = doc.data() as IDiscussion
if (!snapshot.empty) {
const { _id, badges, coverImages, location } = user
const creatorImage = setCreatorImage(coverImages)

const userDetails = {
creatorCountry: location?.countryCode || '',
...(creatorImage ? { creatorImage } : {}),
isUserVerified: !!badges?.verified,
isUserSupporter: !!badges?.supporter,
}

let updatedCommentCount = 0

const comments = discussion.comments.map((comment) => {
if (comment._creatorId !== _id) return comment
for (const doc of snapshot.docs) {
const discussion = doc.data() as IDiscussion

updatedCommentCount++
return {
...comment,
...userDetails,
}
})
const comments = discussion.comments.map((comment) => {
if (comment._creatorId !== _id) return comment

updatedCommentCount++
return {
...comment,
...userDetails,
}
})

await doc.ref.update({ comments })
}
return console.log(`Updated ${updatedCommentCount} discussion comments`)
}
}

await doc.ref.update({ comments })
const setCreatorImage = (coverImages) => {
if (coverImages && coverImages[0] && coverImages[0].downloadUrl) {
return coverImages[0].downloadUrl
}
console.log(`Updated ${updatedCommentCount} discussion comments`)
return undefined
}

0 comments on commit b68abba

Please sign in to comment.