Skip to content

Commit

Permalink
fix: filter contact with multiple customer (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssiyad authored Oct 20, 2023
1 parent f24177a commit de10d81
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions helpdesk/helpdesk/doctype/hd_ticket/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ def get_one(name):

def get_customer_criteria():
QBTicket = frappe.qb.DocType("HD Ticket")

user = frappe.session.user
customer = get_customer(user)
conditions = [
QBTicket.contact == user,
QBTicket.customer == customer,
QBTicket.raised_by == user,
]
customer = get_customer(user)
for c in customer:
conditions.append(QBTicket.customer == c)
return Criterion.any(conditions)


Expand Down
11 changes: 7 additions & 4 deletions helpdesk/helpdesk/doctype/hd_ticket/hd_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ def get_list_filters(query: Query):
QBTeamMember = frappe.qb.DocType("HD Team Member")
QBTicket = frappe.qb.DocType("HD Ticket")
user = frappe.session.user
customer = get_customer(user)
conditions = (
[
QBTicket.contact == user,
QBTicket.customer == customer,
QBTicket.raised_by == user,
]
if not is_agent()
else []
)
customer = get_customer(user)
for c in customer:
conditions.append(QBTicket.customer == c)
query = query.where(Criterion.any(conditions))

enable_restrictions, ignore_restrictions = frappe.get_value(
Expand Down Expand Up @@ -219,7 +220,9 @@ def set_customer(self):
# Skip if `Customer` is already set
if self.customer:
return
self.customer = get_customer(self.contact)
customer = get_customer(self.contact)
if len(customer) > 0:
self.customer = customer[0]

def set_priority(self):
if self.priority:
Expand Down Expand Up @@ -750,6 +753,6 @@ def has_permission(doc, user=None):
doc.contact == user
or doc.raised_by == user
or doc.owner == user
or doc.customer == get_customer(user)
or is_agent(user)
or doc.customer in get_customer(user)
)
29 changes: 14 additions & 15 deletions helpdesk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def capture_event(event: str):
return _capture(event, "helpdesk")


def get_customer(contact: str) -> str | None:
def get_customer(contact: str) -> tuple[str]:
"""
Get `Customer` from `Contact`
Expand All @@ -79,20 +79,19 @@ def get_customer(contact: str) -> str | None:
QBDynamicLink = frappe.qb.DocType("Dynamic Link")
QBContact = frappe.qb.DocType("Contact")
conditions = [QBDynamicLink.parent == contact, QBContact.email_id == contact]
res = (
frappe.qb.from_(QBDynamicLink)
.select(QBDynamicLink.link_name)
.where(QBDynamicLink.parentfield == "links")
.where(QBDynamicLink.parenttype == "Contact")
.join(QBContact)
.on(QBDynamicLink.parent == QBContact.name)
.where(Criterion.any(conditions))
.limit(1)
.run(as_dict=True)
)
if not len(res):
return
return res.pop().link_name
return [
i[0]
for i in (
frappe.qb.from_(QBDynamicLink)
.select(QBDynamicLink.link_name)
.where(QBDynamicLink.parentfield == "links")
.where(QBDynamicLink.parenttype == "Contact")
.join(QBContact)
.on(QBDynamicLink.parent == QBContact.name)
.where(Criterion.any(conditions))
.run()
)
]


def extract_mentions(html):
Expand Down

0 comments on commit de10d81

Please sign in to comment.