From 243722ea635337c9764ebe5ef551ae27e2d29e40 Mon Sep 17 00:00:00 2001 From: AlexisG Date: Sat, 16 Nov 2024 10:48:15 +0100 Subject: [PATCH] fix: HasMany helpers doesn't work when remove deep values See https://github.com/cozy/cozy-client/issues/1560 --- src/connections/relatedRelationships.js | 33 +++++++-- src/connections/relatedRelationships.spec.js | 73 ++++++++++++++++++++ 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/src/connections/relatedRelationships.js b/src/connections/relatedRelationships.js index b87c22f98..0187c8a96 100644 --- a/src/connections/relatedRelationships.js +++ b/src/connections/relatedRelationships.js @@ -165,6 +165,31 @@ export const createRelatedContact = async ({ } } +/** + * setHasManyItem/updateHasManyItem do not work when remove one or many metadata.relationTypes + * @param {Object} params + * @param {import('cozy-client/types/types').IOCozyContact} params.relatedContact - Related contact + * @param {string} params.originalContactId - Original contact id + * @param {import('../types').RelatedRelationships} params.relation - Relation to update + */ +const updateRelatedRelationshipsWithoutMerge = ({ + relatedContact, + originalContactId, + relation +}) => { + const updatedRelatedContact = structuredClone(relatedContact) + const relatedIndex = + updatedRelatedContact.relationships.related.data.findIndex( + el => el._id === originalContactId + ) + + if (relatedIndex >= 0) { + updatedRelatedContact.relationships.related.data[relatedIndex] = relation + } + + return updatedRelatedContact +} + /** * @param {Object} params * @param {import('cozy-client/types/CozyClient').default} params.client - CozyClient @@ -183,12 +208,12 @@ export const updateRelatedContact = async ({ contactsQueryById.definition() ) const relationConverted = makeRelationMapping(relation, originalContactId) - const updatedRelatedContact = setHasManyItem( + + const updatedRelatedContact = updateRelatedRelationshipsWithoutMerge({ relatedContact, - 'related', originalContactId, - relationConverted - ) + relation: relationConverted + }) return updatedRelatedContact } catch (error) { diff --git a/src/connections/relatedRelationships.spec.js b/src/connections/relatedRelationships.spec.js index 4f9511237..52d2b2504 100644 --- a/src/connections/relatedRelationships.spec.js +++ b/src/connections/relatedRelationships.spec.js @@ -108,6 +108,79 @@ describe('relatedRelationships', () => { ]) }) + it('should return updated related relationships when adding relationTypes', () => { + const oldContact = { + relationships: { + related: { + data: [{ _id: 'related0', metadata: { relationTypes: ['friend'] } }] + } + } + } + const updatedContact = { + relationships: { + related: { + data: [ + { + _id: 'related0', + metadata: { relationTypes: ['friend', 'spouse'] } + } + ] + } + } + } + + expect( + getRelatedRelationshipsToUpdate(oldContact, updatedContact) + ).toEqual([ + { + relation: { + _id: 'related0', + metadata: { relationTypes: ['friend', 'spouse'] } + }, + type: 'UPDATE' + } + ]) + }) + + it('should return updated related relationships when deleting relationTypes', () => { + const oldContact = { + relationships: { + related: { + data: [ + { + _id: 'related0', + metadata: { relationTypes: ['friend', 'spouse'] } + } + ] + } + } + } + const updatedContact = { + relationships: { + related: { + data: [ + { + _id: 'related0', + metadata: { relationTypes: ['friend'] } + } + ] + } + } + } + + expect( + getRelatedRelationshipsToUpdate(oldContact, updatedContact) + ).toEqual([ + { + relation: { + _id: 'related0', + metadata: { relationTypes: ['friend'] } + }, + type: 'UPDATE' + } + ]) + }) + it('should return removed related relationships to update', () => { const oldContact = { relationships: {