Prevented the creation of multiple equal invites
This commit is contained in:
@@ -45,6 +45,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const body = req.body as Invite;
|
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));
|
const invitedRef = await getDoc(doc(db, "users", body.to));
|
||||||
if (!invitedRef.exists()) return res.status(404).json({ ok: false });
|
if (!invitedRef.exists()) return res.status(404).json({ ok: false });
|
||||||
|
|
||||||
@@ -72,8 +76,13 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
existingInvites.filter((i) => i.to === body.to && i.from === body.from)
|
||||||
|
.length == 0
|
||||||
|
) {
|
||||||
const shortUID = new ShortUniqueId();
|
const shortUID = new ShortUniqueId();
|
||||||
await setDoc(doc(db, "invites", body.id || shortUID.randomUUID(8)), body);
|
await setDoc(doc(db, "invites", body.id || shortUID.randomUUID(8)), body);
|
||||||
|
}
|
||||||
|
|
||||||
res.status(200).json({ ok: true });
|
res.status(200).json({ ok: true });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user