Skip to content

Commit

Permalink
Merge pull request #956 from cozy/fix/Order-of-contacts
Browse files Browse the repository at this point in the history
fix: Correct order of contacts
  • Loading branch information
Merkur39 committed Jan 18, 2024
2 parents adb4d05 + 99b884c commit a89807c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 deletions.
42 changes: 29 additions & 13 deletions src/helpers/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,52 @@ export const harmonizeAndSortByFamilyNameGivenNameEmailCozyUrl = (
updateIndexFullNameAndDisplayName(contact)
)
const concatedContacts = contactsWithIndexes.concat(updatedContacts)
const {
itemsFound: [mySelf],
remainingItems: contactsWithoutMySelf
} = filterWithRemaining(concatedContacts, contact => contact.me)
const sortedContacts = sortBy(contactsWithoutMySelf, [
const sortedContacts = sortBy(concatedContacts, [
'indexes.byFamilyNameGivenNameEmailCozyUrl'
])
if (mySelf) sortedContacts.unshift(mySelf)

return sortedContacts
}

/**
* Move myself to first position in contacts list
* @param {object[]} contacts - contacts list
* @returns {object[]}
*/
const moveMyselfToFirstPosition = contacts => {
const {
itemsFound: [mySelf],
remainingItems: contactsWithoutMySelf
} = filterWithRemaining(contacts, contact => contact.me)

if (mySelf) return [mySelf, ...contactsWithoutMySelf]

return contacts
}

/**
* Sort and rework contacts according to 'keepIndexFullNameAndDisplayNameUpToDate' service status
* @param {bool} hasServiceBeenLaunched - 'keepIndexFullNameAndDisplayNameUpToDate' service launch status
* @param {array} contactsWithIndexes - Contacts with indexes
* @param {array} contactsWithNoIndexes - Contacts without indexes
* @returns {array} Sorted and harmonized contacts
* @param {object[]} contactsWithIndexes - Contacts with indexes
* @param {object[]} contactsWithNoIndexes - Contacts without indexes
* @returns {object[]} Sorted and harmonized contacts
*/
export const reworkContacts = (
hasServiceBeenLaunched,
contactsWithIndexes,
contactsWithNoIndexes
) => {
const reworkedContacts = hasServiceBeenLaunched
? contactsWithIndexes.concat(contactsWithNoIndexes)
: harmonizeAndSortByFamilyNameGivenNameEmailCozyUrl(
contactsWithIndexes,
contactsWithNoIndexes
? moveMyselfToFirstPosition(
contactsWithIndexes.concat(contactsWithNoIndexes)
)
: moveMyselfToFirstPosition(
harmonizeAndSortByFamilyNameGivenNameEmailCozyUrl(
contactsWithIndexes,
contactsWithNoIndexes
)
)

return reworkedContacts
}

Expand Down
55 changes: 45 additions & 10 deletions src/helpers/contacts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('harmonizeAndSortByFamilyNameGivenNameEmailCozyUrl', () => {
familyName: 'Damon'
},
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'mattdamon'
byFamilyNameGivenNameEmailCozyUrl: 'damonmatt'
}
},
{
Expand All @@ -173,16 +173,16 @@ describe('harmonizeAndSortByFamilyNameGivenNameEmailCozyUrl', () => {
familyName: 'Doe'
},
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'johndoe'
byFamilyNameGivenNameEmailCozyUrl: 'doejohn'
}
},
{
name: {
givenName: 'Max',
familyName: 'Payne'
familyName: 'Abe'
},
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'maxpayne'
byFamilyNameGivenNameEmailCozyUrl: 'abemax'
},
me: true
}
Expand All @@ -205,9 +205,9 @@ describe('harmonizeAndSortByFamilyNameGivenNameEmailCozyUrl', () => {

const expected = [
{
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'maxpayne' },
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'abemax' },
me: true,
name: { familyName: 'Payne', givenName: 'Max' }
name: { familyName: 'Abe', givenName: 'Max' }
},
{
displayName: 'Anton Bradbury',
Expand All @@ -216,12 +216,12 @@ describe('harmonizeAndSortByFamilyNameGivenNameEmailCozyUrl', () => {
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'bradburyanton' }
},
{
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'johndoe' },
name: { familyName: 'Doe', givenName: 'John' }
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'damonmatt' },
name: { familyName: 'Damon', givenName: 'Matt' }
},
{
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'mattdamon' },
name: { familyName: 'Damon', givenName: 'Matt' }
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'doejohn' },
name: { familyName: 'Doe', givenName: 'John' }
},
{
displayName: 'William Wallace',
Expand Down Expand Up @@ -252,6 +252,16 @@ describe('reworkContacts', () => {
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'mattdamon'
}
},
{
name: {
givenName: 'Max',
familyName: 'Abe'
},
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'maxabe'
},
me: true
}
]

Expand All @@ -265,6 +275,16 @@ describe('reworkContacts', () => {
]

const expected = [
{
name: {
givenName: 'Max',
familyName: 'Abe'
},
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'maxabe'
},
me: true
},
{
displayName: 'Anton Bradbury',
fullname: 'Anton Bradbury',
Expand Down Expand Up @@ -297,6 +317,16 @@ describe('reworkContacts', () => {
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'mattdamon'
}
},
{
name: {
givenName: 'Max',
familyName: 'Abe'
},
indexes: {
byFamilyNameGivenNameEmailCozyUrl: 'maxabe'
},
me: true
}
]

Expand All @@ -310,6 +340,11 @@ describe('reworkContacts', () => {
]

const expected = [
{
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'maxabe' },
me: true,
name: { familyName: 'Abe', givenName: 'Max' }
},
{
indexes: { byFamilyNameGivenNameEmailCozyUrl: 'mattdamon' },
name: { familyName: 'Damon', givenName: 'Matt' }
Expand Down

0 comments on commit a89807c

Please sign in to comment.