Skip to content

Commit

Permalink
fix: HasMany helpers doesn't work when remove deep values
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed Nov 18, 2024
1 parent b1cf0b1 commit 243722e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/connections/relatedRelationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
73 changes: 73 additions & 0 deletions src/connections/relatedRelationships.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 243722e

Please sign in to comment.