Skip to content

Commit

Permalink
[15.0][ADD] heldespk_mgmt: Helpdesk stages per teams
Browse files Browse the repository at this point in the history
TT45710
  • Loading branch information
carolinafernandez-tecnativa committed Nov 7, 2023
1 parent 77cdcc6 commit f74955a
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 17 deletions.
5 changes: 0 additions & 5 deletions helpdesk_mgmt/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ def _prepare_submit_ticket_vals(self, **kw):
"partner_id": request.env.user.partner_id.id,
"partner_name": request.env.user.partner_id.name,
"partner_email": request.env.user.partner_id.email,
# Need to set stage_id so that the _track_template() method is called
# and the mail is sent automatically if applicable
"stage_id": request.env["helpdesk.ticket"]
.with_company(company.id)
.default_get(["stage_id"])["stage_id"],
}
if company.helpdesk_mgmt_portal_select_team and kw.get("team"):
team = (
Expand Down
17 changes: 15 additions & 2 deletions helpdesk_mgmt/controllers/myaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ def portal_my_tickets(
searchbar_filters = {
"all": {"label": _("All"), "domain": []},
}
for stage in request.env["helpdesk.ticket.stage"].search([]):
for stage in request.env["helpdesk.ticket.stage"].search(
[
"|",
("team_ids", "=", HelpdeskTicket.team_id.id),
("team_ids", "=", False),
]
):
searchbar_filters[str(stage.id)] = {
"label": stage.name,
"domain": [("stage_id", "=", stage.id)],
Expand Down Expand Up @@ -183,8 +189,15 @@ def portal_my_ticket(self, ticket_id, access_token=None, **kw):

def _ticket_get_page_view_values(self, ticket, access_token, **kwargs):
closed_stages = request.env["helpdesk.ticket.stage"].search(
[("close_from_portal", "=", True)]
[
"&",
"|",
("team_ids", "=", ticket.team_id.id),
("team_ids", "=", False),
("close_from_portal", "=", True),
]
)

values = {
"closed_stages": closed_stages, # used to display close buttons
"page_name": "ticket",
Expand Down
52 changes: 42 additions & 10 deletions helpdesk_mgmt/models/helpdesk_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,38 @@ class HelpdeskTicket(models.Model):
_inherit = ["mail.thread.cc", "mail.activity.mixin", "portal.mixin"]

def _get_default_stage_id(self):
return self.env["helpdesk.ticket.stage"].search([], limit=1).id
if self.team_id:
return self.team_id._determine_stage()[self.team_id.id].id

Check warning on line 15 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L15

Added line #L15 was not covered by tests
else:
return (
self.env["helpdesk.ticket.stage"]
.search([("team_ids", "=", False)], limit=1)
.id
)

@api.depends("team_id")
def _compute_stage_ids(self):
for ticket in self.filtered(lambda ticket: ticket.team_id):
if ticket.team_id:
ticket.stage_id = ticket.team_id._determine_stage()[ticket.team_id.id]
else:
ticket.stage_id = (

Check warning on line 29 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L29

Added line #L29 was not covered by tests
self.env["helpdesk.ticket.stage"]
.search([("team_ids", "=", False)], limit=1)
.id
)

@api.model
def _read_group_stage_ids(self, stages, domain, order):
stage_ids = self.env["helpdesk.ticket.stage"].search([])
return stage_ids
search_domain = [("id", "in", stages.ids)]

Check warning on line 37 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L37

Added line #L37 was not covered by tests
if self.env.context.get("default_team_id"):
search_domain = [

Check warning on line 39 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L39

Added line #L39 was not covered by tests
"|",
"|",
("team_ids", "=", False),
("team_ids", "in", self.env.context["default_team_id"]),
] + search_domain
return stages.search(search_domain, order=order)

Check warning on line 45 in helpdesk_mgmt/models/helpdesk_ticket.py

View check run for this annotation

Codecov / codecov/patch

helpdesk_mgmt/models/helpdesk_ticket.py#L45

Added line #L45 was not covered by tests

number = fields.Char(string="Ticket number", default="/", readonly=True)
name = fields.Char(string="Title", required=True)
Expand All @@ -31,16 +57,22 @@ def _read_group_stage_ids(self, stages, domain, order):
user_ids = fields.Many2many(
comodel_name="res.users", related="team_id.user_ids", string="Users"
)

stage_id = fields.Many2one(
comodel_name="helpdesk.ticket.stage",
string="Stage",
group_expand="_read_group_stage_ids",
compute="_compute_stage_ids",
default=_get_default_stage_id,
tracking=True,
store=True,
readonly=False,
ondelete="restrict",
index=True,
tracking=True,
group_expand="_read_group_stage_ids",
copy=False,
index=True,
domain="['|',('team_ids', '=', team_id),('team_ids','=',False)]",
)

partner_id = fields.Many2one(comodel_name="res.partner", string="Contact")
partner_name = fields.Char()
partner_email = fields.Char(string="Email")
Expand All @@ -67,10 +99,7 @@ def _read_group_stage_ids(self, stages, domain, order):
comodel_name="helpdesk.ticket.category",
string="Category",
)
team_id = fields.Many2one(
comodel_name="helpdesk.ticket.team",
string="Team",
)
team_id = fields.Many2one(comodel_name="helpdesk.ticket.team", string="Team")
priority = fields.Selection(
selection=[
("0", "Low"),
Expand Down Expand Up @@ -130,6 +159,9 @@ def create(self, vals_list):
vals["number"] = self._prepare_ticket_number(vals)
if vals.get("user_id") and not vals.get("assigned_date"):
vals["assigned_date"] = fields.Datetime.now()
if vals.get("team_id"):
team = self.env["helpdesk.ticket.team"].browse(vals["team_id"])
vals["stage_id"] = team._determine_stage()[team.id].id
return super().create(vals_list)

def copy(self, default=None):
Expand Down
6 changes: 6 additions & 0 deletions helpdesk_mgmt/models/helpdesk_ticket_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class HelpdeskTicketStage(models.Model):
string="Company",
default=lambda self: self.env.company,
)
team_ids = fields.Many2many(
comodel_name="helpdesk.ticket.team",
relation="team_stage_rel",
string="Helpdesk Teams",
help="Specific team that uses this stage. If it is empty all teams could uses",
)

@api.onchange("closed")
def _onchange_closed(self):
Expand Down
10 changes: 10 additions & 0 deletions helpdesk_mgmt/models/helpdesk_ticket_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class HelpdeskTeam(models.Model):
help="Allow to select this team when creating a new ticket in the portal.",
)

def _determine_stage(self):
result = dict.fromkeys(self.ids, self.env["helpdesk.ticket.stage"])
for team in self:
result[team.id] = self.env["helpdesk.ticket.stage"].search(
["|", ("team_ids", "=", False), ("team_ids", "in", team.id)],
order="sequence",
limit=1,
)
return result

@api.depends("ticket_ids", "ticket_ids.stage_id")
def _compute_todo_tickets(self):
ticket_model = self.env["helpdesk.ticket"]
Expand Down
1 change: 1 addition & 0 deletions helpdesk_mgmt/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

* Pedro M. Baeza
* Víctor Martínez
* Carolina Fernandez

* `ID42 Sistemas <https://www.id42.com.br>`_:

Expand Down
1 change: 1 addition & 0 deletions helpdesk_mgmt/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ <h2><a class="toc-backref" href="#toc-entry-13">Contributors</a></h2>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>Pedro M. Baeza</li>
<li>Víctor Martínez</li>
<li>Carolina Fernandez</li>
</ul>
</li>
<li><a class="reference external" href="https://www.id42.com.br">ID42 Sistemas</a>:<ul>
Expand Down
1 change: 1 addition & 0 deletions helpdesk_mgmt/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def setUpClass(cls):
cls.team_b = helpdesk_ticket_team.create(
{"name": "Team B", "user_ids": [(6, 0, [cls.user_team.id])]}
)
cls.new_stage = cls.env.ref("helpdesk_mgmt.helpdesk_ticket_stage_new")
cls.ticket_a_unassigned = cls._create_ticket(cls, cls.team_a)
cls.ticket_a_unassigned.priority = "3"
cls.ticket_a_user_own = cls._create_ticket(cls, cls.team_a, cls.user_own)
Expand Down
30 changes: 30 additions & 0 deletions helpdesk_mgmt/tests/test_helpdesk_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,33 @@ def test_helpdesk_ticket_message_new(self):
self.assertEqual(
t.name, title, "The ticket should have the correct (new) title."
)

def test_ticket_with_team_stage(self):
self.new_stage.team_ids = [(6, 0, [self.team_a.id, self.team_b.id])]
in_progress_stage = self.env.ref(
"helpdesk_mgmt.helpdesk_ticket_stage_in_progress"
)
in_progress_stage.team_ids = [(6, 0, [self.team_b.id])]
new_ticket = self.env["helpdesk.ticket"].create(
{
"name": "New Ticket",
"description": "Description",
"team_id": self.team_a.id,
"user_id": self.user.id,
"priority": "1",
}
)
self.assertEqual(
new_ticket.stage_id, self.team_a._determine_stage()[self.team_a.id]
)
self.assertEqual(new_ticket.stage_id, self.new_stage)
self.new_stage.team_ids = [(6, 0, [self.team_a.id])]
new_ticket.team_id = self.team_b
self.assertEqual(new_ticket.stage_id, in_progress_stage)

def test_ticket_without_team_stage(self):
self.new_stage.team_ids = False
self.assertEqual(
self.ticket.stage_id, self.team_a._determine_stage()[self.team_a.id]
)
self.assertEqual(self.ticket.stage_id, self.new_stage)
3 changes: 3 additions & 0 deletions helpdesk_mgmt/views/helpdesk_ticket_stage_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/>
<separator />
<field name="name" filter_domain="[('name', 'ilike', self)]" />
<field name="team_ids" />
<field name="company_id" groups="base.group_multi_company" />
<group>
<filter
Expand Down Expand Up @@ -49,6 +50,7 @@
<group name="main_left">
<field name="sequence" />
<field name="mail_template_id" />
<field name="team_ids" widget="many2many_tags" />
<field
name="company_id"
groups="base.group_multi_company"
Expand Down Expand Up @@ -77,6 +79,7 @@
<field name="sequence" widget="handle" />
<field name="name" />
<field name="mail_template_id" />
<field name="team_ids" widget="many2many_tags" />
<field
name="company_id"
optional="hide"
Expand Down

0 comments on commit f74955a

Please sign in to comment.