cozy-sharing 13.1.2 → 13.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,33 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [13.3.0](https://github.com/cozy/cozy-libs/compare/cozy-sharing@13.2.0...cozy-sharing@13.3.0) (2024-05-02)
7
+
8
+
9
+ ### Features
10
+
11
+ * **share:** Manage the case of a contact that is already in ([7c979af](https://github.com/cozy/cozy-libs/commit/7c979af7efebd906e45f1e83b43414bf2cb5b984))
12
+
13
+
14
+
15
+
16
+
17
+ # [13.2.0](https://github.com/cozy/cozy-libs/compare/cozy-sharing@13.1.2...cozy-sharing@13.2.0) (2024-04-18)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * Only show add email suggestion when we can't send an email ([9212597](https://github.com/cozy/cozy-libs/commit/92125976900b939e985330682f9375ce22f2ab42))
23
+
24
+
25
+ ### Features
26
+
27
+ * Allow to add empty group ([a16f535](https://github.com/cozy/cozy-libs/commit/a16f535008a4dbe9426ee026acdbd413280585dd))
28
+
29
+
30
+
31
+
32
+
6
33
  ## [13.1.2](https://github.com/cozy/cozy-libs/compare/cozy-sharing@13.1.1...cozy-sharing@13.1.2) (2024-04-17)
7
34
 
8
35
 
@@ -1,5 +1,8 @@
1
1
  import React from 'react';
2
+ import { useClient, generateWebLink } from 'cozy-client';
3
+ import Buttons from 'cozy-ui/transpiled/react/Buttons';
2
4
  import { Dialog } from 'cozy-ui/transpiled/react/CozyDialogs';
5
+ import Empty from 'cozy-ui/transpiled/react/Empty';
3
6
  import Typography from 'cozy-ui/transpiled/react/Typography';
4
7
  import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n';
5
8
  import { GroupRecipientDetailWithAccess } from './GroupRecipientDetailWithAccess';
@@ -11,6 +14,7 @@ var GroupRecipientDetail = function GroupRecipientDetail(_ref) {
11
14
  members = _ref.members,
12
15
  onClose = _ref.onClose,
13
16
  isOwner = _ref.isOwner;
17
+ var client = useClient();
14
18
 
15
19
  var _useI18n = useI18n(),
16
20
  t = _useI18n.t;
@@ -22,6 +26,12 @@ var GroupRecipientDetail = function GroupRecipientDetail(_ref) {
22
26
  return ['revoked', 'mail-not-sent'].includes(member.status);
23
27
  });
24
28
  var ownerName = owner.public_name;
29
+ var contactsLink = generateWebLink({
30
+ cozyUrl: client.getStackClient().uri,
31
+ slug: 'contacts',
32
+ pathname: '/',
33
+ subDomainType: client.getInstanceOptions().subdomain
34
+ });
25
35
  return /*#__PURE__*/React.createElement(Dialog, {
26
36
  open: true,
27
37
  size: "small",
@@ -33,7 +43,16 @@ var GroupRecipientDetail = function GroupRecipientDetail(_ref) {
33
43
  })) : null),
34
44
  content: /*#__PURE__*/React.createElement("div", {
35
45
  className: "u-flex u-stack-xs u-flex-column"
36
- }, withAccess.length > 0 ? /*#__PURE__*/React.createElement(GroupRecipientDetailWithAccess, {
46
+ }, members.length === 0 ? /*#__PURE__*/React.createElement(Empty, {
47
+ text: t('GroupRecipientDetail.empty.content')
48
+ }, /*#__PURE__*/React.createElement(Buttons, {
49
+ component: "a",
50
+ className: "u-mt-1",
51
+ href: contactsLink,
52
+ target: "_blank",
53
+ rel: "noopener noreferrer",
54
+ label: t('GroupRecipientDetail.empty.action')
55
+ })) : null, withAccess.length > 0 ? /*#__PURE__*/React.createElement(GroupRecipientDetailWithAccess, {
37
56
  withAccess: withAccess
38
57
  }) : null, withoutAccess.length > 0 ? /*#__PURE__*/React.createElement(GroupRecipientDetailWithoutAccess, {
39
58
  withoutAccess: withoutAccess,
@@ -41,7 +41,7 @@ var GroupRecipientDetailWithoutAccess = function GroupRecipientDetailWithoutAcce
41
41
  style: {
42
42
  marginRight: '0.25rem'
43
43
  }
44
- }) : null, t("GroupRecipientDetail.withoutAccess.status.".concat(recipient.status)), isOwner ? " - ".concat(t('GroupRecipientDetail.withoutAccess.addEmail')) : null)
44
+ }) : null, t("GroupRecipientDetail.withoutAccess.status.".concat(recipient.status)), isOwner && recipient.status === 'mail-not-sent' ? " - ".concat(t('GroupRecipientDetail.withoutAccess.addEmail')) : null)
45
45
  }));
46
46
  })));
47
47
  };
@@ -14,7 +14,7 @@ import ShareSubmit from './Sharesubmit';
14
14
  import ShareTypeSelect from './Sharetypeselect';
15
15
  import { getOrCreateFromArray } from '../helpers/contacts';
16
16
  import { mergeRecipients, spreadGroupAndMergeRecipients, hasReachRecipientsLimit } from '../helpers/recipients';
17
- import { getSuccessMessage } from '../helpers/successMessage';
17
+ import { getSuccessMessage, getErrorMessage } from '../helpers/share';
18
18
  import { isReadOnlySharing } from '../state';
19
19
  var styles = {
20
20
  "share-modal-content": "share__share-modal-content___1iqEq",
@@ -99,7 +99,7 @@ export var ShareByEmail = function ShareByEmail(_ref) {
99
99
 
100
100
  var share = /*#__PURE__*/function () {
101
101
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
102
- var recipientsBefore, contacts, readWriteRecipients, readOnlyRecipients, showGenericAlert, detail, sharingRights;
102
+ var recipientsBefore, contacts, readWriteRecipients, readOnlyRecipients;
103
103
  return _regeneratorRuntime.wrap(function _callee$(_context) {
104
104
  while (1) switch (_context.prev = _context.next) {
105
105
  case 0:
@@ -155,54 +155,34 @@ export var ShareByEmail = function ShareByEmail(_ref) {
155
155
  severity: 'success',
156
156
  variant: 'filled'
157
157
  });
158
- _context.next = 25;
158
+ _context.next = 22;
159
159
  break;
160
160
 
161
161
  case 19:
162
162
  _context.prev = 19;
163
163
  _context.t0 = _context["catch"](7);
164
- showGenericAlert = true;
165
-
166
- if (_context.t0.name === 'FetchError' && _context.t0.status === 400) {
167
- detail = JSON.parse(_context.t0.message).errors[0].detail;
168
-
169
- if (detail.startsWith('A group member cannot be added as they are already in')) {
170
- // To know the sharing rights of the other group
171
- sharingRights = selectedOption === 'readOnly' ? 'readWrite' : 'readOnly';
172
- showAlert({
173
- message: t('Share.errors.groupMemberAlreadyInSharing', {
174
- smart_count: recipients.length,
175
- groupName: recipients[0].name,
176
- sharingRights: t("Share.errors.sharingRights.".concat(sharingRights)),
177
- icon: false
178
- }),
179
- severity: 'error',
180
- variant: 'filled'
181
- });
182
- showGenericAlert = false;
183
- }
184
- }
185
-
186
- if (showGenericAlert) {
187
- showAlert({
188
- message: t("".concat(documentType, ".share.error.generic")),
189
- severity: 'error',
190
- variant: 'filled'
191
- });
192
- }
193
-
194
- throw _context.t0;
164
+ showAlert({
165
+ message: t.apply(void 0, _toConsumableArray(getErrorMessage({
166
+ t: t,
167
+ err: _context.t0,
168
+ documentType: documentType,
169
+ recipients: recipients,
170
+ selectedOption: selectedOption
171
+ }))),
172
+ severity: 'error',
173
+ variant: 'filled'
174
+ });
195
175
 
196
- case 25:
197
- _context.prev = 25;
176
+ case 22:
177
+ _context.prev = 22;
198
178
  reset();
199
- return _context.finish(25);
179
+ return _context.finish(22);
200
180
 
201
- case 28:
181
+ case 25:
202
182
  case "end":
203
183
  return _context.stop();
204
184
  }
205
- }, _callee, null, [[7, 19, 25, 28]]);
185
+ }, _callee, null, [[7, 19, 22, 25]]);
206
186
  }));
207
187
 
208
188
  return function share() {
@@ -11,7 +11,7 @@ import { useQueryAll, isQueryLoading } from 'cozy-client';
11
11
  import flag from 'cozy-flags';
12
12
  import ShareAutosuggest from './ShareAutosuggest';
13
13
  import { getContactsFromGroupId } from '../helpers/contacts';
14
- import { buildReachableContactsQuery, buildContactGroupsByIdsQuery, buildUnreachableContactsWithGroupsQuery } from '../queries/queries';
14
+ import { buildReachableContactsQuery, buildContactGroupsQuery, buildUnreachableContactsWithGroupsQuery } from '../queries/queries';
15
15
  /**
16
16
  * ShareRecipientsInput is responsible for fetching contacts and groups.
17
17
  * We retrieve all the contacts that are reachable and the groups they belong to.
@@ -30,20 +30,14 @@ var ShareRecipientsInput = function ShareRecipientsInput(_ref) {
30
30
  var unreachableContactsWithGroupsResult = useQueryAll(unreachableContactsWithGroupsQuery.definition, _objectSpread(_objectSpread({}, unreachableContactsWithGroupsQuery.options), {}, {
31
31
  enabled: !!flag('sharing.show-recipient-groups')
32
32
  }));
33
- var contactGroupsByIdsQuery = buildContactGroupsByIdsQuery(_toConsumableArray(new Set((reachableContactsResult.data || []).flatMap(function (contact) {
34
- var _contact$relationship;
35
-
36
- return (((_contact$relationship = contact.relationships) === null || _contact$relationship === void 0 || (_contact$relationship = _contact$relationship.groups) === null || _contact$relationship === void 0 ? void 0 : _contact$relationship.data) || []).map(function (group) {
37
- return group._id;
38
- });
39
- }))));
40
- var contactGroupsByIdsResult = useQueryAll(contactGroupsByIdsQuery.definition, contactGroupsByIdsQuery.options);
41
- var isLoading = isQueryLoading(reachableContactsResult) || reachableContactsResult.hasMore || isQueryLoading(contactGroupsByIdsResult) || contactGroupsByIdsResult.hasMore || flag('sharing.show-recipient-groups') && (isQueryLoading(unreachableContactsWithGroupsResult) || unreachableContactsWithGroupsResult.hasMore);
33
+ var contactGroupsQuery = buildContactGroupsQuery();
34
+ var contactGroupsResult = useQueryAll(contactGroupsQuery.definition, contactGroupsQuery.options);
35
+ var isLoading = isQueryLoading(reachableContactsResult) || reachableContactsResult.hasMore || isQueryLoading(contactGroupsResult) || contactGroupsResult.hasMore || flag('sharing.show-recipient-groups') && (isQueryLoading(unreachableContactsWithGroupsResult) || unreachableContactsWithGroupsResult.hasMore);
42
36
  var currentRecipientGroups = currentRecipients.map(function (_ref2) {
43
37
  var id = _ref2.id;
44
38
  return id;
45
39
  }).filter(Boolean);
46
- var contactGroups = (contactGroupsByIdsResult.data || []).filter(function (_ref3) {
40
+ var contactGroups = (contactGroupsResult.data || []).filter(function (_ref3) {
47
41
  var _id = _ref3._id;
48
42
  return !flag('sharing.show-recipient-groups') || !currentRecipientGroups.includes(_id);
49
43
  }).map(function (contactGroup) {
@@ -44,7 +44,7 @@ describe('ShareRecipientsInput component', function () {
44
44
  doctype: 'io.cozy.contacts',
45
45
  data: contacts
46
46
  },
47
- 'io.cozy.contacts.groups/by-ids/fe86af20-c6c5': {
47
+ 'io.cozy.contacts.groups': {
48
48
  doctype: 'io.cozy.contacts.groups',
49
49
  data: contactGroups
50
50
  },
@@ -5,7 +5,7 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
5
5
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
6
 
7
7
  import { models } from 'cozy-client';
8
- import { Contact, Group } from '../models';
8
+ import { Contact, Group, getDisplayName } from '../models';
9
9
  export var countNewRecipients = function countNewRecipients(currentRecipients, newRecipients) {
10
10
  var newRecipientsNotAlreadyIncluded = newRecipients.filter(function (recipient) {
11
11
  if (Contact.isContact(recipient)) {
@@ -75,4 +75,37 @@ export var getSuccessMessage = function getSuccessMessage(recipientsBefore, reci
75
75
  }];
76
76
  }
77
77
  }
78
+ };
79
+ export var getErrorMessage = function getErrorMessage(_ref) {
80
+ var t = _ref.t,
81
+ err = _ref.err,
82
+ documentType = _ref.documentType,
83
+ recipients = _ref.recipients,
84
+ selectedOption = _ref.selectedOption;
85
+
86
+ if (err.name === 'FetchError' && err.status === 400) {
87
+ var detail = JSON.parse(err.message).errors[0].detail;
88
+
89
+ if (detail.startsWith('A group member cannot be added as they are already in')) {
90
+ var sharingRights = t("Share.errors.sharingRights.".concat(selectedOption === 'readOnly' ? 'readWrite' : 'readOnly'));
91
+
92
+ if (recipients.length === 1 && recipients[0]._type === Contact.doctype) {
93
+ return ['Share.errors.contactAlreadyInSharing', {
94
+ smart_count: recipients.length,
95
+ contactName: getDisplayName(recipients[0]),
96
+ sharingRights: sharingRights,
97
+ icon: false
98
+ }];
99
+ }
100
+
101
+ return ['Share.errors.groupMemberAlreadyInSharing', {
102
+ smart_count: recipients.length,
103
+ groupName: recipients[0].name,
104
+ sharingRights: sharingRights,
105
+ icon: false
106
+ }];
107
+ }
108
+ }
109
+
110
+ return ["".concat(documentType, ".share.error.generic")];
78
111
  };
@@ -0,0 +1,385 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { getSuccessMessage, countNewRecipients, getErrorMessage } from './share';
3
+ describe('share', function () {
4
+ describe('getSuccessMessage', function () {
5
+ var props = {
6
+ currentRecipients: [{
7
+ email: 'sansa.stark@winterfell.westeros'
8
+ }],
9
+ documentType: 'Files',
10
+ sharingDesc: 'fake-doc.odt',
11
+ onShare: jest.fn(),
12
+ createContact: jest.fn()
13
+ };
14
+ it('should return a success message and its params for multiple recipients', function () {
15
+ var recipientsAfter = [{
16
+ _type: 'io.cozy.contacts',
17
+ email: 'jon.snow@thewall.westeros'
18
+ }, {
19
+ _type: 'io.cozy.contacts',
20
+ email: 'arya.stark@winterfell.westeros'
21
+ }, {
22
+ _type: 'io.cozy.contacts',
23
+ email: 'sansa.stark@winterfell.westeros'
24
+ }];
25
+
26
+ var _getSuccessMessage = getSuccessMessage(props.currentRecipients, recipientsAfter, props.documentType),
27
+ _getSuccessMessage2 = _slicedToArray(_getSuccessMessage, 2),
28
+ message = _getSuccessMessage2[0],
29
+ params = _getSuccessMessage2[1];
30
+
31
+ expect(message).toEqual('Files.share.shareByEmail.contactSuccess');
32
+ expect(params).toEqual({
33
+ smart_count: 2
34
+ });
35
+ });
36
+ it('should return a success message and its params for one recipient with email', function () {
37
+ var recipientsBefore = props.currentRecipients;
38
+
39
+ var _getSuccessMessage3 = getSuccessMessage(recipientsBefore, [{
40
+ email: 'jon.snow@thewall.westeros'
41
+ }], props.documentType),
42
+ _getSuccessMessage4 = _slicedToArray(_getSuccessMessage3, 2),
43
+ message = _getSuccessMessage4[0],
44
+ params = _getSuccessMessage4[1];
45
+
46
+ expect(message).toEqual('Files.share.shareByEmail.singleContactSuccess');
47
+ expect(params).toEqual({
48
+ email: 'jon.snow@thewall.westeros'
49
+ });
50
+ });
51
+ it('should return a success message and its params for one recipient (contact) with email', function () {
52
+ var recipientsBefore = props.currentRecipients;
53
+
54
+ var _getSuccessMessage5 = getSuccessMessage(recipientsBefore, [{
55
+ _id: 'e8c0e15f-da7d',
56
+ _type: 'io.cozy.contacts',
57
+ fullname: 'Jon Snow',
58
+ email: [{
59
+ address: 'jon.snow@thewall.westeros',
60
+ primary: true
61
+ }, {
62
+ address: 'jon.snow@winterfell.westeros',
63
+ primary: false
64
+ }]
65
+ }], props.documentType),
66
+ _getSuccessMessage6 = _slicedToArray(_getSuccessMessage5, 2),
67
+ message = _getSuccessMessage6[0],
68
+ params = _getSuccessMessage6[1];
69
+
70
+ expect(message).toEqual('Files.share.shareByEmail.singleContactSuccess');
71
+ expect(params).toEqual({
72
+ email: 'jon.snow@thewall.westeros'
73
+ });
74
+ });
75
+ it('should return a success message and its params for one recipient with cozy', function () {
76
+ var recipientsBefore = props.currentRecipients;
77
+
78
+ var _getSuccessMessage7 = getSuccessMessage(recipientsBefore, [{
79
+ cozy: [{
80
+ url: 'https://doranmartell.mycozy.cloud'
81
+ }]
82
+ }], props.documentType),
83
+ _getSuccessMessage8 = _slicedToArray(_getSuccessMessage7, 2),
84
+ message = _getSuccessMessage8[0],
85
+ params = _getSuccessMessage8[1];
86
+
87
+ expect(message).toEqual('Files.share.shareByEmail.singleContactSuccess');
88
+ expect(params).toEqual({
89
+ email: 'https://doranmartell.mycozy.cloud'
90
+ });
91
+ });
92
+ it('should use contact success message if recipient has no email and no cozy', function () {
93
+ var recipientsBefore = props.currentRecipients;
94
+
95
+ var _getSuccessMessage9 = getSuccessMessage(recipientsBefore, [{
96
+ _id: 'fcb16b9e-7421'
97
+ }], props.documentType),
98
+ _getSuccessMessage10 = _slicedToArray(_getSuccessMessage9, 2),
99
+ message = _getSuccessMessage10[0],
100
+ params = _getSuccessMessage10[1];
101
+
102
+ expect(message).toEqual('Files.share.shareByEmail.contactSuccess');
103
+ expect(params).toEqual({
104
+ smart_count: 1
105
+ });
106
+ });
107
+ it('should use single group success message if recipient is a group', function () {
108
+ var recipientsBefore = props.currentRecipients;
109
+
110
+ var _getSuccessMessage11 = getSuccessMessage(recipientsBefore, [{
111
+ _type: 'io.cozy.contacts.groups',
112
+ name: 'Stark family'
113
+ }], props.documentType),
114
+ _getSuccessMessage12 = _slicedToArray(_getSuccessMessage11, 2),
115
+ message = _getSuccessMessage12[0],
116
+ params = _getSuccessMessage12[1];
117
+
118
+ expect(message).toEqual('Files.share.shareByEmail.singleGroupSuccess');
119
+ expect(params).toEqual({
120
+ groupName: 'Stark family'
121
+ });
122
+ });
123
+ it('should use contact success message if recipient has only multiple contacts', function () {
124
+ var recipientsBefore = props.currentRecipients;
125
+
126
+ var _getSuccessMessage13 = getSuccessMessage(recipientsBefore, [{
127
+ _type: 'io.cozy.contacts'
128
+ }, {
129
+ _type: 'io.cozy.contacts'
130
+ }, {
131
+ _type: 'io.cozy.contacts'
132
+ }], props.documentType),
133
+ _getSuccessMessage14 = _slicedToArray(_getSuccessMessage13, 2),
134
+ message = _getSuccessMessage14[0],
135
+ params = _getSuccessMessage14[1];
136
+
137
+ expect(message).toEqual('Files.share.shareByEmail.contactSuccess');
138
+ expect(params).toEqual({
139
+ smart_count: 3
140
+ });
141
+ });
142
+ it('should use group success message if recipient has only multiple groups', function () {
143
+ var recipientsBefore = props.currentRecipients;
144
+
145
+ var _getSuccessMessage15 = getSuccessMessage(recipientsBefore, [{
146
+ _type: 'io.cozy.contacts.groups'
147
+ }, {
148
+ _type: 'io.cozy.contacts.groups'
149
+ }], props.documentType),
150
+ _getSuccessMessage16 = _slicedToArray(_getSuccessMessage15, 2),
151
+ message = _getSuccessMessage16[0],
152
+ params = _getSuccessMessage16[1];
153
+
154
+ expect(message).toEqual('Files.share.shareByEmail.groupSuccess');
155
+ expect(params).toEqual({
156
+ smart_count: 2
157
+ });
158
+ });
159
+ it('should use contact and group success message if recipient has multiple contacts and groups', function () {
160
+ var recipientsBefore = props.currentRecipients;
161
+
162
+ var _getSuccessMessage17 = getSuccessMessage(recipientsBefore, [{
163
+ _type: 'io.cozy.contacts'
164
+ }, {
165
+ _id: 'fcb16b9e-7421',
166
+ _type: 'io.cozy.contacts.groups'
167
+ }, {
168
+ _id: 'f45b16be-7523',
169
+ _type: 'io.cozy.contacts.groups'
170
+ }], props.documentType),
171
+ _getSuccessMessage18 = _slicedToArray(_getSuccessMessage17, 2),
172
+ message = _getSuccessMessage18[0],
173
+ params = _getSuccessMessage18[1];
174
+
175
+ expect(message).toEqual('Files.share.shareByEmail.contactAndGroupSuccess');
176
+ expect(params).toEqual({
177
+ nbContact: 1,
178
+ nbGroup: 2
179
+ });
180
+ });
181
+ });
182
+ describe('countNewRecipients', function () {
183
+ var currentRecipients = [{
184
+ email: 'arya.stark@winterfell.westeros'
185
+ }, {
186
+ email: 'sansa.stark@winterfell.westeros'
187
+ }, {
188
+ email: 'jon.snow@thewall.westeros'
189
+ }];
190
+ var addedRecipients = [{
191
+ _id: 'e8c0e15f-da7d',
192
+ _type: 'io.cozy.contacts',
193
+ fullname: 'Jon Snow',
194
+ email: [{
195
+ address: 'jon.snow@thewall.westeros',
196
+ primary: true
197
+ }, {
198
+ address: 'jon.snow@winterfell.westeros',
199
+ primary: false
200
+ }]
201
+ }, {
202
+ _id: 'fcb16b9e-7421',
203
+ _type: 'io.cozy.contacts'
204
+ }, {
205
+ _id: 'f45b16be-7523',
206
+ _type: 'io.cozy.contacts',
207
+ cozy: [{
208
+ url: 'https://doranmartell.mycozy.cloud'
209
+ }]
210
+ }];
211
+ it('should count the new recipients with email', function () {
212
+ var result = countNewRecipients(currentRecipients, addedRecipients);
213
+ expect(result.contact).toEqual(2);
214
+ expect(result.group).toEqual(0);
215
+ });
216
+ it('should count the new recipients with cozy url', function () {
217
+ var currentRecipients = [{
218
+ instance: 'https://bran.mycozy.cloud'
219
+ }, {
220
+ instance: 'https://jon.mycozy.cloud'
221
+ }];
222
+ var addedRecipients = [{
223
+ _type: 'io.cozy.contacts',
224
+ cozy: [{
225
+ url: 'https://jon.mycozy.cloud',
226
+ type: 'primary'
227
+ }]
228
+ }, {
229
+ _type: 'io.cozy.contacts',
230
+ cozy: [{
231
+ url: 'https://sansa.mycozy.cloud',
232
+ type: 'primary'
233
+ }]
234
+ }, {
235
+ _type: 'io.cozy.contacts',
236
+ cozy: [{
237
+ url: 'https://bran.mycozy.cloud',
238
+ type: 'primary'
239
+ }]
240
+ }, {
241
+ _type: 'io.cozy.contacts',
242
+ cozy: [{
243
+ url: 'https://arya.mycozy.cloud',
244
+ type: 'primary'
245
+ }]
246
+ }];
247
+ var result = countNewRecipients(currentRecipients, addedRecipients);
248
+ expect(result.contact).toEqual(2);
249
+ expect(result.group).toEqual(0);
250
+ });
251
+ it('should count the new recipients with groups', function () {
252
+ var result = countNewRecipients(currentRecipients, [].concat(addedRecipients, [{
253
+ _id: 'f45b16be-7523',
254
+ _type: 'io.cozy.contacts.groups'
255
+ }]));
256
+ expect(result.contact).toEqual(2);
257
+ expect(result.group).toEqual(1);
258
+ });
259
+ it('should return 0 if there are no new recipients', function () {
260
+ var result = countNewRecipients(currentRecipients, []);
261
+ expect(result.contact).toEqual(0);
262
+ expect(result.group).toEqual(0);
263
+ });
264
+ });
265
+ describe('getErrorMessage', function () {
266
+ var t = function t(key) {
267
+ return key;
268
+ }; // Mock translation function
269
+
270
+
271
+ var documentType = 'document';
272
+ var recipients = [{
273
+ name: 'John Doe'
274
+ }];
275
+ var selectedOption = 'readOnly';
276
+ it('should return generic error message for non-FetchError', function () {
277
+ var err = new Error('Some error');
278
+ var result = getErrorMessage({
279
+ t: t,
280
+ err: err,
281
+ documentType: documentType,
282
+ recipients: recipients,
283
+ selectedOption: selectedOption
284
+ });
285
+ expect(result).toEqual(["".concat(documentType, ".share.error.generic")]);
286
+ });
287
+ it('should return specific error message for FetchError with status 400 and specific detail', function () {
288
+ var err = {
289
+ name: 'FetchError',
290
+ status: 400,
291
+ message: JSON.stringify({
292
+ errors: [{
293
+ detail: 'A group member cannot be added as they are already in'
294
+ }]
295
+ })
296
+ };
297
+ var result = getErrorMessage({
298
+ t: t,
299
+ err: err,
300
+ documentType: documentType,
301
+ recipients: recipients,
302
+ selectedOption: selectedOption
303
+ });
304
+ expect(result).toEqual(['Share.errors.groupMemberAlreadyInSharing', {
305
+ smart_count: recipients.length,
306
+ groupName: recipients[0].name,
307
+ sharingRights: 'Share.errors.sharingRights.readWrite',
308
+ icon: false
309
+ }]);
310
+ });
311
+ it('should return opposite sharingRights for the other group when the group member already in sharing', function () {
312
+ var err = {
313
+ name: 'FetchError',
314
+ status: 400,
315
+ message: JSON.stringify({
316
+ errors: [{
317
+ detail: 'A group member cannot be added as they are already in'
318
+ }]
319
+ })
320
+ };
321
+ var result = getErrorMessage({
322
+ t: t,
323
+ err: err,
324
+ documentType: documentType,
325
+ recipients: recipients,
326
+ selectedOption: 'readWrite'
327
+ });
328
+ expect(result).toEqual(['Share.errors.groupMemberAlreadyInSharing', {
329
+ smart_count: recipients.length,
330
+ groupName: recipients[0].name,
331
+ sharingRights: 'Share.errors.sharingRights.readOnly',
332
+ icon: false
333
+ }]);
334
+ });
335
+ it('should return specific text if there is only one recipient and it is a contact', function () {
336
+ var err = {
337
+ name: 'FetchError',
338
+ status: 400,
339
+ message: JSON.stringify({
340
+ errors: [{
341
+ detail: 'A group member cannot be added as they are already in'
342
+ }]
343
+ })
344
+ };
345
+ var result = getErrorMessage({
346
+ t: t,
347
+ err: err,
348
+ documentType: documentType,
349
+ recipients: [{
350
+ _type: 'io.cozy.contacts',
351
+ name: {
352
+ givenName: 'John',
353
+ familyName: 'Doe'
354
+ }
355
+ }],
356
+ selectedOption: selectedOption
357
+ });
358
+ expect(result).toEqual(['Share.errors.contactAlreadyInSharing', {
359
+ smart_count: recipients.length,
360
+ contactName: 'John Doe',
361
+ sharingRights: 'Share.errors.sharingRights.readWrite',
362
+ icon: false
363
+ }]);
364
+ });
365
+ it('should return generic error message for FetchError with status 400 and different detail', function () {
366
+ var err = {
367
+ name: 'FetchError',
368
+ status: 400,
369
+ message: JSON.stringify({
370
+ errors: [{
371
+ detail: 'Some other detail'
372
+ }]
373
+ })
374
+ };
375
+ var result = getErrorMessage({
376
+ t: t,
377
+ err: err,
378
+ documentType: documentType,
379
+ recipients: recipients,
380
+ selectedOption: selectedOption
381
+ });
382
+ expect(result).toEqual(["".concat(documentType, ".share.error.generic")]);
383
+ });
384
+ });
385
+ });
@@ -53,13 +53,12 @@ export var buildReachableContactsQuery = function buildReachableContactsQuery()
53
53
  }
54
54
  };
55
55
  };
56
- export var buildContactGroupsByIdsQuery = function buildContactGroupsByIdsQuery(ids) {
56
+ export var buildContactGroupsQuery = function buildContactGroupsQuery() {
57
57
  return {
58
- definition: Q(Group.doctype).getByIds(ids),
58
+ definition: Q(Group.doctype),
59
59
  options: {
60
- as: "io.cozy.contacts.groups/by-ids/".concat(ids.join('/')),
61
- fetchPolicy: defaultFetchPolicy,
62
- enabled: ids.length > 0
60
+ as: "io.cozy.contacts.groups",
61
+ fetchPolicy: defaultFetchPolicy
63
62
  }
64
63
  };
65
64
  };