Avoid duplicating any video credential (#3593)

Co-authored-by: gitstart <gitstart@users.noreply.github.com>
Co-authored-by: alannnc <alannnc@gmail.com>
pull/3437/head^2
GitStart 2022-08-03 00:22:21 +05:30 committed by GitHub
parent e767d233ad
commit 6301ea16f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -34,6 +34,26 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const responseBody = await result.json();
const userId = req.session?.user.id;
if (!userId) {
return res.status(404).json({ message: "No user found" });
}
const existingCredentialTandemVideo = await prisma.credential.findMany({
select: {
id: true,
},
where: {
type: "tandem_video",
userId: req.session?.user.id,
appId: "tandem",
},
});
const credentialIdsToDelete = existingCredentialTandemVideo.map((item) => item.id);
if (credentialIdsToDelete.length > 0) {
await prisma.credential.deleteMany({ where: { id: { in: credentialIdsToDelete }, userId } });
}
if (result.ok) {
responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000);
delete responseBody.expires_in;

View File

@ -26,6 +26,32 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
responseBody.expiry_date = Math.round(Date.now() + responseBody.expires_in * 1000);
delete responseBody.expires_in;
const userId = req.session?.user.id;
if (!userId) {
return res.status(404).json({ message: "No user found" });
}
/**
* With this we take care of no duplicate zoom_video key for a single user
* when creating a video room we only do findFirst so the if they have more than 1
* others get ignored
* */
const existingCredentialZoomVideo = await prisma.credential.findMany({
select: {
id: true,
},
where: {
type: "zoom_video",
userId: req.session?.user.id,
appId: "zoom",
},
});
// Making sure we only delete zoom_video
const credentialIdsToDelete = existingCredentialZoomVideo.map((item) => item.id);
if (credentialIdsToDelete.length > 0) {
await prisma.credential.deleteMany({ where: { id: { in: credentialIdsToDelete }, userId } });
}
await prisma.user.update({
where: {
id: req.session?.user.id,