From 4e40dc9c8c43d2a6d507419b6e28f3d8b31e5a45 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Tue, 30 Jan 2024 17:50:48 +0000 Subject: [PATCH] Prevented the creation of multiple equal invites --- src/pages/api/invites/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pages/api/invites/index.ts b/src/pages/api/invites/index.ts index 72402665..023e6e2d 100644 --- a/src/pages/api/invites/index.ts +++ b/src/pages/api/invites/index.ts @@ -45,6 +45,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) { async function post(req: NextApiRequest, res: NextApiResponse) { const body = req.body as Invite; + const existingInvites = (await getDocs(collection(db, "invites"))).docs.map( + (x) => ({ ...x.data(), id: x.id }), + ) as Invite[]; + const invitedRef = await getDoc(doc(db, "users", body.to)); if (!invitedRef.exists()) return res.status(404).json({ ok: false }); @@ -72,8 +76,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) { console.log(e); } - const shortUID = new ShortUniqueId(); - await setDoc(doc(db, "invites", body.id || shortUID.randomUUID(8)), body); + if ( + existingInvites.filter((i) => i.to === body.to && i.from === body.from) + .length == 0 + ) { + const shortUID = new ShortUniqueId(); + await setDoc(doc(db, "invites", body.id || shortUID.randomUUID(8)), body); + } res.status(200).json({ ok: true }); }