From ba2644feab8d155ae5d7f37bbb28c5b2646b2fc0 Mon Sep 17 00:00:00 2001 From: Duda Nogueira Date: Sun, 11 Jun 2023 11:00:21 -0300 Subject: [PATCH] ROCKET.CHAT: NEW TASK! Reset department count --- rocket_connect/instance/models.py | 26 ++++++++++++++++++++++++++ rocket_connect/instance/tasks.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/rocket_connect/instance/models.py b/rocket_connect/instance/models.py index e3f7ba5..adf83af 100644 --- a/rocket_connect/instance/models.py +++ b/rocket_connect/instance/models.py @@ -487,6 +487,32 @@ def install_server_tasks(self): ) self.tasks.add(task) added_tasks.append(task) + + # + # T8 reset department count + # + task = PeriodicTask.objects.filter( + task="instance.tasks.reset_department_count", + kwargs__contains=self.external_token, + ) + if not task.exists(): + crontab = CrontabSchedule.objects.first() + task = PeriodicTask.objects.create( + enabled=False, + name=f"Reset Department Count for agents for {self.name} (ID {self.id})", + description="""This task will reset the agent chat count for configured departments""", + crontab=crontab, + task="instance.tasks.reset_department_count", + kwargs=json.dumps( + { + "server_token": self.external_token, + "department_ids": [], + } + ), + ) + self.tasks.add(task) + added_tasks.append(task) + # return added tasks return added_tasks diff --git a/rocket_connect/instance/tasks.py b/rocket_connect/instance/tasks.py index 9d7e530..0496cbd 100644 --- a/rocket_connect/instance/tasks.py +++ b/rocket_connect/instance/tasks.py @@ -337,3 +337,32 @@ def manage_abandoned_chats( ) return output + + +# T8 +@celery_app.task(retry_kwargs={"max_retries": 7, "countdown": 5}) +def reset_department_count(server_token, department_ids=[]): + output = {} + # get server + server = Server.objects.get(external_token=server_token) + # get rocket + rocket = server.get_rocket_client() + # for each department + for department_id in department_ids: + department_info = rocket.call_api_get("livechat/department/" + department_id) + department_payload = department_info.json() + new_agents = [] + for agent in department_payload.get("agents", []): + agent["count"] = 0 + new_agents.append(agent) + department_payload["agents"] = new_agents + # remove the success from the department info payload + del department_payload["success"] + # update department + output[department_id] = rocket.call_api_put( + "livechat/department/" + department_id, **department_payload + ).json() + + # clear all selected deparments + + return output