Skip to content

Commit

Permalink
fix: messages list only for not closed tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Nov 2, 2023
1 parent 6b6a96a commit 4983fa2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
15 changes: 11 additions & 4 deletions uniticket/uni_ticket/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,22 +1330,29 @@ class Meta:
verbose_name_plural = _("Competenza Ticket")

@staticmethod
def get_ticket_per_structure(structure, follow_check=True):
def get_ticket_per_structure(structure, follow_check=True, only_open_tickets=False):
""" """
q_base = Q(office__organizational_structure=structure,
office__is_active=True)
q_follow = Q(follow=True) if follow_check else Q()
q_only_open = Q(ticket__is_closed=False) if only_open_tickets else Q()
ticket_assignments = TicketAssignment.objects.filter(
q_base,
q_follow
q_follow,
q_only_open
).values_list("ticket__code", flat=True)
return set(ticket_assignments)

@staticmethod
def get_ticket_in_office_list(office_list, follow_check=True):
def get_ticket_in_office_list(office_list,
follow_check=True,
only_open_tickets=False):
""" """
q_only_open = Q(ticket__is_closed=False) if only_open_tickets else Q()
ticket_assignments = TicketAssignment.objects.filter(
office__in=office_list, office__is_active=True
q_only_open,
office__in=office_list,
office__is_active=True
).values("ticket__code", "follow")
ticket_set = set()
for assignment in ticket_assignments:
Expand Down
13 changes: 8 additions & 5 deletions uniticket/uni_ticket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,24 @@ def format_slugged_name(field_name, capitalize=True):
return f


def visible_tickets_to_user(user, structure, office_employee):
def visible_tickets_to_user(user,
structure,
office_employee,
only_open_tickets=False):
"""
Returns a list of tickets that are visible to user
"""
model = apps.get_model("uni_ticket", "TicketAssignment")
# default_office = office_employee.filter(office__is_default=True).first()
# if default_office:
if user_is_in_default_office(user, structure):
tickets = model.get_ticket_per_structure(structure)
tickets = model.get_ticket_per_structure(structure=structure,
only_open_tickets=only_open_tickets)
return tickets
offices = []
for oe in office_employee:
if oe.office not in offices:
offices.append(oe.office)
tickets = model.get_ticket_in_office_list(offices)
tickets = model.get_ticket_in_office_list(offices_list=offices,
only_open_tickets=only_open_tickets)
return tickets


Expand Down
11 changes: 8 additions & 3 deletions uniticket/uni_ticket/views/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,25 @@ def ticket_messages(request, structure_slug=None, structure=None, office_employe
by_operator = False
if user_type == "user":
tickets = Ticket.objects.filter(
Q(created_by=request.user) | Q(compiled_by=request.user)
Q(created_by=request.user) | Q(compiled_by=request.user),
is_closed=False,
).values("code")
# if user_type is 'user', retrieve messages leaved by a manager/operator
# (linked to a structure)
by_operator = True
elif user_type == "operator":
# if user is an operator, retrieve his tickets
tickets = visible_tickets_to_user(
user=request.user, structure=structure, office_employee=office_employee
user=request.user,
structure=structure,
office_employee=office_employee,
only_open_tickets=True
)
else:
# if user is a manager, get structure tickets
ta = TicketAssignment
tickets = ta.get_ticket_per_structure(structure=structure)
tickets = ta.get_ticket_per_structure(structure=structure,
only_open_tickets=True)

if by_operator:
not_read = Count(
Expand Down

0 comments on commit 4983fa2

Please sign in to comment.