Skip to content

Commit

Permalink
Fix: getCollaborators inflated query (#6003)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruggi authored Jun 19, 2024
1 parent 66d3479 commit 9359aef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 4 additions & 1 deletion utopia-remix/app/models/project.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof ProjectWithoutContentFromDB, true> = {
id: true,
Expand Down Expand Up @@ -206,7 +207,9 @@ export async function listSharedWithMeProjectsAndCollaborators(params: {
Project: {
select: {
...selectProjectWithoutContent,
ProjectCollaborator: { select: { User: true } },
ProjectCollaborator: {
select: { User: { select: selectUserDetailsForProjectCollaborator } },
},
},
},
},
Expand Down
21 changes: 17 additions & 4 deletions utopia-remix/app/models/projectCollaborators.server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
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<Record<keyof UserDetails, boolean>> =
{
user_id: true,
name: true,
picture: true,
}

export async function getCollaborators(params: {
ids: string[]
userId: string
}): Promise<CollaboratorsByProject> {
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 = {}
for (const project of projects) {
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
}

Expand Down
4 changes: 3 additions & 1 deletion utopia-remix/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export interface Collaborator {

export type CollaboratorsByProject = { [projectId: string]: Collaborator[] }

export function userToCollaborator(user: UserDetails): Collaborator {
export function userToCollaborator(
user: Pick<UserDetails, 'user_id' | 'name' | 'picture'>,
): Collaborator {
return {
id: user.user_id,
name: user.name,
Expand Down

0 comments on commit 9359aef

Please sign in to comment.