Untitled
unknown
plain_text
24 days ago
8.0 kB
2
Indexable
Never
from datetime import datetime from typing import List from sqlalchemy import update from strawberry import mutation, type from app.dependencies.get_db import get_db from app.graphql.contetx import context_dependency, get_context from app.graphql.schema import Info from app.graphql.types.field_details_type import ( CreateFieldDetailsInput, DeleteFieldDetailsInput, FieldDetailsType, UpdateFieldDetailsInput, ) from app.models import FieldDetails, User @type class UpdateFieldDetailsMutationPayload: updated_field_details: List[FieldDetailsType] success: bool message: str = "" @type class CreateFieldDetailsMutationPayload: created_field_details: List[FieldDetailsType] success: bool message: str = "" @type class DeleteFieldDetailsMutationPayload: deleted_field_details: List[FieldDetailsType] success: bool message: str = "" @type class FieldDetailsMutation: @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 updated_field_details_ids = [] for field_detail in input: if field_detail.type in ['Text Box', 'Text Area']: 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, )) elif field_detail.type in ['Select', 'Multi-Select']: 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_detail.field_format if field_detail.field_format else None, validation_id=field_detail updated_by=current_user, )) info.context.session.execute(stmt) updated_field_details_ids.append(field_detail.id) info.context.session.commit() updated_field_details = ( info.context.session.query(FieldDetails) .filter(FieldDetails.id.in_(updated_field_details_ids)) .all() ) return UpdateFieldDetailsMutationPayload( updated_field_details=updated_field_details, success=True, message="Field details updated successfully", ) except Exception as e: info.context.session.rollback() return UpdateFieldDetailsMutationPayload( updated_field_details=[], success=False, message=f"Update failed: {str(e)}", ) @mutation def create_field_details( self, info: Info, input: List[CreateFieldDetailsInput] ) -> CreateFieldDetailsMutationPayload: try: current_user = info.context.current_user.login created_field_details_ids = [] for field_detail in input: new_field_detail = FieldDetails( label=field_detail.label, name=field_detail.name, type=field_detail.type, order=field_detail.order, field_format=field_detail.field_format if field_detail.field_format else None, validation_id=field_detail.validation_id if field_detail.validation_id else None, options=field_detail.options if field_detail.options else None, rcvd_from_reltio=field_detail.rcvd_from_reltio if field_detail.rcvd_from_reltio else None, is_dynamic=field_detail.is_dynamic if field_detail.is_dynamic else "Y", is_active=field_detail.is_active if field_detail.is_active else "Y", is_editable=field_detail.is_editable if field_detail.is_editable else "Y", is_deleted=0, is_visible=field_detail.is_visible if field_detail.is_visible else "Y", is_downstream=field_detail.is_downstream if field_detail.is_downstream else "Y", is_published=field_detail.is_published if field_detail.is_published else "Y", default_option=field_detail.default_option if field_detail.default_option else "Y", created_date=datetime.now(), created_by=current_user, updated_by=current_user, ) info.context.session.add(new_field_detail) info.context.session.flush() # Ensures the new field detail gets an ID before commit created_field_details_ids.append(new_field_detail.id) info.context.session.commit() created_field_details = ( info.context.session.query(FieldDetails) .filter(FieldDetails.id.in_(created_field_details_ids)) .all() ) return CreateFieldDetailsMutationPayload( created_field_details=created_field_details, success=True, message="Field details created successfully", ) except Exception as e: info.context.session.rollback() return CreateFieldDetailsMutationPayload( created_field_details=[], success=False, message=f"Creation failed: {str(e)}", ) @mutation def delete_field_details( self, info: Info, input: List[DeleteFieldDetailsInput] ) -> DeleteFieldDetailsMutationPayload: try: deleted_field_details = [] total_deleted = 0 for field_detail in input: stmt = ( update(FieldDetails) .where(FieldDetails.id == field_detail.id) .values(is_deleted=True) ) result = info.context.session.execute(stmt) total_deleted += result.rowcount # Fetch the deleted content object deleted_field_detail = info.context.session.query(FieldDetails).get( field_detail.id ) deleted_field_details.append(deleted_field_detail) info.context.session.commit() if total_deleted > 0: return DeleteFieldDetailsMutationPayload( deleted_field_details=deleted_field_details, success=True, message=f"{total_deleted} field detail(s) deleted successfully", ) else: return DeleteFieldDetailsMutationPayload( deleted_field_details=[], success=False, message="No field detail was deleted", ) except Exception as e: info.context.session.rollback() return DeleteFieldDetailsMutationPayload( deleted_field_details=[], success=False, message=f"Deletion failed: {str(e)}", )
Leave a Comment