Skip to content

Commit

Permalink
ROCKET.CHAT: NEW TASK! Reset department count
Browse files Browse the repository at this point in the history
  • Loading branch information
dudanogueira committed Jun 11, 2023
1 parent 92fb679 commit ba2644f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions rocket_connect/instance/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions rocket_connect/instance/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ba2644f

Please sign in to comment.