diff --git a/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/members/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/members/index.ts index 3f42e03ee..72eaec20f 100644 --- a/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/members/index.ts +++ b/pages/api/teams/[teamId]/datarooms/[id]/groups/[groupId]/members/index.ts @@ -61,10 +61,11 @@ export default async function handle( } // First, create or connect viewers - const something = await prisma.viewer.createMany({ + await prisma.viewer.createMany({ data: emails.map((email) => ({ email, dataroomId, + teamId, })), skipDuplicates: true, }); diff --git a/pages/api/teams/[teamId]/datarooms/[id]/users/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/users/index.ts index 66d8c1082..4d738278f 100644 --- a/pages/api/teams/[teamId]/datarooms/[id]/users/index.ts +++ b/pages/api/teams/[teamId]/datarooms/[id]/users/index.ts @@ -67,6 +67,7 @@ export default async function handle( data: emails.map((email) => ({ email, dataroomId, + teamId, invitedAt: new Date(), })), skipDuplicates: true, diff --git a/pages/api/teams/[teamId]/datarooms/[id]/viewers/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/viewers/index.ts index 39f8bf732..c8d2404dd 100644 --- a/pages/api/teams/[teamId]/datarooms/[id]/viewers/index.ts +++ b/pages/api/teams/[teamId]/datarooms/[id]/viewers/index.ts @@ -44,33 +44,31 @@ export default async function handle( return res.status(403).end("Unauthorized to access this team"); } - const dataroom = await prisma.dataroom.findUnique({ + const viewers = await prisma.viewer.findMany({ where: { - id: dataroomId, - teamId: teamId, + views: { + some: { + dataroomId: dataroomId, + viewType: "DATAROOM_VIEW", + }, + }, }, select: { id: true, teamId: true, - name: true, - viewers: { + email: true, + views: { + where: { + dataroomId: dataroomId, + viewType: "DATAROOM_VIEW", + }, orderBy: { - createdAt: "desc", + viewedAt: "desc", }, include: { - views: { - where: { - viewType: "DATAROOM_VIEW", - }, - orderBy: { - viewedAt: "desc", - }, - include: { - link: { - select: { - name: true, - }, - }, + link: { + select: { + name: true, }, }, }, @@ -78,6 +76,16 @@ export default async function handle( }, }); + const dataroom = await prisma.dataroom.findUnique({ + where: { + id: dataroomId, + teamId: teamId, + }, + select: { + name: true, + }, + }); + const users = await prisma.user.findMany({ where: { teams: { @@ -91,8 +99,6 @@ export default async function handle( }, }); - const viewers = dataroom?.viewers || []; - const returnViews = viewers.map((viewer) => { return { ...viewer, @@ -106,7 +112,7 @@ export default async function handle( return res.status(200).json(returnViews); } catch (error) { log({ - message: `Failed to get views for dataroom: _${dataroomId}_. \n\n ${error} \n\n*Metadata*: \`{teamId: ${teamId}, userId: ${userId}}\``, + message: `Failed to get viewers for dataroom: _${dataroomId}_. \n\n ${error} \n\n*Metadata*: \`{teamId: ${teamId}, userId: ${userId}}\``, type: "error", }); errorhandler(error, res); diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.ts index 98ff6d03b..ac4c87788 100644 --- a/pages/api/views-dataroom.ts +++ b/pages/api/views-dataroom.ts @@ -106,6 +106,11 @@ export default async function handle( watermarkConfig: true, groupId: true, audienceType: true, + dataroom: { + select: { + teamId: true, + }, + }, }, }); @@ -326,12 +331,10 @@ export default async function handle( if (email && !isPreview) { // find or create a viewer console.time("find-viewer"); - viewer = await prisma.viewer.findUnique({ + viewer = await prisma.viewer.findFirst({ where: { - dataroomId_email: { - email: email, - dataroomId: dataroomId!, - }, + email: email, + teamId: link.dataroom?.teamId, }, select: { id: true }, }); @@ -344,6 +347,7 @@ export default async function handle( email: email, dataroomId: dataroomId!, verified: isEmailVerified, + teamId: link.dataroom?.teamId!, }, select: { id: true }, });