From 9359aef6d08a56d0decc8b4cb1268bdc8656f1de Mon Sep 17 00:00:00 2001 From: Federico Ruggi <1081051+ruggi@users.noreply.github.com> Date: Wed, 19 Jun 2024 18:07:17 +0200 Subject: [PATCH] Fix: getCollaborators inflated query (#6003) --- utopia-remix/app/models/project.server.ts | 5 ++++- .../app/models/projectCollaborators.server.ts | 21 +++++++++++++++---- utopia-remix/app/types.ts | 4 +++- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/utopia-remix/app/models/project.server.ts b/utopia-remix/app/models/project.server.ts index 9e7478b9b0ed..bdd5576109ef 100644 --- a/utopia-remix/app/models/project.server.ts +++ b/utopia-remix/app/models/project.server.ts @@ -16,6 +16,7 @@ import { } from '../types' import { ensure } from '../util/api.server' import { Status } from '../util/statusCodes' +import { selectUserDetailsForProjectCollaborator } from './projectCollaborators.server' const selectProjectWithoutContent: Record = { id: true, @@ -206,7 +207,9 @@ export async function listSharedWithMeProjectsAndCollaborators(params: { Project: { select: { ...selectProjectWithoutContent, - ProjectCollaborator: { select: { User: true } }, + ProjectCollaborator: { + select: { User: { select: selectUserDetailsForProjectCollaborator } }, + }, }, }, }, diff --git a/utopia-remix/app/models/projectCollaborators.server.ts b/utopia-remix/app/models/projectCollaborators.server.ts index 2216301367db..467c51b62272 100644 --- a/utopia-remix/app/models/projectCollaborators.server.ts +++ b/utopia-remix/app/models/projectCollaborators.server.ts @@ -1,17 +1,28 @@ -import type { ProjectCollaborator, UserDetails } from 'prisma-client' +import type { UserDetails } from 'prisma-client' import type { UtopiaPrismaClient } from '../db.server' import { prisma } from '../db.server' -import type { CollaboratorsByProject } from '../types' -import { userToCollaborator } from '../types' +import { userToCollaborator, type CollaboratorsByProject } from '../types' import type { GetBatchResult } from 'prisma-client/runtime/library.js' +export const selectUserDetailsForProjectCollaborator: Partial> = + { + user_id: true, + name: true, + picture: true, + } + export async function getCollaborators(params: { ids: string[] userId: string }): Promise { const projects = await prisma.project.findMany({ where: { proj_id: { in: params.ids }, owner_id: params.userId }, - include: { ProjectCollaborator: { include: { User: true } } }, + select: { + proj_id: true, + ProjectCollaborator: { + select: { User: { select: selectUserDetailsForProjectCollaborator } }, + }, + }, }) let collaboratorsByProject: CollaboratorsByProject = {} @@ -19,6 +30,8 @@ export async function getCollaborators(params: { const collaboratorUserDetails = project.ProjectCollaborator.map(({ User }) => User) collaboratorsByProject[project.proj_id] = collaboratorUserDetails.map(userToCollaborator) } + + // TODO this could return a much slimmer payload by having two objects: 1) projectId -> userId[], 2) userId -> userDetails return collaboratorsByProject } diff --git a/utopia-remix/app/types.ts b/utopia-remix/app/types.ts index 909a3bb2adea..7deddff58c84 100644 --- a/utopia-remix/app/types.ts +++ b/utopia-remix/app/types.ts @@ -53,7 +53,9 @@ export interface Collaborator { export type CollaboratorsByProject = { [projectId: string]: Collaborator[] } -export function userToCollaborator(user: UserDetails): Collaborator { +export function userToCollaborator( + user: Pick, +): Collaborator { return { id: user.user_id, name: user.name,