Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
9.2 kB
3
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 User
from app.models.field_details import FieldDetails


@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:
            updated_field_details_ids = []
            for field_detail in input:
                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,
                    )
                )
                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()
            )
            updated_field_details_graphql = [
                FieldDetailsType(
                    id=field_detail.id,
                    label=field_detail.label,
                    name=field_detail.name,
                    type=field_detail.type,
                    order=field_detail.order,
                    field_format=field_detail.field_format,
                    validation_id=field_detail.validation_id,
                    options=field_detail.options,
                    rcvd_from_reltio=field_detail.rcvd_from_reltio,
                    is_dynamic=field_detail.is_dynamic,
                    is_active=field_detail.is_active,
                    is_editable=field_detail.is_editable,
                    is_deleted=field_detail.is_deleted,
                    is_visible=field_detail.is_visible,
                    is_downstream=field_detail.is_downstream,
                    is_published=field_detail.is_published,
                    default_option=field_detail.default_option,
                    created_date=field_detail.created_date,
                    created_by=field_detail.created_by,
                    updated_by=field_detail.updated_by,
                )
                for field_detail in updated_field_details
            ]
            return UpdateFieldDetailsMutationPayload(
                updated_field_details=updated_field_details_graphql,
                success=True,
                message="Field details updated successfully",
            )
        except Exception as e:
            info.context.session.rollback()
            return UpdateFieldDetailsMutationPayload(
                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,
                    validation_id=field_detail.validation_id,
                    options=field_detail.options,
                    rcvd_from_reltio=field_detail.rcvd_from_reltio,
                    is_dynamic=field_detail.is_dynamic,
                    is_active=field_detail.is_active,
                    is_editable=field_detail.is_editable,
                    is_deleted=0,
                    is_visible=field_detail.is_visible,
                    is_downstream=field_detail.is_downstream,
                    is_published=field_detail.is_published,
                    default_option=field_detail.default_option,
                    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()
            )
            created_field_details_graphql = [
                FieldDetailsType(
                    id=field_detail.id,
                    label=field_detail.label,
                    name=field_detail.name,
                    type=field_detail.type,
                    order=field_detail.order,
                    field_format=field_detail.field_format,
                    validation_id=field_detail.validation_id,
                    options=field_detail.options,
                    rcvd_from_reltio=field_detail.rcvd_from_reltio,
                    is_dynamic=field_detail.is_dynamic,
                    is_active=field_detail.is_active,
                    is_editable=field_detail.is_editable,
                    is_deleted=field_detail.is_deleted,
                    is_visible=field_detail.is_visible,
                    is_downstream=field_detail.is_downstream,
                    is_published=field_detail.is_published,
                    default_option=field_detail.default_option,
                    created_date=field_detail.created_date,
                    created_by=field_detail.created_by,
                    updated_by=field_detail.updated_by,
                )
                for field_detail in created_field_details
            ]
            return CreateFieldDetailsMutationPayload(
                created_field_details=created_field_details_graphql,
                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_detail = []
            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.append(
                    info.context.session.query(FieldDetails).get(field_detail.id)
                )
            info.context.session.commit()

            if total_deleted > 0:
                return DeleteFieldDetailsMutationPayload(
                    deleted_field_details=deleted_field_detail,
                    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