Skip to content

Commit

Permalink
Merge pull request #1027 from eciis/refresh-user-institutions
Browse files Browse the repository at this point in the history
Fix institution removal notification process
  • Loading branch information
mayzabeel authored Apr 26, 2018
2 parents ed28454 + df54b16 commit 90dd1a7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 73 deletions.
12 changes: 0 additions & 12 deletions backend/handlers/institution_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from models.institution import Institution
from util.json_patch import JsonPatch
from service_entities import enqueue_task
from send_email_hierarchy.remove_institution_email_sender import RemoveInstitutionEmailSender

from handlers.base_handler import BaseHandler

Expand Down Expand Up @@ -204,17 +203,6 @@ def delete(self, user, institution_key):

enqueue_task('remove-inst', params)

email_params = {
"justification": self.request.get('justification'),
"body": """Lamentamos informar que a instituição %s foi removida
pelo administrador %s """ % (institution.name, user.name),
"subject": "Remoção de instituição",
"inst_key": institution_key
}

email_sender = RemoveInstitutionEmailSender(**email_params)
email_sender.send_email()

notification_entity = {
'key': institution_key,
'institution_name': institution.name,
Expand Down
3 changes: 0 additions & 3 deletions backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ def setup_current_institution(user, request):
try:
institution_header = request.headers['Institution-Authorization']
institution_key = ndb.Key(urlsafe=institution_header)
Utils._assert(not user.is_member(institution_key),
"Invalid Current Institution! User is not an active member.",
NotAuthorizedException)
user.current_institution = institution_key
except KeyError as error:
user.current_institution = None
Expand Down
42 changes: 42 additions & 0 deletions backend/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""Send notifications handler."""
import webapp2
import json
Expand All @@ -15,6 +16,7 @@
from jinja2 import Environment, FileSystemLoader
from permissions import DEFAULT_SUPER_USER_PERMISSIONS
from permissions import DEFAULT_ADMIN_PERMISSIONS
from send_email_hierarchy.remove_institution_email_sender import RemoveInstitutionEmailSender


def should_remove(user, institution_key, current_inst_key):
Expand Down Expand Up @@ -120,6 +122,45 @@ def remove_permissions(remove_hierarchy, institution):
admin.remove_permission(permission, institution_key)


def notify_institution_removal(institution, remove_hierarchy, user):
"""This method has two possibilities of flow depending on
the remove_hierarchy's value.
If it's true, the method send email and notification to all
the hierarchy. Otherwise it send just to the first institution.
Params:
institution -- the current institution in the hierarchy whose admin
will receive an email and a notification.
remove_hierarchy -- string that works as a flag informing if the hierarchy.
has been removed or not.
user -- the user who made the request to remove the institution.
"""
email_params = {
"body": """Lamentamos informar que a instituição %s foi removida
pelo usuário %s """ % (institution.name, user.name),
"subject": "Remoção de instituição",
"inst_key": institution.key.urlsafe()
}
email_sender = RemoveInstitutionEmailSender(**email_params)
email_sender.send_email()

user_has_to_receive_notification = institution.admin != user.key

if user_has_to_receive_notification:
send_message_notification(
institution.admin.urlsafe(),
user.key.urlsafe(),
'DELETED_INSTITUTION',
institution.key.urlsafe()
)

if remove_hierarchy == "true":
for child_key in institution.children_institutions:
child = child_key.get()
if child.state == "inactive":
notify_institution_removal(child, remove_hierarchy, user)


class BaseHandler(webapp2.RequestHandler):
"""Base Handler."""

Expand Down Expand Up @@ -193,6 +234,7 @@ def apply_remove_operation(remove_hierarchy, institution, user):
institution.handle_hierarchy_removal(remove_hierarchy, user)
institution.remove_institution_from_users(remove_hierarchy)
remove_permissions(remove_hierarchy, institution)
notify_institution_removal(institution, remove_hierarchy, user)
apply_remove_operation(remove_hierarchy, institution, user)


Expand Down
8 changes: 3 additions & 5 deletions frontend/notification/notificationDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
icon: "clear",
action: function (properties, notification, event) {
if (notification.status !== 'READ') {
return refreshUser(notification, true);
return refreshUser(notification);
}
}
},
"DELETED_INSTITUTION": {
icon: "clear",
action: function (properties, notification, event) {
if (notification.status !== 'READ') {
return refreshUser(notification, true);
return refreshUser(notification);
}
}
},
Expand Down Expand Up @@ -276,9 +276,7 @@
$state.go('app.user.notifications');
}

function refreshUser(notification, change_institution=false) {
if (change_institution)
notificationCtrl.user.goToDifferentInstitution(notification.entity.key, notification.entity.remove_hierarchy);
function refreshUser(notification) {
UserService.load().then(function success(response) {
notificationCtrl.user.institutions = response.institutions;
notificationCtrl.user.follows = response.follows;
Expand Down
40 changes: 0 additions & 40 deletions frontend/test/specs/userSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,46 +289,6 @@
});
});

describe('goToDifferentInstitution', function () {
it('should change current institution', function () {
var user = new User({
institutions: [inst, other_inst],
current_institution: inst
});

expect(user.current_institution).toBe(inst);

user.goToDifferentInstitution(inst.key);

expect(user.current_institution).toBe(other_inst);
});

it('should not change the current institution', function() {
var user = new User({
institutions: [inst],
current_institution: inst
});

expect(user.current_institution).toBe(inst);

user.goToDifferentInstitution(inst.key);

expect(user.current_institution).toBe(inst);
});

it('should go to a non child institution', function() {
var test_inst = {name: 'TEST'};
var user = new User({
institutions: [inst, other_inst, test_inst],
current_institution: inst
});

user.goToDifferentInstitution(inst.key, "true");

expect(user.current_institution).toBe(test_inst);
});
});

describe('addPermissions', function () {
it('should add the permissions', function () {
var user = new User({permissions: {}});
Expand Down
13 changes: 0 additions & 13 deletions frontend/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,6 @@ User.prototype.updateInstProfile = function updateInstProfile(institution) {
this.institution_profiles[index].institution.photo_url = institution.photo_url;
};

User.prototype.goToDifferentInstitution = function goToDifferentInstitution(previousKey, removeHierarchy) {
var user = this;
_.forEach(this.institutions, function(institution) {
if(institution.key !== previousKey) {
const institutionHasBeenDeleted = removeHierarchy === "true" && institution.parent_institution === previousKey;
if (!(institutionHasBeenDeleted)) {
user.current_institution = institution;
window.localStorage.userInfo = JSON.stringify(this);
}
}
});
};

User.prototype.getProfileColor = function getProfileColor() {
const instKey = this.current_institution.key;
return this.institution_profiles.reduce(
Expand Down

0 comments on commit 90dd1a7

Please sign in to comment.