Skip to content

Commit

Permalink
refactor: check if link available on team
Browse files Browse the repository at this point in the history
  • Loading branch information
mfts committed Oct 27, 2024
1 parent 1f5ae5c commit a542b15
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions pages/api/links/[id]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,22 @@ export default async function handle(
const documentLink = linkType === "DOCUMENT_LINK";

try {
const team = await prisma.team.findUnique({
const existingLink = await prisma.link.findUnique({
where: {
id: teamId,
users: {
some: { userId },
id: id,
teamId: teamId,
team: {
users: {
some: { userId },
},
},
},
});

if (!team) {
return res.status(400).json({ error: "Team not found." });
if (!existingLink) {
return res
.status(404)
.json({ error: "Link not found or unauthorized" });
}
} catch (error) {
return res.status(500).json({
Expand Down Expand Up @@ -239,22 +244,24 @@ export default async function handle(
metaImage: linkData.metaImage || null,
metaFavicon: linkData.metaFavicon || null,
enableQuestion: linkData.enableQuestion,
feedback: {
upsert: {
create: {
data: {
question: linkData.questionText,
type: linkData.questionType,
...(linkData.enableQuestion && {
feedback: {
upsert: {
create: {
data: {
question: linkData.questionText,
type: linkData.questionType,
},
},
},
update: {
data: {
question: linkData.questionText,
type: linkData.questionType,
update: {
data: {
question: linkData.questionText,
type: linkData.questionType,
},
},
},
},
},
}),
enableAgreement: linkData.enableAgreement,
agreementId: linkData.agreementId || null,
showBanner: linkData.showBanner,
Expand Down

0 comments on commit a542b15

Please sign in to comment.