Skip to content

Commit

Permalink
Merge pull request #51 from weni-ai/feature/new-initial-flow
Browse files Browse the repository at this point in the history
adding to feature_version the action_base_flow_<uuid/name> and modify…
  • Loading branch information
barbosajackson authored Oct 4, 2024
2 parents f5e8d04 + 640d4c5 commit 81ac759
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 37 deletions.
33 changes: 24 additions & 9 deletions retail/api/integrated_feature/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ def post(self, request, *args, **kwargs):
"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"]
update_fields=["sectors", "globals_values"]
)

for sector in integrated_feature.sectors:
Expand All @@ -68,6 +65,28 @@ 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 != 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": ""
}
)

body = {
"definition": integrated_feature.feature_version.definition,
"user_email": integrated_feature.user.email,
Expand All @@ -76,11 +95,7 @@ def post(self, request, *args, **kwargs):
"feature_version": str(integrated_feature.feature_version.uuid),
"feature_uuid": str(integrated_feature.feature.uuid),
"sectors": sectors_data,
"action": {
"name": integrated_feature.feature_version.action_name,
"prompt": integrated_feature.feature_version.action_prompt,
"root_flow_uuid": integrated_feature.action_base_flow,
},
"action": actions
}

IntegratedFeatureEDA().publisher(body=body, exchange="integrated-feature.topic")
Expand Down
35 changes: 34 additions & 1 deletion retail/features/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Meta:
"action_types",
"action_name",
"action_prompt",
"action_base_flow_name",
]

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -52,6 +53,20 @@ def clean_definition(self):
)

return definition

def clean_action_base_flow_name(self):
action_base_flow_name = self.cleaned_data.get("action_base_flow_name", None)
if action_base_flow_name is None:
return action_base_flow_name
definition = self.cleaned_data.get("definition")
if definition is None:
raise forms.ValidationError("Você precisa colocar uma definition")
error_message = "você tem de digitar um nome de fluxo existente na sua definition, são eles: "
for flow in definition.get("flows"):
error_message += "\n" + flow.get("name")
if flow.get("name") == action_base_flow_name:
return action_base_flow_name
raise forms.ValidationError(error_message)

def save(self, commit: bool) -> FeatureVersion:
feature_version: FeatureVersion = super().save(commit)
Expand Down Expand Up @@ -99,6 +114,8 @@ def save(self, commit: bool) -> FeatureVersion:
if len(flow["integrations"]["ticketers"]) > 0:
for ticketer in flow["integrations"]["ticketers"]:
sectors.append(ticketer)
if flow.get("name") == self.instance.action_base_flow_name:
self.instance.action_base_flow_uuid = flow.get("uuid")

sectors_base = []
for sector in sectors:
Expand Down Expand Up @@ -149,14 +166,14 @@ class Meta:
"action_types",
"action_name",
"action_prompt",
"action_base_flow_name",
]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def clean_definition(self):
definition = self.cleaned_data.get("definition")

flows = definition.get("flows")
if not flows:
raise forms.ValidationError(
Expand All @@ -182,6 +199,20 @@ def clean_definition(self):
)

return definition

def clean_action_base_flow_name(self):
action_base_flow_name = self.cleaned_data.get("action_base_flow_name", None)
if action_base_flow_name is None:
return action_base_flow_name
definition = self.cleaned_data.get("definition")
if definition is None:
raise forms.ValidationError("Você precisa colocar uma definition")
error_message = "você tem de digitar um nome de fluxo existente na sua definition, são eles: "
for flow in definition.get("flows"):
error_message += "\n" + flow.get("name")
if flow.get("name") == action_base_flow_name:
return action_base_flow_name
raise forms.ValidationError(error_message)

def save(self, commit: bool) -> FeatureVersion:
feature_version: FeatureVersion = super().save(commit)
Expand All @@ -205,6 +236,8 @@ def save(self, commit: bool) -> FeatureVersion:
if len(flow["integrations"]["ticketers"]) > 0:
for ticketer in flow["integrations"]["ticketers"]:
sectors.append(ticketer)
if flow.get("name") == self.instance.action_base_flow_name:
self.instance.action_base_flow_uuid = flow.get("uuid")

sectors_base = []
for sector in sectors:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.1 on 2024-10-04 11:28

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("features", "0012_function_remove_integratedfeature_action_base_flow"),
]

operations = [
migrations.AddField(
model_name="featureversion",
name="action_base_flow_name",
field=models.CharField(blank=True, null=True),
),
migrations.AddField(
model_name="featureversion",
name="action_base_flow_uuid",
field=models.UUIDField(blank=True, null=True),
),
]
2 changes: 2 additions & 0 deletions retail/features/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class FeatureVersion(models.Model):
null=True, blank=True, choices=ACTION_TYPES_CHOICES, default="PERSONALIZADO"
)
action_type_brain = models.TextField(null=True, blank=True)
action_base_flow_name = models.CharField(null=True, blank=True, choices=None)
action_base_flow_uuid = models.UUIDField(null=True, blank=True)

created_on = models.DateTimeField(auto_now_add=True)

Expand Down
24 changes: 0 additions & 24 deletions retail/features/templates/integrate_feature.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@
<form method="post" class="form-horizontal">
{% csrf_token %}
{{ form.as_p }}
<p>Fluxo inicial</p>
<select name="base_flows" id="base-flows">
<option value="">--------</option>
</select>
<br/>
<button type="submit" class="btn btn-primary">{{ button_title }}</button>
</form>

<script>
let featureVersions = {{ versions | safe }};
let featureSectors = {{ versions_sectors | safe }};
let lastVersionParams = {{ last_version_params | safe }};
let lastVersionSectors = {{ version_sectors | safe }};
let lastVersionActions = {{ action_base_flow | safe }}
let featureActions = {{actions | safe}};

function getVersionParamsObject(versionParams) {
if (Object.keys(versionParams).length == 0) {
Expand All @@ -33,39 +24,24 @@
featureVersionElement = document.getElementById('id_feature_version')
parametersElement = document.getElementById('id_globals_values')
sectorsElement = document.getElementById('id_sectors')
actionsElement = document.getElementById('base-flows')

parametersElement.value = getVersionParamsObject(lastVersionParams)
if(Object.keys(lastVersionSectors).length == 0) {
sectorsElement.value = "{}"
} else {
sectorsElement.value = JSON.stringify(lastVersionSectors, null, 2)
}
lastVersionActions.forEach((flow) => {
let optionElement = document.createElement("option")
optionElement.value = flow.flow_uuid
optionElement.textContent = flow.flow_name
actionsElement.appendChild(optionElement)
})
featureVersionElement.addEventListener('input', function() {
if (featureVersionElement.value) {
parametersElement.disabled = false;
let versionParams = featureVersions[featureVersionElement.value]
let sectorParams = featureSectors[featureVersionElement.value]
let flows = featureActions[featureVersionElement.value]
parametersElement.value = getVersionParamsObject(versionParams)
if(Object.keys(sectorParams).length == 0) {
sectorsElement.value = "{}"
} else {
sectorsElement.value = JSON.stringify(sectorParams, null, 2)
}
actionsElement.innerHTML = "<option value=''>--------</option>"
flows.forEach((flow) => {
let optionElement = document.createElement("option")
optionElement.value = flow.flow_uuid
optionElement.textContent = flow.flow_name
actionsElement.appendChild(optionElement)
})
} else {
parametersElement.disabled = true;
parametersElement.value = ""
Expand Down
5 changes: 2 additions & 3 deletions retail/features/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def integrate_feature_view(request, project_uuid, feature_uuid):
integrated_feature = form.save(commit=False)
integrated_feature.project = project
integrated_feature.user = request.user
integrated_feature.action_base_flow = request.POST["base_flows"]
integrated_feature.save()

sectors_data = []
Expand All @@ -50,7 +49,7 @@ def integrate_feature_view(request, project_uuid, feature_uuid):
"action": {
"name": integrated_feature.feature_version.action_name,
"prompt": integrated_feature.feature_version.action_prompt,
"root_flow_uuid": integrated_feature.action_base_flow,
"root_flow_uuid": integrated_feature.feature_version.action_base_flow_uuid,
},
}
IntegratedFeatureEDA().publisher(
Expand Down Expand Up @@ -128,7 +127,7 @@ def update_feature_view(request, project_uuid, integrated_feature_uuid):
"action": {
"name": integrated_feature.feature_version.action_name,
"prompt": integrated_feature.feature_version.action_prompt,
"root_flow_uuid": integrated_feature.action_base_flow,
"root_flow_uuid": integrated_feature.feature_version.action_base_flow_uuid,
},
}
IntegratedFeatureEDA().publisher(
Expand Down

0 comments on commit 81ac759

Please sign in to comment.