Coverage for ingadhoc-odoo-saas-adhoc / saas_provider_upgrade / models / helpdesk_team.py: 71%
17 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-09 18:15 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-09 18:15 +0000
1from odoo import fields, models
2from odoo.models import Model
5class HelpdeskTeam(models.Model):
6 _inherit = "helpdesk.team"
8 upgrade_team = fields.Boolean(
9 string="Upgrade Team?",
10 )
11 technician_member_ids = fields.Many2many(
12 "res.users",
13 "helpdesk_team_technician_rel",
14 "team_id",
15 "user_id",
16 string="Technician Team Members",
17 domain=lambda self: [("group_ids", "in", self.env.ref("saas_provider_upgrade.group_upgrade_advanced_user").id)],
18 help="Technical team that will be automatically assigned to the 'Assigned Technician' field in upgrade tickets.",
19 )
21 def _determine_technician_to_assign(self) -> dict[int, Model]:
22 """
23 Get a dict with the technician (per team) that should be assigned to the newly created upgrade ticket.
24 Uses the technician_member_ids field which is specifically configured for upgrade teams.
26 :returns: A mapping of team identifier with the "to assign" technician (maybe an empty record).
27 """
28 result = dict.fromkeys(self.ids, self.env["res.users"])
29 for team in self:
30 if not team.technician_member_ids: 30 ↛ 34line 30 didn't jump to line 34 because the condition on line 30 was always true
31 continue
33 # Use directly the members of the technical team (they are already filtered by domain)
34 member_ids = team.technician_member_ids.ids
36 # Assign to the one with the least open tickets (balanced)
37 ticket_count_data = self.env["helpdesk.ticket"]._read_group(
38 [
39 ("stage_id.fold", "=", False),
40 ("assigned_technician_id", "in", member_ids),
41 ("team_id", "=", team.id),
42 ("upgrade_team", "=", True),
43 ],
44 ["assigned_technician_id"],
45 ["__count"],
46 )
48 open_ticket_per_tech_map = dict.fromkeys(member_ids, 0)
49 open_ticket_per_tech_map.update((user.id, count) for user, count in ticket_count_data)
50 result[team.id] = self.env["res.users"].browse(
51 min(open_ticket_per_tech_map, key=lambda uid: open_ticket_per_tech_map[uid])
52 )
54 return result