Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Synchronize relation between contacts #1020

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/components/ContactCard/ContactForm/fieldsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,17 @@ export const fields = [
value: '{"type":"coWorker"}',
label: 'label.relationship.coWorker'
},
{
value: '{"type":"agent"}',
label: 'label.relationship.agent'
},
{
value: '{"type":"acquaintance"}',
label: 'label.relationship.acquaintance'
},
{
value: '{"type":"helper"}',
label: 'label.relationship.helper'
},
{
value: '{"type":"recipient"}',
label: 'label.relationship.recipient'
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/Modals/ContactFormModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const ContactFormModal = () => {
try {
await createOrUpdateContact({
client,
isUpdated: !!currentContact,
oldContact: currentContact,
formData,
selectedGroup
})
Expand Down
10 changes: 8 additions & 2 deletions src/components/Modals/ContactFormModal.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('ContactFormModal component', () => {

expect(createOrUpdateContact).toBeCalledWith({
client: expect.anything(),
isUpdated: false,
oldContact: undefined,
formData: expected,
selectedGroup: expect.anything()
})
Expand Down Expand Up @@ -224,7 +224,13 @@ describe('ContactFormModal component', () => {

expect(createOrUpdateContact).toHaveBeenCalledWith({
client: expect.anything(),
isUpdated: true,
oldContact: {
_id: 'ID',
name: {
familyName: 'John',
givenName: 'Doe'
}
},
formData: expected,
selectedGroup: expect.anything()
})
Expand Down
36 changes: 31 additions & 5 deletions src/connections/allContacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import uniqWith from 'lodash/uniqWith'

import minilog from 'cozy-minilog'

import {
getRelatedRelationshipsToUpdate,
updateRelatedRelationships
} from './relatedRelationships'
import { addGroupToContact } from '../helpers/contacts'
import { DOCTYPE_CONTACTS } from '../helpers/doctypes'
import { hasSelectedGroup } from '../helpers/groups'
Expand Down Expand Up @@ -77,14 +81,14 @@ export const deleteTrashedContacts = async client => {
/**
* @param {Object} params
* @param {import('cozy-client/types/CozyClient').default} params.client - CozyClient
* @param {boolean} params.isUpdated - Is the contact updated
* @param {import('cozy-client/types/types').ContactDocument} params.oldContact - Is the contact updated
* @param {Object} params.formData - Contact data
* @param {Object} params.selectedGroup - Selected group
* @returns {Promise<import('cozy-client/types/types').IOCozyContact>} - Contact
*/
export const createOrUpdateContact = async ({
client,
isUpdated,
oldContact,
formData,
selectedGroup
}) => {
Expand All @@ -93,9 +97,31 @@ export const createOrUpdateContact = async ({
if (hasSelectedGroup(selectedGroup)) {
updatedContact = addGroupToContact(updatedContact, selectedGroup)
}
return isUpdated
? client.save(updatedContact)
: client.create(DOCTYPE_CONTACTS, updatedContact)

const { data: originalContact } = oldContact
? await client.save(updatedContact)
: await client.create(DOCTYPE_CONTACTS, updatedContact)

const relatedRelationships = getRelatedRelationshipsToUpdate(
oldContact,
updatedContact
)
Merkur39 marked this conversation as resolved.
Show resolved Hide resolved

if (relatedRelationships) {
try {
const relatedContactsToSaved = await updateRelatedRelationships({
client,
relatedRelationships,
originalContactId: originalContact._id
})

client.saveAll(relatedContactsToSaved)
} catch (error) {
log.error('Error updating related contacts', error)
}
}

return originalContact
}

/**
Expand Down
Loading
Loading