Skip to content

Commit

Permalink
feat: add profile tags to user update function
Browse files Browse the repository at this point in the history
  • Loading branch information
benfurber committed Oct 23, 2024
1 parent 6fd3dc5 commit 054f10b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion functions/src/userUpdates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { backupUser } from './backupUser'
import { updateDiscussionComments } from './updateDiscussionComments'
import { updateMapPins } from './updateMapPins'

import type { IUserDB } from 'oa-shared/models/user'
import type { IDBDocChange } from '../models'
import { IUserDB } from 'oa-shared/models/user'

/*********************************************************************
* Side-effects to be carried out on various user updates, namely:
Expand Down
3 changes: 2 additions & 1 deletion functions/src/userUpdates/updateDiscussionComments.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IUserDB } from 'oa-shared/models/user'
import { updateDiscussionComments } from './updateDiscussionComments'

import type { IUserDB } from 'oa-shared/models/user'

const prevUser = {
_id: 'hjg235z',
location: { countryCode: 'UK' },
Expand Down
104 changes: 100 additions & 4 deletions functions/src/userUpdates/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,86 @@ import {
hasDetailsForMapPinChanged,
hasLocationDetailsChanged,
hasUserImageChanged,
hasUserTagsChanged,
} from './utils'

import type { IUploadedFileMeta } from 'oa-shared'
import type { IUserDB } from 'oa-shared/models/user'

const unimportantUserDetails = {
_authID: '00',
_id: 'unchangeable',
_created: '',
_deleted: false,
userName: 'unchangeable',
verified: false,
coverImages: [],
links: [],
}

describe('hasDetailsChanged', () => {
it("returns false for every field that's the same", () => {
const user = {
_lastActive: 'same',
about: 'about',
displayName: 'same',
isContactableByPublic: true,
profileType: 'member',
userImage: {
downloadUrl: 'https://more.same/image.jpg',
},
} as IUploadedFileMeta,
badges: {
verified: true,
supporter: false,
},
tags: {
hguowewer: true,
'76khbrw': false,
},
...unimportantUserDetails,
} as IUserDB

expect(hasDetailsChanged(user, user)).toEqual([false, false, false, false])
})

it("returns true for every field that's different", () => {
const prevUser = {
_lastActive: 'yesterday',
about: 'Old about',
displayName: 'old',
profileType: 'member',
workspaceType: null,
userImage: {
downloadUrl: 'https://more.old/image.jpg',
},
} as IUploadedFileMeta,
badges: {
verified: true,
supporter: true,
},
tags: {
hguowewer: true,
},
...unimportantUserDetails,
} as IUserDB

const user = {
_lastActive: 'today',
about: 'New about description.',
displayName: 'new',
profileType: 'space',
workspaceType: 'extrusion',
userImage: {
downloadUrl: 'https://more.new/image.jpg',
},
} as IUploadedFileMeta,
badges: {
verified: false,
supporter: false,
},
tags: {
hguowewer: true,
'76khbrw': true,
},
...unimportantUserDetails,
} as IUserDB

expect(hasDetailsChanged(prevUser, user)).toEqual([true, true, true, true])
Expand Down Expand Up @@ -99,7 +138,6 @@ describe('hasDetailsForCommentsChanged', () => {
userImage: {
downloadUrl: 'http://etc.',
},

badges: {
verified: true,
supporter: false,
Expand Down Expand Up @@ -196,3 +234,61 @@ describe('hasUserImageChanged', () => {
expect(hasUserImageChanged(prevUser, user)).toEqual(true)
})
})

describe('hasUserTagsChanged', () => {
it('returns false when nothing has changed', () => {
const user = {
displayName: 'displayName',
profileType: 'member',
tags: {
gyi: false,
bnhjo: true,
},
...unimportantUserDetails,
} as IUserDB
expect(hasUserTagsChanged(user, user)).toEqual(false)
})

it('returns true when a tag is added', () => {
const prevUser = {
displayName: 'displayName',
profileType: 'member',
tags: {
gyi: false,
},
...unimportantUserDetails,
} as IUserDB

const user = {
displayName: 'displayName',
profileType: 'member',
tags: {
gyi: false,
bnhjo: true,
},
...unimportantUserDetails,
} as IUserDB
expect(hasUserTagsChanged(prevUser, user)).toEqual(true)
})

it('returns true when a tag is changed', () => {
const prevUser = {
displayName: 'displayName',
profileType: 'member',
tags: {
gyi: false,
},
...unimportantUserDetails,
} as IUserDB

const user = {
displayName: 'displayName',
profileType: 'member',
tags: {
gyi: true,
},
...unimportantUserDetails,
} as IUserDB
expect(hasUserTagsChanged(prevUser, user)).toEqual(true)
})
})
8 changes: 8 additions & 0 deletions functions/src/userUpdates/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const hasDetailsForMapPinChanged = (
prevUser.isContactableByPublic !== user.isContactableByPublic,
prevUser.profileType !== user.profileType,
prevUser.workspaceType !== user.workspaceType,
hasUserTagsChanged(prevUser, user),
...hasDetailsChanged(prevUser, user),
...hasLocationDetailsChanged(prevUser, user),
]
Expand All @@ -66,6 +67,13 @@ export const hasUserImageChanged = (
if (!prevUser.userImage && user.userImage) return true
}

export const hasUserTagsChanged = (
prevUser: IUserDB,
user: IUserDB,
): boolean => {
return !valuesAreDeepEqual(prevUser.tags, user.tags)
}

export const getCreatorImage = (userImage: IUserDB['userImage']) => {
return userImage?.downloadUrl || null
}
Expand Down

0 comments on commit 054f10b

Please sign in to comment.