Skip to content

Commit

Permalink
Merge pull request #65 from weni-ai/fix/integrated-feature-clone-flows
Browse files Browse the repository at this point in the history
Fix/integrated feature clone flows
  • Loading branch information
barbosajackson authored Oct 18, 2024
2 parents a6731fa + eb8780f commit df1aca0
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 101 deletions.
18 changes: 15 additions & 3 deletions retail/api/features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@ def get_feature_uuid(self, obj):

def get_globals(self, obj):
last_version = obj.last_version
return last_version.globals_values if last_version else None
globals_values = last_version.globals_values if last_version else []
for function in obj.functions.all():
function_last_version = function.last_version
for function_global in function_last_version.globals_values:
if function_global not in globals_values:
globals_values.append(function_global)
return globals_values

def get_sectors(self, obj):
last_version = obj.last_version
sectors = []
if last_version and last_version.sectors:
return [
sectors = [
sector.get("name")
for sector in last_version.sectors
if sector.get("name")
]
return []
for function in obj.functions.all():
function_last_version = function.last_version
for sector in function_last_version.sectors:
if sector.get("name") not in sectors:
sectors.append(sector.get("name"))
return sectors

def get_initial_flow(self, obj):
last_version = obj.last_version
Expand Down
12 changes: 10 additions & 2 deletions retail/api/features/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from rest_framework import views, status
from django.conf import settings

from rest_framework import status
from rest_framework.response import Response

from retail.api.base_service_view import BaseServiceView
Expand All @@ -19,7 +21,13 @@ def get(self, request, project_uuid: str):
features = Feature.objects.exclude(uuid__in=integrated_features)
features = features.exclude(feature_type="FUNCTION")
features = features.exclude(status="development")
features = features.exclude(status="testing")

can_testing = False
for email in settings.EMAILS_CAN_TESTING:
if email in request.user.email:
can_testing = True
if not can_testing:
features = features.exclude(status="testing")
if category:
features = features.filter(category=category)

Expand Down
84 changes: 50 additions & 34 deletions retail/api/integrated_feature/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,70 @@
from retail.features.models import Feature


class IntegratedFeatureSerializer(serializers.Serializer):
class ListIntegratedFeatureSerializer(serializers.Serializer):
name = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
disclaimer = serializers.SerializerMethodField()
documentation_url = serializers.SerializerMethodField()
feature_uuid = serializers.SerializerMethodField()
name = serializers.CharField()
description = serializers.CharField()
disclaimer = serializers.CharField()
documentation_url = serializers.CharField()
globals = serializers.SerializerMethodField()
sectors = serializers.SerializerMethodField()
version = serializers.SerializerMethodField()
versions = serializers.SerializerMethodField()

def get_name(self, obj):
return obj.feature.name

def get_description(self, obj):
return obj.feature.description

def get_disclaimer(self, obj):
return obj.feature.disclaimer

def get_documentation_url(self, obj):
return obj.feature.documentation_url

def get_feature_uuid(self, obj):
return obj.uuid
return obj.feature.uuid

def get_version(self, obj):
return obj.feature_version.version

def get_globals(self, obj):
integrated_features = obj.integrated_features.all()
def get_versions(self, obj):
versions = []
for version in obj.feature.versions.all():
body = {
"version": version.version,
"globals": version.globals_values,
"sectors": []
}
for sector in version.sectors:
body["sectors"].append({"name": sector.get("name", ""), "tags": sector.get("tags")})
versions.append(body)

globals_list = []
for integrated_feature in integrated_features:
if isinstance(integrated_feature.globals_values, dict):
globals_list.extend(
[
{"name": key, "value": value}
for key, value in integrated_feature.globals_values.items()
]
)
return versions

return globals_list

def get_globals(self, obj):
return obj.globals_values

def get_sectors(self, obj):
integrated_features = obj.integrated_features.all()

sectors_list = []
for integrated_feature in integrated_features:
if isinstance(integrated_feature.sectors, list):
for sector in integrated_feature.sectors:
if (
isinstance(sector, dict)
and "name" in sector
and "tags" in sector
):
sectors_list.append(
{"name": sector.get("name"), "tags": sector.get("tags", [])}
)

for sector in obj.sectors:
sectors_list.append({"name": sector.get("name"), "tags": sector.get("tags", [])})
return sectors_list


class FeatureSerializer(serializers.Serializer):
feature_uuid = serializers.SerializerMethodField()
name = serializers.CharField()
description = serializers.CharField()
disclaimer = serializers.CharField()
documentation_url = serializers.CharField()

def get_feature_uuid(self, obj):
return obj.uuid

class Meta:
model = Feature
fields = (
Expand All @@ -57,6 +75,4 @@ class Meta:
"description",
"disclaimer",
"documentation_url",
"globals",
"sectors",
)
109 changes: 74 additions & 35 deletions retail/api/integrated_feature/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rest_framework.response import Response

from retail.api.base_service_view import BaseServiceView
from retail.api.integrated_feature.serializers import IntegratedFeatureSerializer
from retail.api.integrated_feature.serializers import FeatureSerializer, ListIntegratedFeatureSerializer
from retail.api.usecases.populate_globals_values import PopulateGlobalsValuesUsecase

from retail.features.models import Feature, IntegratedFeature
Expand Down Expand Up @@ -53,6 +53,67 @@ def post(self, request, *args, **kwargs):
globals_values_request = {}
for globals_values in feature_version.globals_values:
globals_values_request[globals_values] = ""

actions = []
definition_data = integrated_feature.definition
for function in feature.functions.all():
function_last_version = function.last_version

for globals_values in function_last_version.globals_values:
globals_values_request[globals_values] = ""

for sector in function_last_version.sectors:
can = True
for sec in integrated_feature.sectors:
if sec.get("name") == sector.get("name"):
can = False
if can:
for r_sector in request.data.get("sectors", []):
if r_sector.get("name") == sector.get("name"):
new_sector = {
"name": r_sector.get("name"),
"tags": r_sector.get("tags"),
"queues": sector.get("queues"),
}
integrated_feature.sectors.append(new_sector)
break

if function_last_version.action_base_flow_uuid is not None:
actions.append(
{
"name": function_last_version.action_name,
"prompt": function_last_version.action_prompt,
"root_flow_uuid": str(
function_last_version.action_base_flow_uuid
),
"type": "",
}
)

for flow in function_last_version.definition["flows"]:
definition_data["flows"].append(flow)

for campaign in function_last_version.definition["campaigns"]:
definition_data["campaigns"].append(campaign)

for trigger in function_last_version.definition["triggers"]:
definition_data["triggers"].append(trigger)

for field in function_last_version.definition["fields"]:
definition_data["fields"].append(field)

for group in function_last_version.definition["groups"]:
definition_data["groups"].append(group)

if feature_version.action_base_flow_uuid:
actions.append(
{
"name": feature_version.action_name,
"prompt": feature_version.action_prompt,
"root_flow_uuid": str(feature_version.action_base_flow_uuid),
"type": "",
}
)

for key, value in request.data.get("globals_values", {}).items():
globals_values_request[key] = value
Expand All @@ -78,32 +139,12 @@ def post(self, request, *args, **kwargs):
}
)

actions = []
for function in feature.functions.all():
function_last_version = function.last_version
if function_last_version.action_base_flow_uuid is not None:
actions.append(
{
"name": function_last_version.action_name,
"prompt": function_last_version.action_prompt,
"root_flow_uuid": str(
function_last_version.action_base_flow_uuid
),
"type": "",
}
)
if feature_version.action_base_flow_uuid:
actions.append(
{
"name": feature_version.action_name,
"prompt": feature_version.action_prompt,
"root_flow_uuid": str(feature_version.action_base_flow_uuid),
"type": "",
}
)

integrated_feature.save()
globals_data = []
for key, value in integrated_feature.globals_values.items():
globals_data.append({"name": key, "value": value})
body = {
"definition": integrated_feature.feature_version.definition,
"definition": definition_data,
"user_email": integrated_feature.user.email,
"project_uuid": str(integrated_feature.project.uuid),
"parameters": integrated_feature.globals_values,
Expand All @@ -116,17 +157,18 @@ def post(self, request, *args, **kwargs):
IntegratedFeatureEDA().publisher(body=body, exchange="integrated-feature.topic")
print(f"message sent `integrated feature` - body: {body}")

serializer = IntegratedFeatureSerializer(integrated_feature.feature)
serializer = FeatureSerializer(integrated_feature.feature)

response = {
"status": 200,
"data": {
"feature": integrated_feature.feature.uuid,
**serializer.data,
"feature_version": integrated_feature.feature_version.uuid,
"project": integrated_feature.project.uuid,
"user": integrated_feature.user.email,
"integrated_on": integrated_feature.integrated_on,
**serializer.data,
"globals": globals_data,
"sectors": integrated_feature.sectors,
},
}
return Response(response)
Expand All @@ -135,16 +177,13 @@ def get(self, request, project_uuid):
try:

category = request.query_params.get("category", None)
integrated_features = IntegratedFeature.objects.filter(
project__uuid=project_uuid
).values_list("feature__uuid", flat=True)

features = Feature.objects.filter(uuid__in=integrated_features)
integrated_features = IntegratedFeature.objects.filter(project__uuid=project_uuid)

if category:
features = features.filter(category=category)
integrated_features = integrated_features.filter(feature__category=category)

serializer = IntegratedFeatureSerializer(features, many=True)
serializer = ListIntegratedFeatureSerializer(integrated_features, many=True)

return Response({"results": serializer.data}, status=status.HTTP_200_OK)

Expand Down
25 changes: 0 additions & 25 deletions retail/features/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,6 @@ def save(self, commit: bool) -> FeatureVersion:
self.instance.globals_values.append(match)
self.instance.save()

if feature.feature_type == "FEATURE":
for feature_function in feature.functions.all():
function_version = feature_function.versions.order_by(
"created_on"
).last()
for flow in function_version.definition["flows"]:
self.instance.definition["flows"].append(flow)

for campaign in function_version.definition["campaigns"]:
self.instance.definition["campaigns"].append(campaign)

for trigger in function_version.definition["triggers"]:
self.instance.definition["triggers"].append(trigger)

for field in function_version.definition["fields"]:
self.instance.definition["fields"].append(field)

for group in function_version.definition["groups"]:
self.instance.definition["groups"].append(group)

for globals_values in function_version.globals_values:
if globals_values not in self.instance.globals_values:
self.instance.globals_values.append(globals_values)
self.instance.save()

flows = self.instance.definition.get("flows", [])
sectors = []
for flow in flows:
Expand Down
2 changes: 0 additions & 2 deletions retail/features/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,4 @@ class Meta:

def __init__(self, *args, **kwargs):
feature = kwargs.get("instance", None)
if feature and feature.feature_type == "FUNCTION":
functions = functions.exclude(uuid=feature.uuid)
super().__init__(*args, **kwargs)
2 changes: 2 additions & 0 deletions retail/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,5 @@
INTEGRATIONS_REST_ENDPOINT = env.str("INTEGRATIONS_REST_ENDPOINT")

FLOWS_REST_ENDPOINT = env.str("FLOWS_REST_ENDPOINT")

EMAILS_CAN_TESTING = env.str("EMAILS_CAN_TESTING", "").split(",")

0 comments on commit df1aca0

Please sign in to comment.