Skip to content

Commit

Permalink
fix(mespapiers): makeContactFlexsearchProps must handle string emails
Browse files Browse the repository at this point in the history
On the connector side, the `email` field of the
`Contact` object could be a string instead of an object array.
Issue fixed here: konnectors/libs#987
Pending the propagation of the fix,
it is relevant to predict the case here.
  • Loading branch information
Merkur39 committed Jan 2, 2024
1 parent ea47838 commit 679cd8a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/cozy-mespapiers-lib/src/components/Search/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,16 @@ export const makeFileFlexsearchProps = ({ doc, scannerT, t }) => ({
}
})

/**
* @param {object} doc - A contact
* @param {object} t - The translation function
* @returns {object} - The flexsearch props for a contact
*/
export const makeContactFlexsearchProps = (doc, t) => {
const flexsearchEmailAddresses = doc.email
// TODO On the connector side, the `email` field of the `Contact` object could be a string instead of an object array. Issue fixed here: https://github.com/konnectors/libs/pull/987. Pending the propagation of the fix, it is relevant to predict the case here
const normalizeEmail =
typeof doc.email === 'string' ? [{ address: doc.email }] : doc.email
const flexsearchEmailAddresses = normalizeEmail
?.map(email => email.address)
.reduce((acc, val, idx) => ({ ...acc, [`email[${idx}].address`]: val }), {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
makeFileTags,
makeContactTags,
makeFileFlexsearchProps,
makeMultipleSearchResultIds
makeMultipleSearchResultIds,
makeContactFlexsearchProps
} from './helpers'
import { index } from './search'

Expand All @@ -18,6 +19,48 @@ jest.mock('./search', () => ({

const mockT = x => x

describe('makeContactFlexsearchProps', () => {
const expectedResult = {
'address[0].formattedAddress': '2 place Victor Hugo',
'email[0].address': '[email protected]',
'phone[0].number': '0123456789',
tag: ['identity', 'home', 'work_study'],
translated: {
address: 'Search.attributeLabel.address',
email: 'Search.attributeLabel.email',
phone: 'Search.attributeLabel.phone'
}
}
it('should return correct formatted contact for flexsearch', () => {
const res = makeContactFlexsearchProps(
{
name: { givenName: 'Victor', familyName: 'Hugo' },
email: [{ address: '[email protected]' }],
phone: [{ number: '0123456789' }],
company: 'Cozy',
address: [{ formattedAddress: '2 place Victor Hugo' }]
},
mockT
)

expect(res).toStrictEqual(expectedResult)
})
it('should return correct formatted contact for flexsearch when email is a String', () => {
const res = makeContactFlexsearchProps(
{
name: { givenName: 'Victor', familyName: 'Hugo' },
email: '[email protected]',
phone: [{ number: '0123456789' }],
company: 'Cozy',
address: [{ formattedAddress: '2 place Victor Hugo' }]
},
mockT
)

expect(res).toStrictEqual(expectedResult)
})
})

describe('makeRealtimeConnection', () => {
it('should return a well structured object', () => {
const res = makeRealtimeConnection(
Expand Down

0 comments on commit 679cd8a

Please sign in to comment.