Skip to content

Commit

Permalink
Merge pull request #385 from konnectors/groupsBugFix
Browse files Browse the repository at this point in the history
Groups bug fix
  • Loading branch information
LucsT authored Jan 7, 2022
2 parents 63f09a1 + c7d40fe commit 06c6049
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 82 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
"travisDeployKey": "./bin/generate_travis_deploy_key"
},
"dependencies": {
"cozy-client": "22.3.0",
"cozy-client": "27.7.0",
"cozy-konnector-libs": "4.45.0",
"node-fetch": "2.6.1",
"jest": "26.6.3",
"json-loader": "0.5.7",
"lodash": "4.17.21",
"node-fetch": "2.6.1",
"p-limit": "3.1.0"
},
"devDependencies": {
Expand Down
9 changes: 4 additions & 5 deletions src/CozyUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ class CozyUtils {
* @param {array} remoteIds
* @returns {array}
*/
async findGroups(accountId, remoteIds) {
async findGroups(accountId) {
const groupsCollection = this.client.collection(DOCTYPE_CONTACTS_GROUPS)
const resp = await groupsCollection.find(
{
cozyMetadata: {
sync: {
[accountId]: {
id: {
$in: remoteIds
contactsAccountsId: {
$in: [accountId]
}
}
}
}
},
{ indexedFields: [`cozyMetadata.sync.${accountId}.id`] }
{ indexedFields: [`cozyMetadata.sync.${accountId}.contactsAccountsId`] }
)

return get(resp, 'data')
Expand Down Expand Up @@ -150,5 +150,4 @@ class CozyUtils {
return this.client.save(params)
}
}

module.exports = CozyUtils
8 changes: 5 additions & 3 deletions src/cozyUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,16 @@ describe('CozyUtils', () => {
cozyMetadata: {
sync: {
fakeAccountId: {
id: {
$in: ['6167-7728-0938-1661', '7273-7639-1773-8379']
contactsAccountsId: {
$in: ['fakeAccountId']
}
}
}
}
},
{ indexedFields: ['cozyMetadata.sync.fakeAccountId.id'] }
{
indexedFields: ['cozyMetadata.sync.fakeAccountId.contactsAccountsId']
}
)
expect(result).toEqual([{ id: 'hufflepuff' }, { id: 'gryffindor' }])
})
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/convertStructuresToGroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const convertStructuresToGroups = structures => {
const structureName = get(structure, 'name')
const structureGroups = get(structure, 'groups', [])

// uuid construction has been changed from gid to name of the group
// old uuid was `${structureId}-${structureGroup.gid}`
structureGroups.forEach(structureGroup => {
groups.push({
uuid: `${structureId}-${structureGroup.gid}`,
uuid: `${structureId}-${structureGroup.name}`,
structure: structureId,
structureName,
...structureGroup
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/convertStructuresToGroups.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Converting structures array to groups', () => {
const result = convertStructuresToGroups(structures)
expect(result).toEqual([
{
uuid: '11111111-1A',
uuid: '11111111-2018-2019 1A',
structure: '11111111',
structureName: 'HOGWARTS',
gid: '1A',
Expand All @@ -67,7 +67,7 @@ describe('Converting structures array to groups', () => {
]
},
{
uuid: '11111111-1B',
uuid: '11111111-2018-2019 1B',
structure: '11111111',
structureName: 'HOGWARTS',
gid: '1B',
Expand All @@ -79,7 +79,7 @@ describe('Converting structures array to groups', () => {
]
},
{
uuid: '22222222-2A',
uuid: '22222222-2018-2019 2A',
structure: '22222222',
structureName: 'BEAUXBATONS',
gid: '2A',
Expand All @@ -91,7 +91,7 @@ describe('Converting structures array to groups', () => {
]
},
{
uuid: '22222222-2B',
uuid: '22222222-2018-2019 2B',
structure: '22222222',
structureName: 'BEAUXBATONS',
gid: '2B',
Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ async function start(fields) {
const remoteGroups = convertStructuresToGroups(remoteStructures)
const filteredGroups = filterValidGroups(remoteGroups)

const remoteGroupsId = filteredGroups.map(({ uuid }) => uuid)
const existingCozyGroups = await cozyUtils.findGroups(
contactAccount._id,
remoteGroupsId
)
const existingCozyGroups = await cozyUtils.findGroups(contactAccount._id)

const {
groups: allConnectorGroups,
...groupsSyncResult
Expand All @@ -76,7 +73,10 @@ async function start(fields) {
log('info', `${groupsSyncResult.skipped} groups skipped`)

log('info', 'Syncing contacts')
// Acquiring contacts from API
const remoteContacts = get(remoteData, 'contacts', [])
log('debug', `${remoteContacts.length} contacts receive from API`)
// Remove duplicate contacts with same uuid
const filteredContacts = filterValidContacts(remoteContacts)

const remoteContactsWithGroups = attachGroupsToContacts(
Expand All @@ -85,7 +85,7 @@ async function start(fields) {
allConnectorGroups,
contactAccount._id
)

// Acquiring contacts from Cozy
const cozyContacts = await cozyUtils.findContacts(contactAccount._id)

const contactsSyncResult = await synchronizeContacts(
Expand Down
54 changes: 28 additions & 26 deletions src/synchronizeContacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const unset = require('lodash/unset')
const omit = require('lodash/omit')
const transpileContactToCozy = require('./helpers/transpileContactToCozy')

const customMerge = (connectorGroupIds) => (existingValue, newValue, key) => {
const customMerge = connectorGroupIds => (existingValue, newValue, key) => {
if (key === 'cozy') return cozyMergeStrategy(existingValue, newValue)
if (key === 'relationships')
return relationshipsMergeStrategy(connectorGroupIds)(
Expand All @@ -25,7 +25,7 @@ const emailMergeStrategy = (existingEmails, newEmails) => {
if (hasAnEmail) {
const emails = existingEmails || []
// Remove all emails with type Pro
remove(emails, (email) => {
remove(emails, email => {
return email.type == 'Pro'
})
// Add new Pro email
Expand All @@ -40,25 +40,27 @@ const cozyMergeStrategy = (existingCozy, newCozy) => {
else return undefined
}

const relationshipsMergeStrategy =
(connectorGroupIds) => (existingRelationships, newRelationships) => {
const otherRelationships = omit(existingRelationships, 'groups')
const existingGroups = get(existingRelationships, 'groups.data', [])
const existingManualGroups = existingGroups.filter(
({ _id: existingGroupId }) =>
!connectorGroupIds.some(
({ _id: connectorGroupId }) => connectorGroupId === existingGroupId
)
)
const newGroups = get(newRelationships, 'groups.data', [])
const relationshipsMergeStrategy = connectorGroupIds => (
existingRelationships,
newRelationships
) => {
const otherRelationships = omit(existingRelationships, 'groups')
const existingGroups = get(existingRelationships, 'groups.data', [])
const existingManualGroups = existingGroups.filter(
({ _id: existingGroupId }) =>
!connectorGroupIds.some(
({ _id: connectorGroupId }) => connectorGroupId === existingGroupId
)
)
const newGroups = get(newRelationships, 'groups.data', [])

return {
...otherRelationships,
groups: {
data: [...newGroups, ...existingManualGroups],
},
return {
...otherRelationships,
groups: {
data: [...newGroups, ...existingManualGroups]
}
}
}

const getFinalContactData = (cozyContact, remoteContact, connectorGroupIds) => {
const merged = mergeWith(
Expand All @@ -84,10 +86,10 @@ const haveRemoteFieldsChanged = (
'jobTitle',
'trashed', // always false for the remote/next contact
`cozyMetadata.sync.${contactAccountId}.id`,
'relationships.groups.data',
'relationships.groups.data'
]
return diffKeys.some(
(key) => !isEqual(get(currentContact, key), get(nextContact, key))
key => !isEqual(get(currentContact, key), get(nextContact, key))
)
}

Expand All @@ -102,12 +104,12 @@ const synchronizeContacts = async (
contacts: {
created: 0,
updated: 0,
skipped: 0,
},
skipped: 0
}
}

for (const remoteContact of remoteContacts) {
const cozyContact = cozyContacts.find((cozyContact) => {
const cozyContact = cozyContacts.find(cozyContact => {
const cozyRemoteId = get(
cozyContact,
`cozyMetadata.sync.${contactAccountId}.id`
Expand Down Expand Up @@ -141,14 +143,14 @@ const synchronizeContacts = async (
}
}

const contactsDeletedOnRemote = cozyContacts.filter((cozyContact) => {
const contactsDeletedOnRemote = cozyContacts.filter(cozyContact => {
const cozyRemoteId = get(
cozyContact,
`cozyMetadata.sync.${contactAccountId}.id`
)

return !remoteContacts.some(
(remoteContact) => cozyRemoteId === remoteContact.uuid
remoteContact => cozyRemoteId === remoteContact.uuid
)
})

Expand All @@ -159,7 +161,7 @@ const synchronizeContacts = async (

const contactWithoutConnectorGroups = {
...contactDeletedOnRemote,
relationships: onlyManualRelationships,
relationships: onlyManualRelationships
}

await cozyUtils.save(contactWithoutConnectorGroups)
Expand Down
30 changes: 29 additions & 1 deletion src/synchronizeGroups.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const get = require('lodash/get')
const set = require('lodash/set')
const transpileGroupToCozy = require('./helpers/transpileGroupToCozy')

const synchronizeGroups = async (
Expand All @@ -14,6 +15,7 @@ const synchronizeGroups = async (
groups: []
}
for (const remoteGroup of remoteGroups) {
const remoteOldId = remoteGroup.structure + '-' + remoteGroup.gid
const transpiledGroup = transpileGroupToCozy(remoteGroup, contactAccountId)
const remoteIdKey = `cozyMetadata.sync.${contactAccountId}.id`
const remoteId = get(transpiledGroup, remoteIdKey)
Expand All @@ -23,7 +25,33 @@ const synchronizeGroups = async (
return cozyRemoteId === remoteId
})

if (!cozyGroup) {
const cozyGroupWithOldId = cozyGroups.find(group => {
const cozyRemoteId = get(group, remoteIdKey)
return cozyRemoteId === remoteOldId
})

if (!cozyGroup && cozyGroupWithOldId) {
const creationDate = Date.parse(
get(cozyGroupWithOldId, 'cozyMetadata.createdAt')
)
// This date was especially choosen because of api opening the 13Sept2021
if (creationDate < Date.parse('2021-09-10')) {
const created = await cozyUtils.save(transpiledGroup)
result.groups.push(created.data)
result.created++
} else {
// Update
let updatedGroup = cozyGroupWithOldId
set(
updatedGroup,
`cozyMetadata.sync.${contactAccountId}.id`,
remoteGroup.structure + '-' + remoteGroup.name
)
const updated = await cozyUtils.save(updatedGroup)
result.groups.push(updated.data)
result.updated++
}
} else if (!cozyGroup) {
const created = await cozyUtils.save(transpiledGroup)
result.groups.push(created.data)
result.created++
Expand Down
Loading

0 comments on commit 06c6049

Please sign in to comment.