forked from mfts/papermark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { getServerSession } from "next-auth/next"; | ||
|
||
import { errorhandler } from "@/lib/errorHandler"; | ||
import prisma from "@/lib/prisma"; | ||
import { CustomUser } from "@/lib/types"; | ||
|
||
import { authOptions } from "../../auth/[...nextauth]"; | ||
|
||
export default async function handle( | ||
req: NextApiRequest, | ||
res: NextApiResponse, | ||
) { | ||
if (req.method === "POST") { | ||
// PUT /api/links/:id/duplicate | ||
const session = await getServerSession(req, res, authOptions); | ||
if (!session) { | ||
return res.status(401).end("Unauthorized"); | ||
} | ||
|
||
const { id, teamId } = req.query as { id: string; teamId: string }; | ||
|
||
try { | ||
const team = await prisma.team.findUnique({ | ||
where: { | ||
id: teamId, | ||
users: { | ||
some: { | ||
userId: (session.user as CustomUser).id, | ||
}, | ||
}, | ||
}, | ||
select: { id: true }, | ||
}); | ||
|
||
if (!team) { | ||
return res.status(401).end("Unauthorized"); | ||
} | ||
|
||
const link = await prisma.link.findUnique({ | ||
where: { id }, | ||
include: { | ||
dataroom: { | ||
select: { | ||
teamId: true, | ||
}, | ||
}, | ||
document: { | ||
select: { teamId: true }, | ||
}, | ||
}, | ||
}); | ||
|
||
if (!link) { | ||
return res.status(404).json({ error: "Link not found" }); | ||
} | ||
|
||
const { dataroom, document, ...linkData } = link; | ||
|
||
const newLinkName = linkData.name | ||
? linkData.name + " (Copy)" | ||
: `Link #${linkData.id.slice(-5)} (Copy)`; | ||
const newLink = await prisma.link.create({ | ||
data: { | ||
...linkData, | ||
id: undefined, | ||
slug: linkData.slug ? linkData.slug + "-copy" : null, | ||
name: newLinkName, | ||
createdAt: undefined, | ||
updatedAt: undefined, | ||
}, | ||
}); | ||
|
||
const linkWithView = { | ||
...newLink, | ||
_count: { views: 0 }, | ||
views: [], | ||
}; | ||
|
||
return res.status(201).json(linkWithView); | ||
} catch (error) { | ||
errorhandler(error, res); | ||
} | ||
} | ||
|
||
// We only allow PUT requests | ||
res.setHeader("Allow", ["POST"]); | ||
return res.status(405).end(`Method ${req.method} Not Allowed`); | ||
} |