Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/server/v2/store/db.py (Python)

Code before:
                detail=f"Store listing version {store_listing_version_id} not found",
            )

        status = (
            prisma.enums.SubmissionStatus.APPROVED
            if is_approved
            else prisma.enums.SubmissionStatus.REJECTED
        )

        create_data = prisma.types.StoreListingSubmissionCreateInput(
            StoreListingVersion={"connect": {"id": store_listing_version_id}},
            Status=status,
            reviewComments=comments,
            Reviewer={"connect": {"id": reviewer_id}},
            StoreListing={"connect": {"id": store_listing_version.StoreListing.id}},
            createdAt=datetime.now(),
            updatedAt=datetime.now(),
        )

        update_data = prisma.types.StoreListingSubmissionUpdateInput(
            Status=status,
            reviewComments=comments,
            Reviewer={"connect": {"id": reviewer_id}},
            StoreListing={"connect": {"id": store_listing_version.StoreListing.id}},
            updatedAt=datetime.now(),
        )

        if is_approved:
            await prisma.models.StoreListing.prisma().update(
                where={"id": store_listing_version.StoreListing.id},
                data={"isApproved": True},
            )

        submission = await prisma.models.StoreListingSubmission.prisma().upsert(
            where={"storeListingVersionId": store_listing_version_id},
            data=prisma.types.StoreListingSubmissionUpsertInput(
                create=create_data,
                update=update_data,
            ),
        )

        if not submission:
            raise fastapi.HTTPException(
                status_code=404,
                detail=f"Store listing submission {store_listing_version_id} not found",
            )

        return submission

    except Exception as e:
        logger.error(f"Error reviewing store submission: {str(e)}")

Review comment (refactor):
move this closer to where its used below

you go from status -> upsert -> using status -> using upsert when you can have them happen sequentally

Code after:

        if not store_listing_version or not store_listing_version.StoreListing:
            raise fastapi.HTTPException(
                status_code=404,
                detail=f"Store listing version {store_listing_version_id} not found",
            )

        if is_approved:
            await prisma.models.StoreListing.prisma().update(
                where={"id": store_listing_version.StoreListing.id},
                data={"isApproved": True},
            )

        submission_status = (
            prisma.enums.SubmissionStatus.APPROVED
            if is_approved
            else prisma.enums.SubmissionStatus.REJECTED
        )

        update_data: prisma.types.StoreListingSubmissionUpdateInput = {
            "Status": submission_status,
            "reviewComments": comments,
            "Reviewer": {"connect": {"id": reviewer_id}},
            "StoreListing": {"connect": {"id": store_listing_version.StoreListing.id}},
        }

        create_data: prisma.types.StoreListingSubmissionCreateInput = {
            **update_data,
            "StoreListingVersion": {"connect": {"id": store_listing_version_id}},
        }

        submission = await prisma.models.StoreListingSubmission.prisma().upsert(
            where={"storeListingVersionId": store_listing_version_id},
            data={
                "create": create_data,
                "update": update_data,
            },
        )

        if not submission:
            raise fastapi.HTTPException(  # FIXME: don't return HTTP exceptions here
                status_code=404,
                detail=f"Store listing submission {store_listing_version_id} not found",
            )

        return submission

    except Exception as e:
        logger.error(f"Could not create store submission review: {str(e)}")
        raise backend.server.v2.store.exceptions.DatabaseError(
            "Failed to create store submission review"