Skip to content

Commit

Permalink
FWF-3490: [Featue] Form validation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
auslin-aot committed Sep 5, 2024
1 parent 1ef4e69 commit 6a7e2db
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,13 @@ def find_all_active_forms(
return query.items, total_count

@classmethod
def find_forms_by_title(cls, form_title, exclude_id) -> FormProcessMapper:
def find_forms_by_title(cls, form_title) -> FormProcessMapper:
"""Find all form process mapper that matches the provided form title."""
query = cls.query.filter(
and_(
FormProcessMapper.form_name == form_title,
FormProcessMapper.deleted.is_(False),
)
)
if exclude_id is not None:
query = query.filter(FormProcessMapper.id != exclude_id)
query = cls.tenant_authorization(query=query)
return query.all()
17 changes: 12 additions & 5 deletions forms-flow-api/src/formsflow_api/services/import_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,23 @@ def validate_form_exists(self, form_json, tenant_key, validate_path_only=False):
def validate_form_title(self, form_json, mapper):
"""Validate form tile in the form_process_mapper table."""
# Exclude the current mapper from the query
mappers = FormProcessMapper.find_forms_by_title(
form_json.get("title"), exclude_id=mapper.id
)
if mappers:
current_app.logger.info(f"Validation for form title...{form_json.get('title')}")
mappers = FormProcessMapper.find_forms_by_title(form_json.get("title"))
current_app.logger.debug(f"mappers matching the title- {mappers}")
# If no mappers are found for the import form title, it's an invalid case
if not mappers:
raise BusinessException(BusinessErrorCode.FORM_EXISTS)
# Filter out the current mapper (mapper.id) from the results
other_mappers = [m for m in mappers if m.id != mapper.id]
# If there are other mappers (besides the current one), raise an error
if other_mappers:
current_app.logger.debug(f"Other mappers matching the title- {other_mappers}")
raise BusinessException(BusinessErrorCode.FORM_EXISTS)
return True

def validate_edit_form_exists(self, form_json, mapper, tenant_key):
"""Validate form exists on edit import."""
current_app.logger.info(f"Validating form exists...{mapper.form_name}")
current_app.logger.info("Validating form exists in import edit...")
# Validate title in mapper table.
self.validate_form_title(form_json, mapper)
# Validate path exists in formio.
Expand Down

0 comments on commit 6a7e2db

Please sign in to comment.