Skip to content

Commit

Permalink
Merge pull request #48 from weni-ai/feature/update-integrated-feature
Browse files Browse the repository at this point in the history
 update integrated feature
  • Loading branch information
barbosajackson authored Oct 3, 2024
2 parents e4acf9d + 0552105 commit d689be5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
5 changes: 5 additions & 0 deletions retail/api/features/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class FeaturesSerializer(serializers.Serializer):
feature_uuid = serializers.SerializerMethodField()
name = serializers.CharField()
description = serializers.CharField()
disclaimer = serializers.CharField()
Expand All @@ -12,6 +13,9 @@ class FeaturesSerializer(serializers.Serializer):
sectors = serializers.SerializerMethodField()
initial_flow = serializers.SerializerMethodField()

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

def get_globals(self, obj):
last_version = obj.last_version
return last_version.globals_values if last_version else None
Expand All @@ -38,6 +42,7 @@ def get_initial_flow(self, obj):
class Meta:
model = Feature
fields = (
"uuid",
"name",
"description",
"disclaimer",
Expand Down
5 changes: 5 additions & 0 deletions retail/api/integrated_feature/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


class IntegratedFeatureSerializer(serializers.Serializer):
feature_uuid = serializers.SerializerMethodField()
name = serializers.CharField()
description = serializers.CharField()
disclaimer = serializers.CharField()
Expand All @@ -12,6 +13,9 @@ class IntegratedFeatureSerializer(serializers.Serializer):
sectors = serializers.SerializerMethodField()
initial_flow = serializers.SerializerMethodField()

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

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

Expand Down Expand Up @@ -75,6 +79,7 @@ def get_initial_flow(self, obj):
class Meta:
model = Feature
fields = (
"uuid",
"name",
"description",
"disclaimer",
Expand Down
76 changes: 59 additions & 17 deletions retail/api/integrated_feature/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ def post(self, request, *args, **kwargs):
)
user, _ = User.objects.get_or_create(email=request.user.email)
feature_version = feature.last_version

integrated_feature = IntegratedFeature.objects.create(
project=project,
feature=feature,
feature_version=feature_version,
user=user
project=project, feature=feature, feature_version=feature_version, user=user
)

sectors_data = []
Expand All @@ -45,14 +42,20 @@ def post(self, request, *args, **kwargs):
new_sector = {
"name": r_sector.get("name"),
"tags": r_sector.get("tags"),
"queues": sector.get("queues")
"queues": sector.get("queues"),
}
integrated_feature.sectors.append(new_sector)
break
for globals_key, globals_value in request.data.get("globals_values", {}).items():
for globals_key, globals_value in request.data.get(
"globals_values", {}
).items():
integrated_feature.globals_values[globals_key] = globals_value
integrated_feature.action_base_flow = request.data.get("action_base_flow", "")
integrated_feature.save(update_fields=["sectors", "globals_values", "action_base_flow"])
integrated_feature.action_base_flow = request.data.get(
"action_base_flow", ""
)
integrated_feature.save(
update_fields=["sectors", "globals_values", "action_base_flow"]
)

for sector in integrated_feature.sectors:
sectors_data.append(
Expand Down Expand Up @@ -80,9 +83,7 @@ def post(self, request, *args, **kwargs):
},
}

IntegratedFeatureEDA().publisher(
body=body, exchange="integrated-feature.topic"
)
IntegratedFeatureEDA().publisher(body=body, exchange="integrated-feature.topic")
print(f"message send `integrated feature` - body: {body}")

response = {
Expand All @@ -92,8 +93,8 @@ def post(self, request, *args, **kwargs):
"feature_version": integrated_feature.feature_version.uuid,
"project": integrated_feature.project.uuid,
"user": integrated_feature.user.email,
"integrated_on": integrated_feature.integrated_on
}
"integrated_on": integrated_feature.integrated_on,
},
}
return Response(response)

Expand Down Expand Up @@ -122,9 +123,16 @@ def delete(self, request, *args, **kwargs):
try:
project = Project.objects.get(uuid=request.data["project_uuid"])
except Project.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND, data={"error": f"Project with uuid equals {request.data['project_uuid' ]} does not exists!"})
return Response(
status=status.HTTP_404_NOT_FOUND,
data={
"error": f"Project with uuid equals {request.data['project_uuid' ]} does not exists!"
},
)

integrated_feature = IntegratedFeature.objects.get(project__uuid=str(project.uuid), feature__uuid=str(feature.uuid))
integrated_feature = IntegratedFeature.objects.get(
project__uuid=str(project.uuid), feature__uuid=str(feature.uuid)
)

body = {
"project_uuid": str(project.uuid),
Expand All @@ -136,4 +144,38 @@ def delete(self, request, *args, **kwargs):
IntegratedFeatureEDA().publisher(body=body, exchange="removed-feature.topic")
print(f"message send to `removed-feature.topic`: {body}")
integrated_feature.delete()
return Response({"status": 200, "data":"integrated feature removed"})
return Response({"status": 200, "data": "integrated feature removed"})

def put(self, request, *args, **kwargs):
feature = Feature.objects.get(uuid=kwargs["feature_uuid"])
try:
project = Project.objects.get(uuid=request.data["project_uuid"])
except Project.DoesNotExist:
return Response(
status=status.HTTP_404_NOT_FOUND,
data={
"error": f"Project with uuid equals {request.data['project_uuid' ]} does not exists!"
},
)
integrated_feature = IntegratedFeature.objects.get(
project=project, feature=feature
)
for key, value in request.data.get("globals_values").items():
integrated_feature.globals_values[key] = value
integrated_feature.save()
for sector in request.data.get("sectors", []):
for integrated_sector in integrated_feature.sectors:
if integrated_sector["name"] == sector["name"]:
integrated_sector["tags"] = sector["tags"]
integrated_feature.save()

return Response(
{
"status": 200,
"data": {
"message": "Integrated feature updated",
"globals_values": integrated_feature.globals_values,
"sectors": integrated_feature.sectors,
},
}
)

0 comments on commit d689be5

Please sign in to comment.