Skip to content

Commit

Permalink
refactor(viewers): disconnect viewers from datarooms
Browse files Browse the repository at this point in the history
  • Loading branch information
mfts committed Sep 20, 2024
1 parent 361a8f5 commit 1525dfe
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
1 change: 1 addition & 0 deletions pages/api/teams/[teamId]/datarooms/[id]/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default async function handle(
data: emails.map((email) => ({
email,
dataroomId,
teamId,
invitedAt: new Date(),
})),
skipDuplicates: true,
Expand Down
50 changes: 28 additions & 22 deletions pages/api/teams/[teamId]/datarooms/[id]/viewers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,48 @@ 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,
},
},
},
},
},
});

const dataroom = await prisma.dataroom.findUnique({
where: {
id: dataroomId,
teamId: teamId,
},
select: {
name: true,
},
});

const users = await prisma.user.findMany({
where: {
teams: {
Expand All @@ -91,8 +99,6 @@ export default async function handle(
},
});

const viewers = dataroom?.viewers || [];

const returnViews = viewers.map((viewer) => {
return {
...viewer,
Expand All @@ -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);
Expand Down
14 changes: 9 additions & 5 deletions pages/api/views-dataroom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export default async function handle(
watermarkConfig: true,
groupId: true,
audienceType: true,
dataroom: {
select: {
teamId: true,
},
},
},
});

Expand Down Expand Up @@ -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 },
});
Expand All @@ -344,6 +347,7 @@ export default async function handle(
email: email,
dataroomId: dataroomId!,
verified: isEmailVerified,
teamId: link.dataroom?.teamId!,
},
select: { id: true },
});
Expand Down

0 comments on commit 1525dfe

Please sign in to comment.