Skip to content

Commit 0796026

Browse files
committed
#101 display the shared addressbook owner in the selection when creating a new contact
1 parent 339814b commit 0796026

File tree

4 files changed

+105
-21
lines changed

4 files changed

+105
-21
lines changed

src/linagora.esn.contact/app/contact/form/contact-edition-form.directive.js

+43-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ angular.module('linagora.esn.contact')
66

77
function contactEditionForm(
88
$rootScope,
9+
$q,
910
session,
11+
userAPI,
12+
userUtils,
1013
contactAddressbookDisplayService,
1114
contactAddressbookService,
1215
CONTACT_ATTRIBUTES_ORDER,
@@ -33,19 +36,48 @@ function contactEditionForm(
3336
});
3437

3538
function _fetchAvailableAddressBooks() {
36-
contactAddressbookService.listAddressbooksUserCanCreateContact().then(function(addressbooks) {
37-
return contactAddressbookDisplayService.convertShellsToDisplayShells(addressbooks, { includePriority: true });
38-
})
39-
.then(function(addressbookDisplayShells) {
40-
$scope.availableAddressbooks = contactAddressbookDisplayService.sortAddressbookDisplayShells(addressbookDisplayShells)
41-
.map(function(addressbookDisplayShell) {
42-
return {
43-
path: addressbookDisplayShell.shell.href,
44-
displayName: addressbookDisplayShell.displayName
45-
};
46-
});
39+
contactAddressbookService
40+
.listAddressbooksUserCanCreateContact()
41+
.then(_loadAdressbooksOwners)
42+
.then(addressbooks => contactAddressbookDisplayService.convertShellsToDisplayShells(
43+
addressbooks,
44+
{ includePriority: true }
45+
))
46+
.then(addressbookDisplayShells => {
47+
$scope.availableAddressbooks = contactAddressbookDisplayService
48+
.sortAddressbookDisplayShells(addressbookDisplayShells)
49+
.map(({ shell: { href, owner }, displayName }) => ({
50+
path: href,
51+
displayName,
52+
owner
53+
}));
54+
55+
console.log($scope.availableAddressbooks);
4756
});
4857
}
58+
59+
function _loadAdressbooksOwners(addressbooks) {
60+
const subscriptionOwnerIds = addressbooks
61+
.map(({ isSubscription, group, source }) => (isSubscription && !group ? source.bookId : null))
62+
.filter(Boolean) // remove null values
63+
.reduce((uniqueUserIds, userId) => (uniqueUserIds.includes(userId) ? uniqueUserIds : [...uniqueUserIds, userId]), []); // remove duplicates
64+
65+
const ownerPromises = subscriptionOwnerIds.map(_fetchOwner);
66+
67+
return $q.all(ownerPromises).then(owners => addressbooks.map(addressbook => {
68+
if (addressbook.isSubscription) {
69+
const owner = owners.find(({ id }) => id === addressbook.source.bookId);
70+
71+
addressbook = Object.keys(owner) ? { ...addressbook, owner } : addressbook;
72+
}
73+
74+
return addressbook;
75+
}));
76+
}
77+
78+
function _fetchOwner(id) {
79+
return userAPI.user(id).then(({ data }) => ({ id, displayName: userUtils.displayNameOf(data) }));
80+
}
4981
}
5082
};
5183
}

src/linagora.esn.contact/app/contact/form/contact-edition-form.directive.spec.js

+55-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
'use strict';
22

3-
/* global chai: false */
3+
/* global chai, sinon: false */
44

55
var expect = chai.expect;
66

7-
describe('The contactEditionForm directive', function() {
7+
describe.only('The contactEditionForm directive', function() {
88
var $compile, $rootScope, $scope, session;
99
var CONTACT_AVATAR_SIZE, DEFAULT_ADDRESSBOOK_NAME, CONTACT_COLLECTED_ADDRESSBOOK_NAME;
1010
var contactAddressbookService, esnConfigMock;
11+
let userAPIMock, contactAddressbookDisplayServiceMock;
1112

1213
beforeEach(function() {
14+
contactAddressbookDisplayServiceMock = {
15+
convertShellsToDisplayShells: sinon.spy(function(shells) {
16+
return shells.map(shell => ({
17+
bookName: shell.bookName,
18+
shell: shell
19+
}));
20+
}),
21+
sortAddressbookDisplayShells: shells => shells
22+
};
1323
angular.mock.module('esn.core');
1424
angular.mock.module('linagora.esn.contact');
1525
angular.mock.module(function($provide) {
1626
esnConfigMock = function() {
1727
return $q.when(true);
1828
};
1929

30+
const user = {
31+
data: {
32+
id: 123456,
33+
firstname: 'john',
34+
lastname: 'doe'
35+
}
36+
};
37+
38+
userAPIMock = {
39+
user: sinon.mock().returns(
40+
$q.when(user)
41+
),
42+
currentUser: () => $q.when(user)
43+
};
44+
2045
$provide.value('esnConfig', esnConfigMock);
46+
$provide.value('userAPI', userAPIMock);
47+
$provide.value('contactAddressbookDisplayService', contactAddressbookDisplayServiceMock);
2148
});
2249
});
2350

@@ -46,18 +73,31 @@ describe('The contactEditionForm directive', function() {
4673
urls: []
4774
};
4875
contactAddressbookService.listAddressbooksUserCanCreateContact = function() {
49-
return $q.when([{
50-
bookName: DEFAULT_ADDRESSBOOK_NAME,
51-
href: '/addressbooks/userId/' + DEFAULT_ADDRESSBOOK_NAME + '.json'
52-
}, {
53-
bookName: CONTACT_COLLECTED_ADDRESSBOOK_NAME,
54-
href: '/addressbooks/userId/' + CONTACT_COLLECTED_ADDRESSBOOK_NAME + '.json'
55-
}]);
76+
return $q.when([
77+
{
78+
bookName: DEFAULT_ADDRESSBOOK_NAME,
79+
href: '/addressbooks/userId/' + DEFAULT_ADDRESSBOOK_NAME + '.json'
80+
},
81+
{
82+
bookName: CONTACT_COLLECTED_ADDRESSBOOK_NAME,
83+
href: '/addressbooks/userId/' + CONTACT_COLLECTED_ADDRESSBOOK_NAME + '.json'
84+
},
85+
{
86+
bookName: 'shared',
87+
href: '/addressbooks/123456/123456.json',
88+
group: undefined,
89+
isSubscription: true,
90+
source: {
91+
bookId: 123456
92+
}
93+
}
94+
]);
5695
};
5796
}));
5897

5998
beforeEach(function() {
6099
session.user = { _id: 'userId' };
100+
session.fetchUser = () => $q.when(session.user);
61101
});
62102

63103
function initDirective(scope) {
@@ -73,4 +113,10 @@ describe('The contactEditionForm directive', function() {
73113

74114
expect(element.isolateScope().avatarSize).to.equal(CONTACT_AVATAR_SIZE.bigger);
75115
});
116+
117+
it('should try to load the user using the userId collected from the shared addressbook', () => {
118+
initDirective($scope);
119+
120+
expect(userAPIMock.user).to.have.been.calledWith(123456);
121+
});
76122
});

src/linagora.esn.contact/app/contact/form/contact-edition-form.pug

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ form.contact-form(role="form", name="form", aria-hidden="true", ng-class="{ 'rea
1818
ng-repeat='item in availableAddressbooks',
1919
ng-selected='item.path === addressbookPath',
2020
ng-value='item.path'
21-
) {{ ::item.displayName }}
21+
)
22+
span {{ ::item.displayName }}
23+
span.ellipsis.addressbook-owner(ng-if='item.owner') - {{ ::item.owner.displayName }}
2224
.col-xs-12
2325
.input-group
2426
.display-profile

src/linagora.esn.contact/app/contact/form/contact-edition-modal.less

+4
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,7 @@
198198
}
199199
}
200200
}
201+
202+
.addressbook-owner {
203+
margin-left: 5px;
204+
}

0 commit comments

Comments
 (0)