This commit is contained in:
nasir@endelospay.com
2025-05-21 01:04:20 +05:00
parent 483bc8714d
commit 2f2e1008a9

View File

@@ -1527,6 +1527,526 @@ class AssistantFnc(llm.FunctionContext):
logger.error(f"Error saving form: {str(e)}") logger.error(f"Error saving form: {str(e)}")
return {"status": "error", "message": f"Error saving form: {str(e)}"} return {"status": "error", "message": f"Error saving form: {str(e)}"}
async def convertToFormeoJson(self, formFields):
"""Convert simple form fields JSON to Formeo JSON structure"""
try:
# Parse the input JSON
fields_list = json.loads(formFields)
# Generate unique IDs for the main structure
form_id = str(uuid.uuid4())
stage_id = str(uuid.uuid4())
# Initialize the structure with empty collections
formeo_json = {
"id": form_id,
"stages": {
stage_id: {
"children": [],
"id": stage_id
}
},
"rows": {},
"columns": {},
"fields": {}
}
# Create rows for each field
row_ids = []
for field_data in fields_list:
# Generate IDs for each component
row_id = str(uuid.uuid4())
column_id = str(uuid.uuid4())
field_id = str(uuid.uuid4())
# Add row ID to the stage children and row_ids list
formeo_json["stages"][stage_id]["children"].append(row_id)
row_ids.append(row_id)
# Create the row
formeo_json["rows"][row_id] = {
"config": {
"fieldset": False,
"legend": "",
"inputGroup": False
},
"children": [column_id],
"className": ["formeo-row"],
"id": row_id
}
# Create the column
formeo_json["columns"][column_id] = {
"config": {
"width": "100%"
},
"children": [field_id],
"className": ["formeo-column"],
"id": column_id
}
# Get field type and label
field_type = field_data.get("type", "Text")
field_label = field_data.get("label", "Field")
field_options = field_data.get("options", [])
field_required = field_data.get("required", False)
field_name = field_data.get("name", f"key_{uuid.uuid4().hex[:20]}")
field_readonly = field_data.get("readonly", False)
field_placeholder = field_data.get("placeholder", "")
# Create the appropriate field based on type
if field_type.lower() == "text":
if "patient first name" in field_label.lower():
formeo_json["fields"][field_id] = {
"tag": "input",
"config": {
"label": field_label,
"disabledAttrs": ["type"],
"lockedAttrs": ["className"],
"controlId": "patient-first-name"
},
"meta": {
"group": "common",
"icon": "👤",
"id": "patient-first-name"
},
"attrs": {
"className": "patient-first-name",
"type": "text",
"name": "patient_custom_fname",
"readonly": True,
"required": field_required,
"placeholder": "Enter patient first name"
},
"id": field_id
}
elif "patient last name" in field_label.lower():
formeo_json["fields"][field_id] = {
"tag": "input",
"config": {
"label": field_label,
"disabledAttrs": ["type"],
"lockedAttrs": ["className"],
"controlId": "patient-last-name"
},
"meta": {
"group": "common",
"icon": "👤",
"id": "patient-last-name"
},
"attrs": {
"className": "patient-last-name",
"type": "text",
"name": "patient_custom_lname",
"readonly": True,
"required": field_required,
"placeholder": "Enter patient last name"
},
"id": field_id
}
elif "patient email" in field_label.lower():
formeo_json["fields"][field_id] = {
"tag": "input",
"config": {
"label": field_label,
"disabledAttrs": ["type"],
"lockedAttrs": ["className"],
"controlId": "patient-email"
},
"meta": {
"group": "common",
"icon": "📧",
"id": "patient-email"
},
"attrs": {
"className": "patient-email",
"type": "email",
"name": "patient_custom_email",
"readonly": True,
"required": field_required,
"placeholder": "Enter patient email"
},
"id": field_id
}
elif "patient phone" in field_label.lower():
formeo_json["fields"][field_id] = {
"tag": "input",
"config": {
"label": field_label,
"disabledAttrs": ["type"],
"lockedAttrs": ["className"],
"controlId": "patient-phone"
},
"meta": {
"group": "common",
"icon": "📞",
"id": "patient-phone"
},
"attrs": {
"className": "patient-phone",
"type": "tel",
"name": "patient_extra_data_preferred_phone",
"required": field_required,
"placeholder": "Enter patient phone number"
},
"id": field_id
}
elif "tags" in field_label.lower():
formeo_json["fields"][field_id] = {
"tag": "input",
"config": {
"label": field_label,
"disabledAttrs": ["type"],
"lockedAttrs": ["className"],
"controlId": "tag-input"
},
"meta": {
"group": "common",
"icon": "🏷️",
"id": "tag-input"
},
"attrs": {
"className": "tags-input",
"type": "text",
"name": field_name,
"required": field_required,
"placeholder": "Add tags..."
},
"id": field_id
}
else:
field_attrs = {
"required": field_required,
"type": "text",
"className": "form-control",
"name": field_name
}
if field_readonly:
field_attrs["readonly"] = True
if field_placeholder:
field_attrs["placeholder"] = field_placeholder
formeo_json["fields"][field_id] = {
"tag": "input",
"attrs": field_attrs,
"config": {
"label": field_label,
"controlId": "text-input"
},
"id": field_id
}
elif field_type.lower() == "textarea":
formeo_json["fields"][field_id] = {
"tag": "textarea",
"attrs": {
"required": field_required,
"className": "form-control",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "textarea"
},
"id": field_id
}
elif field_type.lower() == "number":
formeo_json["fields"][field_id] = {
"tag": "input",
"attrs": {
"required": field_required,
"type": "number",
"className": "form-control",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "number"
},
"id": field_id
}
elif field_type.lower() == "date":
if "patient dob" in field_label.lower():
formeo_json["fields"][field_id] = {
"tag": "input",
"config": {
"label": field_label,
"disabledAttrs": ["type"],
"lockedAttrs": ["className"],
"controlId": "patient-dob"
},
"meta": {
"group": "common",
"icon": '📅',
"id": "patient-dob"
},
"attrs": {
"className": "patient-dob",
"type": "date",
"name": "patient_custom_DOB",
"required": field_required,
"placeholder": "Enter patient DOB"
},
"id": field_id
}
else:
formeo_json["fields"][field_id] = {
"tag": "input",
"attrs": {
"required": field_required,
"type": "date",
"className": "form-control",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "date-input"
},
"id": field_id
}
elif field_type.lower() == "select" or field_type.lower() == "dropdown":
if "patient gender" in field_label.lower():
formeo_json["fields"][field_id] = {
"tag": "select",
"config": {
"label": "Patient Gender",
"disabledAttrs": ["type"],
"lockedAttrs": ["className"]
},
"meta": {
"group": "common",
"icon": "🚻",
"id": "patient-gender"
},
"attrs": {
"className": "patient-gender",
"name": "patient_custom_gender_identity",
"required": True
},
"options": [
{"label": "Male", "value": "Male"},
{"label": "Female", "value": "Female"},
{"label": "Other", "value": "other"}
],
"id": field_id
}
else:
select_field = {
"tag": "select",
"attrs": {
"required": field_required,
"className": "form-control",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "select"
},
"options": [{"label": opt, "value": opt} for opt in field_options],
"id": field_id
}
formeo_json["fields"][field_id] = select_field
elif field_type.lower() == "checkbox":
formeo_json["fields"][field_id] = {
"tag": "input",
"attrs": {
"required": field_required,
"type": "checkbox",
"className": "form-check-input",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "checkbox"
},
"id": field_id
}
elif field_type.lower() == "radio":
radio_field = {
"tag": "div",
"attrs": {
"className": "radio-group",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "radio-group"
},
"content": "",
"id": field_id
}
# Add radio options
radio_content = "<div class='radio-options'>"
for opt in field_options:
radio_id = f"radio_{uuid.uuid4().hex[:8]}"
radio_content += f"""
<div class="form-check">
<input class="form-check-input" type="radio" name="{field_label.lower().replace(' ', '_')}" id="{radio_id}" value="{opt}">
<label class="form-check-label" for="{radio_id}">{opt}</label>
</div>
"""
radio_content += "</div>"
radio_field["content"] = radio_content
formeo_json["fields"][field_id] = radio_field
elif field_type.lower() == "signature":
formeo_json["fields"][field_id] = {
"tag": "signature-pad",
"attrs": {
"className": "signature-pad",
"name": f"signature_key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "signature",
"disabledAttrs": ["type"],
"lockedAttrs": ["className"]
},
"meta": {
"group": "common",
"icon": "✍️",
"id": "signature"
},
"children": [
{
"tag": "div",
"attrs": {
"className": "canvas-wrapper"
},
"children": [
{
"tag": "canvas"
},
{
"tag": "input",
"attrs": {
"type": "hidden",
"name": "signature-data",
"className": "signature-hidden-input"
}
}
]
},
{
"tag": "div",
"attrs": {
"className": "signature-pad--footer"
},
"children": [
{
"tag": "div",
"attrs": {
"className": "signature-pad--actions"
},
"children": [
{
"tag": "div",
"attrs": {
"className": "column"
},
"children": [
{
"tag": "button",
"attrs": {
"type": "button",
"className": "button clear",
"data-action": "clear"
},
"content": "Clear"
}
]
}
]
}
]
}
],
"id": field_id
}
elif field_type.lower() == "file" or field_type.lower() == "upload":
formeo_json["fields"][field_id] = {
"tag": "input",
"attrs": {
"required": field_required,
"type": "file",
"className": "form-control",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "file-input"
},
"id": field_id
}
elif field_type.lower() == "drag-drop-file":
formeo_json["fields"][field_id] = {
"tag": "drag-drop-file",
"attrs": {
"className": "drag-drop-file-container",
"required": field_required,
"type": "file",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "drag-drop-file",
"disabledAttrs": ["type"],
"lockedAttrs": ["className"]
},
"meta": {
"group": "common",
"icon": "📂",
"id": "drag-drop-file"
},
"content": """
<div class="drag-drop-container">
<div class="drag-drop-area">
<p>Drag and drop or click to upload ID</p>
<p class="file-types">Accepted: JPEG, PNG, PDF (Max 5MB)</p>
<input type="file" class="file-input" accept=".jpeg,.jpg,.png,.pdf" style="opacity: 0; position: absolute; z-index: -1;" />
<div class="file-list"></div>
</div>
</div>
""",
"id": field_id
}
else: # Default to text input for unknown types
formeo_json["fields"][field_id] = {
"tag": "input",
"attrs": {
"required": field_required,
"type": "text",
"className": "form-control",
"name": f"key_{uuid.uuid4().hex[:20]}"
},
"config": {
"label": field_label,
"controlId": "text-input"
},
"id": field_id
}
# Convert to JSON string
logger.info(f"Form data formeo_json: {formeo_json}")
return formeo_json
except Exception as e:
logger.error(f"Error converting to Formeo JSON: {str(e)}")
return json.dumps({})
@llm.ai_callable() @llm.ai_callable()
async def bookAppointment(self, async def bookAppointment(self,
practitioner_id: Annotated[ practitioner_id: Annotated[