Skip to content

Commit 7ed48dd

Browse files
committed
Notification about tenant user role changes
1 parent 6d71865 commit 7ed48dd

File tree

6 files changed

+190
-93
lines changed

6 files changed

+190
-93
lines changed

apinf_packages/tenant/client/add/add.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Template.tenantForm.events({
4141
// Get possible users in tenant
4242
if (Session.get('tenantUsers')) {
4343
tenantUsers = Session.get('tenantUsers');
44+
console.log('tenant users=', tenantUsers);
4445
// convert user objects to a list for POST operation
4546
tenant.users = tenantUsers.map((userdata) => {
4647
const usersRow = {
@@ -53,10 +54,13 @@ Template.tenantForm.events({
5354
});
5455
// gather list of notified users email addresses
5556
notifyUserList = tenantUsers.filter((userdata) => {
56-
if (userdata.notification === 'checked') {
57-
return userdata.email;
58-
}
59-
return false;
57+
// if (userdata.notification === 'checked') {
58+
return {
59+
username: userdata.name,
60+
email: userdata.email,
61+
}
62+
// }
63+
// return false;
6064
});
6165
}
6266

@@ -90,14 +94,21 @@ Template.tenantForm.events({
9094
// Close modal
9195
Modal.hide('tenantForm');
9296

97+
// Notification to users of tenant
98+
console.log('Perhaps following users need to be notified=', notifyUserList);
99+
Meteor.call('informTenantUser', notifyUserList, 'userRoleChange', tenant.name, (error, result) => {
100+
if (error) {
101+
sAlert.error('Error in notifying users', { timeout: 'none' });
102+
}
103+
});
104+
93105
// Get success message translation
94106
let message = TAPi18n.__('tenantForm_addTenant_Success_Message');
95107
message = message.concat(tenant.name);
96108

97-
// Alert user of success
109+
// Inform user about success
98110
sAlert.success(message);
99111

100-
console.log('Perhaps following users need to be notified=', notifyUserList);
101112
} else {
102113
// Operation finished, inform spinner
103114
Session.set('tenantUpdateOngoing', false);
@@ -194,6 +205,8 @@ Template.tenantForm.events({
194205
*/
195206

196207
const usersNeedChecking = [];
208+
const notifyChangedUsers = [];
209+
const notifyRemovedUsers = [];
197210

198211
// Go through tenant's original user list and compare it against tenant's modified user list
199212
// Note! Must loop array from right to left in order to get user indexes in descending order,
@@ -233,6 +246,9 @@ Template.tenantForm.events({
233246
// Add user to to-be-checked list
234247
usersNeedChecking.push(checkUser);
235248

249+
// Add user to list for notification about removal
250+
notifyRemovedUsers.push(origUser);
251+
236252
// If user data is modified, set user to be replaced
237253
} else if (origUser.consumer !== sameUserInModified[0].consumer ||
238254
origUser.provider !== sameUserInModified[0].provider) {
@@ -270,6 +286,9 @@ Template.tenantForm.events({
270286
// Add user to to-be-checked list
271287
usersNeedChecking.push(checkUser);
272288

289+
// Add user to notification about modification list
290+
notifyChangedUsers.push(sameUserInModified[0]);
291+
273292
// User data is changed, remove from modified list
274293
modifiedTenant.users.splice(modifiedUserIndex, 1);
275294
} else {
@@ -288,6 +307,9 @@ Template.tenantForm.events({
288307

289308
// If there are any modified users left, they are to be added into request
290309
const newUsers = modifiedTenant.users.map((user) => {
310+
// Add user to notification about modification list
311+
notifyChangedUsers.push(user);
312+
291313
// collect roles
292314
const tenantRoles = [];
293315
if (user.provider) {
@@ -345,6 +367,26 @@ Template.tenantForm.events({
345367
// Close modal
346368
Modal.hide('tenantForm');
347369

370+
// if there are modified users, send notifications
371+
if (notifyChangedUsers.length > 0) {
372+
console.log('Perhaps following users need to be notified about change=', notifyChangedUsers);
373+
Meteor.call('informTenantUser', notifyChangedUsers, 'userRoleChange', modifiedTenant.name, (notifyModifyError, notifyModifyResult) => {
374+
if (notifyModifyError) {
375+
sAlert.error('Error in notifying users', { timeout: 'none' });
376+
}
377+
});
378+
}
379+
380+
if (notifyRemovedUsers.length > 0) {
381+
// if there are removed users, send notifications
382+
console.log('Perhaps following users need to be notified about removal=', notifyRemovedUsers);
383+
Meteor.call('informTenantUser', notifyRemovedUsers, 'userRemoval', modifiedTenant.name, (notifyRemoveError, notifyRemoveResult) => {
384+
if (notifyRemoveError) {
385+
sAlert.error('Error in notifying users', { timeout: 'none' });
386+
}
387+
});
388+
}
389+
348390
// Get success message translation
349391
let message = TAPi18n.__('tenantForm_update_Success_Message');
350392
// Alert user of success

apinf_packages/tenant/client/add/form/form.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@ Template.tenantUserForm.events({
1717
// Alert user of error
1818
sAlert.error(errorMmessage, { timeout: 'none' });
1919
} else {
20+
// get the selected user's email address from complete user list
21+
const userId = $('#completeUserList option:selected').val();
22+
const completeUserList = Session.get('completeUserList');
23+
const thisUser = completeUserList.filter((userdata) => {
24+
if (userdata.id === userId) {
25+
return {
26+
userdata,
27+
}
28+
}
29+
return false;
30+
});
31+
2032
const newUser = {
2133
id: $('#completeUserList option:selected').val(),
2234
name: $('#completeUserList option:selected').text(),
2335
provider: false,
2436
consumer: false,
37+
email: thisUser[0].email,
2538
};
2639

2740
let tenantUsers = [];
@@ -46,12 +59,14 @@ Template.tenantUserForm.events({
4659
// Add new user object to array
4760
tenantUsers.push(newUser);
4861

62+
console.log('new userlist on tenant=', tenantUsers);
4963
// Save to localStorage to be used while listing users of tenant
5064
Session.set('tenantUsers', tenantUsers);
5165

5266
// unselect username
5367
$('#completeUserList option:selected').prop('selected', false);
54-
$('#addUserToTenant').prop('disabled', true);
68+
// Disable add button, when no user is selected
69+
// $('#addUserToTenant').prop('disabled', true);
5570
}
5671
}
5772
},

apinf_packages/tenant/client/remove_tenant/remove_tenant.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ Template.ensureTenantRemovalForm.events({
3939
Meteor.call('deleteTenant', tenantToRemove, (error, result) => {
4040
if (result) {
4141
if (result.status === 204) {
42-
// New tenant successfully added on manager side, empty local list
42+
// Tenant successfully removed from manager side, empty local list
43+
const notifyUserList = Session.get('tenantUsers');
44+
// Notification to users of tenant
45+
console.log('Perhaps following users need to be notified about removal=', notifyUserList);
46+
Meteor.call('informTenantUser', notifyUserList, 'userRemoval', tenantToRemove.name, (notifyError, notifyResult) => {
47+
if (notifyError) {
48+
sAlert.error('Error in notifying users', { timeout: 'none' });
49+
}
50+
});
51+
4352
tenantList = [];
4453
// Save to sessionStorage to be used while adding users to tenant
4554
Session.set('tenantList', tenantList);

apinf_packages/tenant/server/methods.js

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Meteor.methods({
6363
// eslint-disable-next-line new-cap
6464
check(tenantUrl, Match.Maybe(String));
6565
tenantUrl = tenantUrl.concat('tenant');
66-
console.log(+new Date(), ' 2 send GET tenant request to = ', tenantUrl);
6766

6867
// Get user's tenant access token
6968
const accessToken = getTenantToken();
@@ -78,10 +77,6 @@ Meteor.methods({
7877
},
7978
}
8079
);
81-
// Create a monitoring data
82-
83-
console.log('3 tenant GET a ok, result=', result.content);
84-
8580
// deserialize JSON
8681
const tenantList = JSON.parse(result.content);
8782

@@ -112,7 +107,6 @@ Meteor.methods({
112107

113108
response.status = result.statusCode;
114109
} catch (err) {
115-
console.log('3 tenant b nok, err=\n', err);
116110

117111
// Return error object
118112
let errorMessage = TAPi18n.__('tenantRequest_missingTenantList');
@@ -283,50 +277,6 @@ Meteor.methods({
283277
let errorMessage = TAPi18n.__('tenantRequest_missingUserlist');
284278
errorMessage = errorMessage.concat(err);
285279
throw new Meteor.Error(errorMessage);
286-
/*
287-
response.status = err.response.statusCode;
288-
response.content = err.response.content;
289-
290-
// For mock purposes we fill the list here ourself
291-
response.completeUserList = [
292-
{
293-
id: '123456789',
294-
username: 'Håkan',
295-
},
296-
{
297-
id: '223456789',
298-
username: 'Luis',
299-
},
300-
{
301-
id: '323456789',
302-
username: 'Pär',
303-
},
304-
{
305-
id: '423456789',
306-
username: 'Ivan',
307-
},
308-
{
309-
id: '523456789',
310-
username: 'Hans',
311-
},
312-
{
313-
id: '62345689',
314-
username: 'Pierre',
315-
},
316-
{
317-
id: '723456789',
318-
username: 'Väinämöinen',
319-
},
320-
{
321-
id: '82356789',
322-
username: 'Jack',
323-
},
324-
{
325-
id: '92356789',
326-
username: 'Umberto',
327-
},
328-
];
329-
*/
330280
}
331281
} else {
332282
// Return error object
@@ -399,10 +349,6 @@ Meteor.methods({
399349
console.log('3 POST a ok, result=', result);
400350
console.log('3 a ok, response=', response);
401351
} catch (err) {
402-
console.log(+new Date(), ' 3 POST b err=', err);
403-
// response.status = err.response.statusCode || 500;
404-
// response.content = err.response.content || err.error;
405-
406352
// Return error object
407353
throw new Meteor.Error(err.message);
408354
}
@@ -573,4 +519,41 @@ Meteor.methods({
573519
}
574520
return response;
575521
},
522+
informTenantUser (userlist, notificationType, tenantName) {
523+
// Check the type of notification
524+
let text;
525+
if (notificationType === "userRoleChange") {
526+
text = ` has changes in roles in tenant ${tenantName}.`;
527+
console.log('Notify change');
528+
} else if (notificationType === "userRemoval") {
529+
text = ` is no more a user of tenant ${tenantName}. `;
530+
console.log('Notify user removal');
531+
}
532+
533+
// Send notification for each user in list
534+
if (text) {
535+
// Get settings
536+
const settings = Settings.findOne();
537+
538+
// Check if email settings are configured
539+
if (settings.mail && settings.mail.enabled) {
540+
userlist.forEach( user => {
541+
console.log('postia tulossa=', user);
542+
// send notification
543+
let message = user.name;
544+
message = message.concat(text);
545+
console.log('actual sending to ', user.email);
546+
console.log('message=', message);
547+
548+
// Send the e-mail
549+
Email.send({
550+
to: settings.mail.toEmail,
551+
from: settings.mail.fromEmail,
552+
subject: `Tenant user related changes`,
553+
text,
554+
});
555+
});
556+
}
557+
}
558+
},
576559
});

0 commit comments

Comments
 (0)