From 44c61af32f92b3ea390b7b8c109e1df4961bbe62 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Tue, 9 Apr 2024 18:09:11 -0700 Subject: [PATCH] Addressed CR comments --- .../migration.sql | 1 + .../src/app/users/[username]/UserLayout.tsx | 4 +- .../src/graphql/helpers/modelExportHelpers.ts | 43 ------------------- packages/hub/src/graphql/queries/exports.ts | 36 ++++++++++------ packages/hub/src/graphql/types/Group.ts | 4 +- 5 files changed, 27 insertions(+), 61 deletions(-) delete mode 100644 packages/hub/src/graphql/helpers/modelExportHelpers.ts diff --git a/packages/hub/prisma/migrations/20240405224331_model_export_is_current/migration.sql b/packages/hub/prisma/migrations/20240405224331_model_export_is_current/migration.sql index 2c59e555a9..1994ced6b4 100644 --- a/packages/hub/prisma/migrations/20240405224331_model_export_is_current/migration.sql +++ b/packages/hub/prisma/migrations/20240405224331_model_export_is_current/migration.sql @@ -1,2 +1,3 @@ -- AlterTable ALTER TABLE "ModelExport" ADD COLUMN "isCurrent" BOOLEAN NOT NULL DEFAULT false; +UPDATE "ModelExport" SET "isCurrent" = true WHERE "modelRevisionId" IN (SELECT "currentRevisionId" FROM "Model"); \ No newline at end of file diff --git a/packages/hub/src/app/users/[username]/UserLayout.tsx b/packages/hub/src/app/users/[username]/UserLayout.tsx index 757cfb6662..9f3e0e0664 100644 --- a/packages/hub/src/app/users/[username]/UserLayout.tsx +++ b/packages/hub/src/app/users/[username]/UserLayout.tsx @@ -17,7 +17,7 @@ import { newModelRoute, userDefinitionsRoute, userGroupsRoute, - userModelExportRoute, + userModelExportsRoute, userRoute, } from "@/routes"; @@ -119,7 +119,7 @@ export const UserLayout: FC< {isMe || user.modelExports.edges.length ? ( ) : null} {isMe || user.relativeValuesDefinitions.edges.length ? ( diff --git a/packages/hub/src/graphql/helpers/modelExportHelpers.ts b/packages/hub/src/graphql/helpers/modelExportHelpers.ts deleted file mode 100644 index 7f920b226e..0000000000 --- a/packages/hub/src/graphql/helpers/modelExportHelpers.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Prisma } from "@prisma/client"; -import { Session } from "next-auth"; - -export function modelExportWhereHasAccess( - session: Session | null -): Prisma.ModelExportWhereInput { - const orParts: Prisma.ModelExportWhereInput[] = [ - { - modelRevision: { - model: { - isPrivate: false, - }, - }, - }, - ]; - - if (session) { - orParts.push({ - modelRevision: { - model: { - owner: { - OR: [ - { - user: { email: session.user.email }, - }, - { - group: { - memberships: { - some: { - user: { email: session.user.email }, - }, - }, - }, - }, - ], - }, - }, - }, - }); - } - - return { OR: orParts }; -} diff --git a/packages/hub/src/graphql/queries/exports.ts b/packages/hub/src/graphql/queries/exports.ts index 0aa17e8369..e74f0940ac 100644 --- a/packages/hub/src/graphql/queries/exports.ts +++ b/packages/hub/src/graphql/queries/exports.ts @@ -1,7 +1,9 @@ +import merge from "lodash/merge"; + import { builder } from "@/graphql/builder"; import { prisma } from "@/prisma"; -import { modelExportWhereHasAccess } from "../helpers/modelExportHelpers"; +import { modelWhereHasAccess } from "../helpers/modelHelpers"; import { ModelExport, ModelExportConnection } from "../types/ModelExport"; const ModelExportQueryInput = builder.inputType("ModelExportQueryInput", { @@ -22,22 +24,28 @@ builder.queryField("modelExports", (t) => }, resolve: (query, _, { input }, { session }) => { const modelId = input?.modelId; + + const queries = merge( + {}, + { modelRevision: { model: modelWhereHasAccess(session) } }, + modelId && { + modelRevision: { + modelId: modelId, + }, + }, + input?.owner && { + modelRevision: { model: { owner: { slug: input.owner } } }, + }, + input && + input.variableName && { + variableName: input.variableName, + } + ); + return prisma.modelExport.findMany({ ...query, where: { - ...modelExportWhereHasAccess(session), - ...(modelId && { - modelRevision: { - modelId: modelId, - }, - }), - ...(input && - input.variableName && { - variableName: input.variableName, - }), - ...(input?.owner && { - modelRevision: { model: { owner: { slug: input.owner } } }, - }), + ...queries, isCurrent: true, }, orderBy: { diff --git a/packages/hub/src/graphql/types/Group.ts b/packages/hub/src/graphql/types/Group.ts index afb4043850..c287e6f4cb 100644 --- a/packages/hub/src/graphql/types/Group.ts +++ b/packages/hub/src/graphql/types/Group.ts @@ -155,9 +155,9 @@ export const Group = builder.prismaNode("Group", { }, }, }), - resolve: (user, args, ctx) => { + resolve: (group, args, ctx) => { const exports = - user.asOwner?.models + group.asOwner?.models .map((model) => model.currentRevision?.exports ?? []) .flat() ?? []; return modelExportConnectionHelpers.resolve(exports, args, ctx);