Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (Consultation): #2725 Planning Notifications #594

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 71 additions & 17 deletions coral/functions/notify_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,82 @@
}
class NotifyPlanning(BaseFunction):

def post_save(self, tile, request, context):
def save(self, tile, request, context):
from arches_orm.models import Person

if context and context.get('escape_function', False):
return

resource_instance_id = str(tile.resourceinstance.resourceinstanceid)
nodegroup_id = str(tile.nodegroup_id)
existing_notification = models.Notification.objects.filter(
context__resource_instance_id=resource_instance_id
context__resource_instance_id=resource_instance_id,
context__group=None
).first()

both_notification = models.Notification.objects.filter(
context__resource_instance_id=resource_instance_id,
context__group='both'
).first()

admin_notification = models.Notification.objects.filter(
context__group='admin'
).first()

resource = Resource.objects.get(pk=resource_instance_id)
name = resource.displayname()

# The existing notification stops groups from being sent multiple of the same notification
# Use this notification as the primary notification
# The other 2 notifications are used to allow multiple groups with different slugs to be notified

if not existing_notification:
notification = models.Notification(
message=f"{name} has been assigned to you",
context={
"resource_instance_id": resource_instance_id,
"consultation_id": name,
"last_notified": None,
"group": None,
"response_slug": 'assign-consultation-workflow'
},
)

else:
notification = existing_notification
notification.message = f"{name} has been assigned to you"

if not admin_notification:
admin_notification = models.Notification(
message=f"{name} has been updated",
context={
"resource_instance_id": resource_instance_id,
"consultation_id": name,
"last_notified": None,
"group": 'admin',
"response_slug": 'assign-consultation-workflow'
},
)
else:
admin_notification.message = f"{name} has been updated"

if not both_notification:
both_notification = models.Notification(
message=f"{name} has been assigned to you",
context={
"resource_instance_id": resource_instance_id,
"consultation_id": name,
"last_notified": None,
"group": 'both',
"response_slug": 'assign-consultation-workflow'
},
)
else:
both_notification.message = f"{name} has been assigned to you"

notification.created = timezone.now()
admin_notification.created = timezone.now()
both_notification.created = timezone.now()

data = tile.data

Expand All @@ -103,7 +148,7 @@ def post_save(self, tile, request, context):
response_group = "HB"
notification.message = f"{name} response has been completed by {response_group}"
response_slug = 'assign-consultation-workflow'
self.notify_group(PLANNING_ADMIN, notification, response_slug)
self.notify_group(PLANNING_ADMIN, PLANNING_ADMIN, notification, response_slug)
return

# fetch re-assigned to data from a seperate nodegroup
Expand All @@ -124,24 +169,33 @@ def post_save(self, tile, request, context):

action_type_conditions = [STATUS_OPEN, EXTENSION_REQUESTED]

# Need to create a new notification per group to keep the slugs unique for each user
if ACTION_STATUS in data and ACTION_TYPE in data:
if data[ACTION_TYPE] is None and data[ACTION_STATUS] in action_type_conditions and not is_assigned_to_a_user:
if existing_notification and existing_notification.context["last_notified"] == PLANNING_ADMIN:
if existing_notification and PLANNING_ADMIN == existing_notification.context["last_notified"]:
return
self.notify_group(PLANNING_ADMIN, notification, 'assign-consultation-workflow')
self.notify_group(PLANNING_ADMIN, PLANNING_ADMIN, notification, 'assign-consultation-workflow')

elif data[ACTION_TYPE] in [ASSIGN_HM, ASSIGN_BOTH] and data[ACTION_STATUS] in action_type_conditions and not is_assigned_to_a_user:
if existing_notification and existing_notification.context["last_notified"] == HM_MANAGERS:
if data[ACTION_TYPE] and data[ACTION_TYPE] == ASSIGN_HM and data[ACTION_STATUS] in action_type_conditions and not is_assigned_to_a_user:
if existing_notification and HM_MANAGERS == existing_notification.context["last_notified"]:
return
self.notify_group(HM_MANAGERS, notification, 'hm-planning-consultation-response-workflow')
self.notify_group(HM_MANAGERS, HM_MANAGERS, notification, 'hm-planning-consultation-response-workflow')

elif data[ACTION_TYPE] in [ASSIGN_HB, ASSIGN_BOTH] and data[ACTION_STATUS] in action_type_conditions and not is_assigned_to_a_user:
if existing_notification and existing_notification.context["last_notified"] == HB_MANAGERS:
if data[ACTION_TYPE] and data[ACTION_TYPE] == ASSIGN_HB and data[ACTION_STATUS] in action_type_conditions and not is_assigned_to_a_user:
if existing_notification and HB_MANAGERS == existing_notification.context["last_notified"]:
return

self.notify_group(HB_MANAGERS, notification, 'hb-planning-consultation-response-workflow')
self.notify_group(HB_MANAGERS, HB_MANAGERS, notification, 'hb-planning-consultation-response-workflow')

if data[ACTION_TYPE] and data[ACTION_TYPE] == ASSIGN_BOTH and data[ACTION_STATUS] in action_type_conditions and not is_assigned_to_a_user:
if existing_notification and 'all' == existing_notification.context["last_notified"]:
return

self.notify_group(HB_MANAGERS, 'all', notification, 'hb-planning-consultation-response-workflow')
self.notify_group(HM_MANAGERS, 'all', both_notification, 'hm-planning-consultation-response-workflow')
self.notify_group(PLANNING_ADMIN, 'all', admin_notification, 'assign-consultation-workflow')

elif data[ACTION_STATUS] in action_type_conditions and is_assigned_to_a_user:
if data[ACTION_STATUS] in action_type_conditions and is_assigned_to_a_user:
with admin():
assigned_users_list = []

Expand All @@ -157,16 +211,16 @@ def post_save(self, tile, request, context):

self.notify_users(assigned_users_list, notification)

def notify_group(self, group_id, notification, response_slug):
def notify_group(self, group_id, last_notified, notification, response_slug):
from arches_orm.models import Group, Person

with admin():
group = Group.find(group_id)
persons = [Person.find(member.id) for member in group.members if isinstance(member, Person)]

notification.context["last_notified"] = group_id
notification.context["last_notified"] = last_notified
notification.context["response_slug"] = response_slug
notification.save()

for person in persons:
user = person.user_account

Expand All @@ -190,7 +244,7 @@ def notify_users(self, assigned_users_list, notification):
continue # Skip already notified users

notified_users_list.append(selected_user.id)
notification.context["last_notified"] = notified_users_list
notification.context["last_notified"] = 'users'
notification.context["response_slug"] = f"{user['team']}-planning-consultation-response-workflow"
notification.save()

Expand Down
4 changes: 3 additions & 1 deletion coral/pkg/graphs/resource_models/Consultation.json
Original file line number Diff line number Diff line change
Expand Up @@ -17398,7 +17398,9 @@
{
"config": {
"triggering_nodegroups": [
"a5e15f5c-51a3-11eb-b240-f875a44e0e11"
"a5e15f5c-51a3-11eb-b240-f875a44e0e11",
"af7677ba-cfe2-11ee-8a4e-0242ac180006",
"dc9bfb24-cfd9-11ee-8cc1-0242ac180006"
]
},
"function_id": "e5de46d7-dd01-418b-a71d-f5b27c143de4",
Expand Down
Loading