Skip to content

Commit

Permalink
update chats and flows after project update
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisoSouza committed Oct 17, 2023
1 parent 06075de commit 30b47cc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
5 changes: 3 additions & 2 deletions connect/api/v1/internal/chats/chats_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import requests

from connect.common.models import Project
from connect.api.v1.internal.internal_authentication import InternalAuthentication
from connect.common.models import ChatsRole


class ChatsRESTClient:
Expand All @@ -15,6 +13,7 @@ def __init__(self):
def update_user_permission(
self, permission: int, user_email: str, project_uuid: str
):
from connect.common.models import ChatsRole
permission_mapper = {
ChatsRole.ADMIN.value: 1,
ChatsRole.AGENT.value: 2,
Expand Down Expand Up @@ -101,6 +100,7 @@ def create_user_permission(
user_email: str,
permission: int
):
from connect.common.models import ChatsRole
permission_mapper = {
ChatsRole.ADMIN.value: 1,
ChatsRole.AGENT.value: 2,
Expand All @@ -120,6 +120,7 @@ def create_user_permission(
return True

def update_chats_project(self, project_uuid):
from connect.common.models import Project
project = Project.objects.get(uuid=project_uuid)
body = dict(
project=str(project.uuid),
Expand Down
3 changes: 3 additions & 0 deletions connect/api/v1/internal/flows/flows_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ def create_globals(self, omie_body: list):
return response

def update_project(self, project_uuid: str, **kwargs: dict):
from connect.common.models import Project
project = Project.objects.get(uuid=project_uuid)
kwargs.update({"timezone": str(project.timezone)})
try:
response = requests.patch(
url=f'{self.base_url}/api/v2/internals/orgs/{project_uuid}/',
Expand Down
15 changes: 9 additions & 6 deletions connect/api/v2/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from connect.internals.event_driven.producer.rabbitmq_publisher import RabbitmqPublisher
from connect.template_projects.models import TemplateType


logger = logging.getLogger(__name__)
User = get_user_model()

Expand Down Expand Up @@ -209,7 +210,7 @@ def publish_create_project_message(self, instance):
if authorization.can_contribute:
authorizations.append({"user_email": authorization.user.email, "role": authorization.role})

extra_fields = self.context["request"].data.get("globals")
extra_fields = self.context["request"].data.get("globals")
if extra_fields is None:
extra_fields = self.context["request"].data.get("project", {}).get("globals", {})

Expand Down Expand Up @@ -571,17 +572,19 @@ class Meta:
date_format = serializers.CharField(max_length=1, required=False)

def update(self, instance, validated_data): # pragma: no cover
flow_client = FlowsRESTClient()
data = validated_data

if validated_data.get("timezone"):
data["timezone"] = str(data["timezone"])

try:
flow_client.update_project(
str(instance.uuid),
**data
instance = super().update(instance, validated_data)
name = validated_data.get("name", instance.name)
celery_app.send_task(
"update_project",
args=[instance.uuid, name],
)
return super().update(instance, validated_data)
return instance
except Exception as error:
logger.error(f"Update project: {error}")
raise error
Expand Down
5 changes: 3 additions & 2 deletions connect/api/v2/projects/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def test_update_project(self, flows_update_project, update_chats_project):
project_uuid = str(self.project1.uuid)
data = {
"name": "Test V2 Project (update)",
"timezone": "America/Argentina/Buenos_Aires"
}
path = f"/v2/organizations/{organization_uuid}projects/{project_uuid}"
method = {"patch": "update"}
Expand All @@ -245,8 +246,8 @@ def test_update_project(self, flows_update_project, update_chats_project):
project_uuid=project_uuid,
data=data
)
# Project.objects.get(uuid=content_data.get("uuid"))
# self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals("America/Argentina/Buenos_Aires", content_data.get("timezone"))
self.assertEquals(response.status_code, status.HTTP_200_OK)

@patch("connect.api.v1.internal.intelligence.intelligence_rest_client.IntelligenceRESTClient.get_organization_intelligences")
@patch("connect.api.v1.internal.flows.flows_rest_client.FlowsRESTClient.get_project_flows")
Expand Down
9 changes: 4 additions & 5 deletions connect/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ def update_user_permission_organization(
retry_backoff=True,
)
def update_project(organization_uuid: str, organization_name: str):
if settings.USE_FLOW_REST:
flow_instance = FlowsRESTClient()
else:
flow_instance = utils.get_grpc_types().get("flow")
flow_instance = FlowsRESTClient()
chats = ChatsRESTClient()
chats.update_chats_project(project_uuid=organization_uuid)
flow_instance.update_project(
organization_uuid=organization_uuid,
project_uuid=organization_uuid,
organization_name=organization_name,
)
return True
Expand Down

0 comments on commit 30b47cc

Please sign in to comment.