Skip to content

Commit

Permalink
feat(share): Manage the case of a contact that is already in
Browse files Browse the repository at this point in the history
  • Loading branch information
cballevre committed May 2, 2024
1 parent bc70d04 commit 7c979af
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/cozy-sharing/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"errors": {
"errors": {
"groupMemberAlreadyInSharing": "%{groupName} members have already been invited with %{sharingRights} rights. It is not possible to invite the same contact twice with different rights. |||| Contacts have already been invited with %{sharingRights} rights. It is not possible to invite the same contact twice with different rights.",
"contactAlreadyInSharing": "%{contactName} has already been invited in a group with %{sharingRights} rights. It is not possible to invite the same contact twice with different rights. |||| Contacts have already been invited with %{sharingRights} rights. It is not possible to invite the same contact twice with different rights.",
"sharingRights": {
"readOnly": "read",
"readWrite": "write"
Expand Down
1 change: 1 addition & 0 deletions packages/cozy-sharing/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
},
"errors": {
"groupMemberAlreadyInSharing": "Des membres de %{groupName} ont déjà été invités avec des droits de %{sharingRights}. Impossible d’inviter 2 fois le même contact avec des droits différents. |||| Des contacts ont déjà été invités avec des droits de %{sharingRights}. Impossible d’inviter 2 fois le même contact avec des droits différents.",
"contactAlreadyInSharing": "%{contactName} a déjà été invité dans un groupe avec des droits de %{sharingRights}. Impossible d’inviter 2 fois le même contact avec des droits différents. |||| Des contacts ont déjà été invités avec des droits de %{sharingRights}. Impossible d’inviter 2 fois le même contact avec des droits différents.",
"sharingRights": {
"readOnly": "lecture",
"readWrite": "modification"
Expand Down
24 changes: 20 additions & 4 deletions packages/cozy-sharing/src/helpers/share.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { models } from 'cozy-client'

import { Contact, Group } from '../models'
import { Contact, Group, getDisplayName } from '../models'

export const countNewRecipients = (currentRecipients, newRecipients) => {
const newRecipientsNotAlreadyIncluded = newRecipients.filter(recipient => {
Expand Down Expand Up @@ -119,14 +119,30 @@ export const getErrorMessage = ({
if (
detail.startsWith('A group member cannot be added as they are already in')
) {
const sharingRights =
selectedOption === 'readOnly' ? 'readWrite' : 'readOnly'
const sharingRights = t(
`Share.errors.sharingRights.${
selectedOption === 'readOnly' ? 'readWrite' : 'readOnly'
}`
)

if (recipients.length === 1 && recipients[0]._type === Contact.doctype) {
return [
'Share.errors.contactAlreadyInSharing',
{
smart_count: recipients.length,
contactName: getDisplayName(recipients[0]),
sharingRights,
icon: false
}
]
}

return [
'Share.errors.groupMemberAlreadyInSharing',
{
smart_count: recipients.length,
groupName: recipients[0].name,
sharingRights: t(`Share.errors.sharingRights.${sharingRights}`),
sharingRights,
icon: false
}
]
Expand Down
36 changes: 36 additions & 0 deletions packages/cozy-sharing/src/helpers/share.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,42 @@ describe('share', () => {
])
})

it('should return specific text if there is only one recipient and it is a contact', () => {
const err = {
name: 'FetchError',
status: 400,
message: JSON.stringify({
errors: [
{ detail: 'A group member cannot be added as they are already in' }
]
})
}
const result = getErrorMessage({
t,
err,
documentType,
recipients: [
{
_type: 'io.cozy.contacts',
name: {
givenName: 'John',
familyName: 'Doe'
}
}
],
selectedOption
})
expect(result).toEqual([
'Share.errors.contactAlreadyInSharing',
{
smart_count: recipients.length,
contactName: 'John Doe',
sharingRights: 'Share.errors.sharingRights.readWrite',
icon: false
}
])
})

it('should return generic error message for FetchError with status 400 and different detail', () => {
const err = {
name: 'FetchError',
Expand Down

0 comments on commit 7c979af

Please sign in to comment.