Skip to content

Commit

Permalink
Merge pull request #2037 from RitvikSardana/refactor-agent-team
Browse files Browse the repository at this point in the history
refactor: hd team and hd agent
  • Loading branch information
RitvikSardana authored Nov 13, 2024
2 parents d810e34 + 4647ac6 commit 35a9f01
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 178 deletions.
14 changes: 3 additions & 11 deletions helpdesk/helpdesk/doctype/hd_agent/hd_agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"user",
"agent_name",
"user_image",
"is_active",
"groups"
"is_active"
],
"fields": [
{
Expand All @@ -35,12 +34,6 @@
"in_list_view": 1,
"label": "Is Active"
},
{
"fieldname": "groups",
"fieldtype": "Table",
"label": "Groups",
"options": "HD Team Item"
},
{
"fetch_from": "user.user_image",
"fieldname": "user_image",
Expand All @@ -51,7 +44,7 @@
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-07-24 22:31:23.178626",
"modified": "2024-11-11 17:30:22.859253",
"modified_by": "Administrator",
"module": "Helpdesk",
"name": "HD Agent",
Expand Down Expand Up @@ -95,6 +88,5 @@
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"title_field": "agent_name",
"track_changes": 1
"title_field": "agent_name"
}
150 changes: 0 additions & 150 deletions helpdesk/helpdesk/doctype/hd_agent/hd_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,156 +21,6 @@ def set_user_roles(self):

user.save()

def on_update(self):
if self.has_value_changed("is_active"):
if not self.is_active:
self.remove_from_support_rotations()
else:
self.add_to_support_rotations()

if self.has_value_changed("groups") and self.is_active:
previous = self.get_doc_before_save()
if previous:
for group in previous.groups:
if not next(
(g for g in self.groups if g.team == group.team),
None,
):
self.remove_from_support_rotations(group.team)

self.add_to_support_rotations()

def on_trash(self):
self.remove_from_support_rotations()

def add_to_support_rotations(self, group=None):
"""
Add the hd_agent to the support rotation for the given group or all groups
the hd_agent belongs to if hd_agent already added to the support roatation
for a group, skip
:param str group: Team name, defaults to None.
"""
rule_docs = []
if not group:
# Add the hd_agent to the base support rotation

rule_docs.append(
frappe.get_doc(
"Assignment Rule",
frappe.get_doc("HD Settings").get_base_support_rotation(),
)
)

# Add the hd_agent to the support rotation for each group they belong to
if self.groups:
for group in self.groups:
try:
team_assignment_rule = frappe.get_doc(
"HD Team", group.team
).get_assignment_rule()
rule_docs.append(
frappe.get_doc(
"Assignment Rule",
team_assignment_rule,
)
)
except frappe.DoesNotExistError:
frappe.throw(
frappe._(
"Assignment Rule for HD Team {0} does not exist"
).format(group.team)
)
else:
# check if the group is in self.groups
if next(
(group for group in self.groups if group["group_name"] == group), None
):
rule_docs.append(
frappe.get_doc(
"Assignment Rule",
frappe.get_doc("HD Team", group).get_assignment_rule(),
)
)
else:
frappe.throw(
frappe._(
"Agent {0} does not belong to team {1}".format(
self.agent_name, group
)
)
)

for rule_doc in rule_docs:
skip = False
if rule_doc:
if rule_doc.users and len(rule_doc.users) > 0:
for user in rule_doc.users:
if (
user.user == self.user
): # if the user is already in the rule, skip
skip = True
break
if skip:
continue

user_doc = frappe.get_doc(
{"doctype": "Assignment Rule User", "user": self.user}
)
rule_doc.append("users", user_doc)
rule_doc.disabled = False # enable the rule if it is disabled
rule_doc.save(ignore_permissions=True)

def remove_from_support_rotations(self, group=None):
rule_docs = []

if group:
# remove the hd_agent from the support rotation for the given group
rule_docs.append(
frappe.get_doc(
"Assignment Rule",
frappe.get_doc("HD Team", group).get_assignment_rule(),
)
)

else:
# Remove the hd_agent from the base support rotation
rule_docs.append(
frappe.get_doc(
"Assignment Rule",
frappe.get_doc("HD Settings").get_base_support_rotation(),
)
)

# Remove the hd_agent from the support rotation for each group they belong to
for group in self.groups:
rule_docs.append(
frappe.get_doc(
"Assignment Rule",
frappe.get_doc("HD Team", group.team).get_assignment_rule(),
)
)

for rule_doc in rule_docs:
if rule_doc.users and len(rule_doc.users) > 0:
for user in rule_doc.users:
if user.user == self.user:
if len(rule_doc.users) == 1:
rule_doc.disabled = (
True # disable the rule if there are no users left
)
rule_doc.remove(user)
rule_doc.save()

def in_group(self, group):
"""
Check if agent is in the given group
"""
if self.groups:
return next((g for g in self.groups if g.team == group), False)

return False


@frappe.whitelist()
def create_hd_agent(first_name, last_name, email, signature, team):
Expand Down
106 changes: 98 additions & 8 deletions helpdesk/helpdesk/doctype/hd_team/hd_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# For license information, please see license.txt

import frappe
from frappe import _
from frappe.core.doctype.version.version import get_diff
from frappe.exceptions import DoesNotExistError
from frappe.model.document import Document
from frappe.model.naming import append_number_if_name_exists
Expand All @@ -12,8 +14,20 @@ class HDTeam(Document):
def rename_self(self, new_name: str):
self.rename(new_name)

# nosemgrep: frappe-semgrep-rules.rules.frappe-modifying-but-not-comitting-other-method
def after_insert(self):
self.create_assignment_rule()
assignment_rule_doc = frappe.get_doc("Assignment Rule", self.assignment_rule)

for user in self.users:
_user = user.get("user")
if not _user:
continue
assignment_rule_doc.append("users", {"user": _user})

if assignment_rule_doc.disabled and assignment_rule_doc.users:
assignment_rule_doc.disabled = False
assignment_rule_doc.save()

def after_rename(self, olddn, newdn, merge=False):
# Update the condition for the linked assignment rule
Expand All @@ -22,18 +36,28 @@ def after_rename(self, olddn, newdn, merge=False):
rule_doc.assign_condition = f"status == 'Open' and agent_group == '{newdn}'"
rule_doc.save(ignore_permissions=True)

def on_trash(self):
def on_update(self):
self.update_support_rotations()

def on_trash(self):
# Deletes the assignment rule for this group
rule = self.assignment_rule
if not rule:
return
try:
rule = self.get_assignment_rule()
if rule:
self.assignment_rule = ""
self.save()
frappe.get_doc("Assignment Rule", rule).delete()
frappe.delete_doc(
"Assignment Rule",
rule,
ignore_permissions=True,
force=True,
ignore_on_trash=True,
)
frappe.db.commit()
except DoesNotExistError:
# TODO: Log this error
pass
frappe.log_error(
title="Assignment Rule not found",
message=f"Assignment Rule {rule} not found",
)

def create_assignment_rule(self):
"""Creates the assignment rule for this group"""
Expand Down Expand Up @@ -72,3 +96,69 @@ def get_assignment_rule(self):
self.create_assignment_rule()

return self.assignment_rule

def update_support_rotations(self):
"""
Update the support rotations for the hd_agent
# If agent removed, remove from the support rule of the team
# If agent added add to the support rule of the team and also, while adding remove from base Support Rotation
"""
assg_rule_doc = frappe.get_doc("Assignment Rule", self.assignment_rule)
if not assg_rule_doc:
return

previous_doc = self.get_doc_before_save()
diff = get_diff(previous_doc, self)
if not diff:
return

if not diff.get("removed") and not diff.get("added"):
return

for user in diff.get("removed"):
self.update_assignment_rule_users(user, assg_rule_doc, action="remove")

for user in diff.get("added"):
self.update_assignment_rule_users(user, assg_rule_doc)

def update_assignment_rule_users(self, user, assignment_rule_doc, action="add"):
_user = user[1].get("user")
if not user:
frappe.throw(_("User Not found"))
return

if action == "add":
assignment_rule_doc.append("users", {"user": _user})
if assignment_rule_doc.disabled:
assignment_rule_doc.disabled = False
assignment_rule_doc.save()

# remove the user from the base assignment rule
base_assignment_rule = frappe.get_value(
"HD Settings", "HD Settings", "base_support_rotation"
)
base_assignment_rule = frappe.get_doc(
"Assignment Rule", base_assignment_rule
)
user_id = frappe.get_value(
"Assignment Rule User",
{"user": _user, "parent": base_assignment_rule.name},
)
if user_id:
frappe.delete_doc("Assignment Rule User", user_id)
else:
user_id = frappe.get_value(
"Assignment Rule User",
{"user": _user, "parent": assignment_rule_doc.name},
)
if not user_id:
return
frappe.delete_doc("Assignment Rule User", user_id)

# disable the assignment rule if there are no users
total_users_in_assignment_rule = frappe.db.count(
"Assignment Rule User", {"parent": assignment_rule_doc.name}
)
if total_users_in_assignment_rule == 0:
assignment_rule_doc.disabled = True
assignment_rule_doc.save()
Loading

0 comments on commit 35a9f01

Please sign in to comment.