Skip to content

Commit

Permalink
api organization updates were overwriting existing data from cache wh…
Browse files Browse the repository at this point in the history
…en field is null
  • Loading branch information
epipav committed Nov 10, 2023
1 parent 1519d7b commit 9d03004
Showing 1 changed file with 75 additions and 12 deletions.
87 changes: 75 additions & 12 deletions backend/src/services/organizationService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isEqual } from 'lodash'
import { websiteNormalizer } from '@crowd/common'
import { LoggerBase } from '@crowd/logging'
import { IOrganization, IOrganizationIdentity, OrganizationMergeSuggestionType } from '@crowd/types'
Expand Down Expand Up @@ -422,14 +423,42 @@ export default class OrganizationService extends LoggerBase {
// if cache exists, merge current data with cache data
// if it doesn't exist, create it from incoming data
if (cache) {
data = {
...cache,
...data,
}
cache = await organizationCacheRepository.update(cache.id, data, {
...this.options,
transaction,
// if exists in cache update it
const updateData: Partial<IOrganization> = {}
// no need to update name since it's aka primary key
const fields = [
'url',
'description',
'emails',
'logo',
'tags',
'github',
'twitter',
'linkedin',
'crunchbase',
'employees',
'location',
'website',
'type',
'size',
'headline',
'industry',
'founded',
]
fields.forEach((field) => {
if (data[field] && !isEqual(data[field], cache[field])) {
updateData[field] = data[field]
}
})
if (Object.keys(updateData).length > 0) {
cache = await organizationCacheRepository.update(cache.id, updateData, {
...this.options,
transaction,
})

cache = { ...cache, ...updateData } // Update the cached data with the new data
}

} else {
// save it to cache
cache = await organizationCacheRepository.create(
Expand Down Expand Up @@ -505,11 +534,45 @@ export default class OrganizationService extends LoggerBase {
data.displayName = cache.name
}

record = await OrganizationRepository.update(
existing.id,
{ ...data, ...cache },
{ ...this.options, transaction },
)
// if it does exists update it
const updateData: Partial<IOrganization> = {}
const fields = [
'displayName',
'description',
'emails',
'logo',
'tags',
'github',
'twitter',
'linkedin',
'crunchbase',
'employees',
'location',
'website',
'type',
'size',
'headline',
'industry',
'founded',
'attributes',
'weakIdentities',
]
fields.forEach((field) => {
if (field === 'website' && !existing.website && cache.website) {
updateData[field] = cache[field]
} else if (
field !== 'website' &&
cache[field] &&
!isEqual(cache[field], existing[field])
) {
updateData[field] = cache[field]
}
})

record = await OrganizationRepository.update(existing.id, updateData, {
...this.options,
transaction,
})
} else {
await OrganizationRepository.checkIdentities(data, this.options)

Expand Down

0 comments on commit 9d03004

Please sign in to comment.