Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
3.5 kB
2
Indexable
Never
@type
class FieldDetailsMutation:

    def _update_select_field(self, field_detail, current_user, session):
        """Handle updates for select and select-multiple fields."""
        stmt = (
            update(FieldDetails)
            .where(FieldDetails.id == field_detail.id)
            .values(
                label=field_detail.label,
                name=field_detail.name,
                type=field_detail.type,
                order=field_detail.order,
                options=field_detail.options if field_detail.options else None,
                updated_by=current_user,
            )
        )
        session.execute(stmt)

    def _update_text_field(self, field_detail, current_user, session):
        """Handle updates for text and textarea fields."""
        field_format_value = (
            field_format_mapping.get(field_detail.field_format)
            if field_detail.field_format
            else None
        )
        validation_record = (
            session.query(Validation)
            .filter_by(type=field_detail.validation_value)
            .first()
        )
        if validation_record:
            stmt = (
                update(FieldDetails)
                .where(FieldDetails.id == field_detail.id)
                .values(
                    label=field_detail.label,
                    name=field_detail.name,
                    type=field_detail.type,
                    order=field_detail.order,
                    field_format=field_format_value,
                    validation_id=validation_record.id,
                    updated_by=current_user,
                )
            )
            session.execute(stmt)
            return True
        return False

    @mutation
    def update_field_details(
        self, info: Info, input: List[UpdateFieldDetailsInput]
    ) -> UpdateFieldDetailsMutationPayload:
        # Use the context manager to manage the database session
        try:
            current_user = info.context.current_user.login
            session = info.context.session
            updated_field_details_ids = []

            for field_detail in input:
                if field_detail.type in ["select", "select-multiple"]:
                    self._update_select_field(field_detail, current_user, session)
                elif field_detail.type in ["text", "textarea"]:
                    success = self._update_text_field(field_detail, current_user, session)
                    if not success:
                        return UpdateFieldDetailsMutationPayload(
                            field_details=[],
                            success=False,
                            message=f"Validation type '{field_detail.validation_value}' not found",
                        )
                updated_field_details_ids.append(field_detail.id)

            session.commit()

            updated_field_details = (
                session.query(FieldDetails)
                .filter(FieldDetails.id.in_(updated_field_details_ids))
                .all()
            )

            return UpdateFieldDetailsMutationPayload(
                field_details=updated_field_details,
                success=True,
                message="Field details updated successfully",
            )

        except Exception as e:
            session.rollback()
            return UpdateFieldDetailsMutationPayload(
                field_details=[],
                success=False,
                message=f"Update failed: {str(e)}",
            )
Leave a Comment