Created a simple invite system that notifies users via e-mail when a corporate uploads an Excel file with already registered students
This commit is contained in:
82
src/pages/api/invites/[id].ts
Normal file
82
src/pages/api/invites/[id].ts
Normal file
@@ -0,0 +1,82 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { app } from "@/firebase";
|
||||
import {
|
||||
getFirestore,
|
||||
getDoc,
|
||||
doc,
|
||||
deleteDoc,
|
||||
setDoc,
|
||||
} from "firebase/firestore";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { Ticket } from "@/interfaces/ticket";
|
||||
import { Invite } from "@/interfaces/invite";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") return await get(req, res);
|
||||
if (req.method === "DELETE") return await del(req, res);
|
||||
if (req.method === "PATCH") return await patch(req, res);
|
||||
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
|
||||
if (snapshot.exists()) {
|
||||
res.status(200).json({ ...snapshot.data(), id: snapshot.id });
|
||||
} else {
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async function del(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
const data = snapshot.data() as Invite;
|
||||
|
||||
const user = req.session.user;
|
||||
if (user.type === "admin" || user.type === "developer") {
|
||||
await deleteDoc(snapshot.ref);
|
||||
res.status(200).json({ ok: true });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(403).json({ ok: false });
|
||||
}
|
||||
|
||||
async function patch(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
|
||||
const user = req.session.user;
|
||||
if (user.type === "admin" || user.type === "developer") {
|
||||
await setDoc(snapshot.ref, req.body, { merge: true });
|
||||
return res.status(200).json({ ok: true });
|
||||
}
|
||||
|
||||
res.status(403).json({ ok: false });
|
||||
}
|
||||
134
src/pages/api/invites/accept/[id].ts
Normal file
134
src/pages/api/invites/accept/[id].ts
Normal file
@@ -0,0 +1,134 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { app } from "@/firebase";
|
||||
import {
|
||||
getFirestore,
|
||||
getDoc,
|
||||
doc,
|
||||
deleteDoc,
|
||||
setDoc,
|
||||
getDocs,
|
||||
collection,
|
||||
where,
|
||||
query,
|
||||
} from "firebase/firestore";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { Ticket } from "@/interfaces/ticket";
|
||||
import { Invite } from "@/interfaces/invite";
|
||||
import { Group, User } from "@/interfaces/user";
|
||||
import { v4 } from "uuid";
|
||||
import { sendEmail } from "@/email";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") return await get(req, res);
|
||||
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
|
||||
if (snapshot.exists()) {
|
||||
const invite = { ...snapshot.data(), id: snapshot.id } as Invite;
|
||||
if (invite.to !== req.session.user.id)
|
||||
return res.status(403).json({ ok: false });
|
||||
|
||||
await deleteDoc(snapshot.ref);
|
||||
const invitedByRef = await getDoc(doc(db, "users", invite.from));
|
||||
if (!invitedByRef.exists()) return res.status(404).json({ ok: false });
|
||||
|
||||
const invitedBy = { ...invitedByRef.data(), id: invitedByRef.id } as User;
|
||||
const invitedByGroupsRef = await getDocs(
|
||||
query(collection(db, "groups"), where("admin", "==", invitedBy.id)),
|
||||
);
|
||||
const invitedByGroups = invitedByGroupsRef.docs.map((g) => ({
|
||||
...g.data(),
|
||||
id: g.id,
|
||||
})) as Group[];
|
||||
|
||||
const typeGroupName =
|
||||
req.session.user.type === "student"
|
||||
? "Students"
|
||||
: req.session.user.type === "teacher"
|
||||
? "Teachers"
|
||||
: undefined;
|
||||
|
||||
if (typeGroupName) {
|
||||
const typeGroup: Group = invitedByGroups.find(
|
||||
(g) => g.name === typeGroupName,
|
||||
) || {
|
||||
id: v4(),
|
||||
admin: invitedBy.id,
|
||||
name: typeGroupName,
|
||||
participants: [],
|
||||
disableEditing: true,
|
||||
};
|
||||
await setDoc(
|
||||
doc(db, "groups", typeGroup.id),
|
||||
{
|
||||
...typeGroup,
|
||||
participants: [
|
||||
...typeGroup.participants.filter((x) => x !== req.session.user!.id),
|
||||
req.session.user.id,
|
||||
],
|
||||
},
|
||||
{ merge: true },
|
||||
);
|
||||
}
|
||||
|
||||
const invitationsGroup: Group = invitedByGroups.find(
|
||||
(g) => g.name === "Invited",
|
||||
) || {
|
||||
id: v4(),
|
||||
admin: invitedBy.id,
|
||||
name: "Invited",
|
||||
participants: [],
|
||||
disableEditing: true,
|
||||
};
|
||||
await setDoc(
|
||||
doc(db, "groups", invitationsGroup.id),
|
||||
{
|
||||
...invitationsGroup,
|
||||
participants: [
|
||||
...invitationsGroup.participants.filter(
|
||||
(x) => x !== req.session.user!.id,
|
||||
),
|
||||
req.session.user.id,
|
||||
],
|
||||
},
|
||||
{
|
||||
merge: true,
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
await sendEmail(
|
||||
"respondedInvite",
|
||||
{
|
||||
corporateName: invitedBy.name,
|
||||
name: req.session.user.name,
|
||||
decision: "accept",
|
||||
},
|
||||
[invitedBy.email],
|
||||
`${req.session.user.name} has accepted your invite!`,
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
} else {
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
}
|
||||
72
src/pages/api/invites/decline/[id].ts
Normal file
72
src/pages/api/invites/decline/[id].ts
Normal file
@@ -0,0 +1,72 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { app } from "@/firebase";
|
||||
import {
|
||||
getFirestore,
|
||||
getDoc,
|
||||
doc,
|
||||
deleteDoc,
|
||||
setDoc,
|
||||
getDocs,
|
||||
collection,
|
||||
where,
|
||||
query,
|
||||
} from "firebase/firestore";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import { Ticket } from "@/interfaces/ticket";
|
||||
import { Invite } from "@/interfaces/invite";
|
||||
import { Group, User } from "@/interfaces/user";
|
||||
import { v4 } from "uuid";
|
||||
import { sendEmail } from "@/email";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") return await get(req, res);
|
||||
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const snapshot = await getDoc(doc(db, "invites", id));
|
||||
|
||||
if (snapshot.exists()) {
|
||||
const invite = { ...snapshot.data(), id: snapshot.id } as Invite;
|
||||
if (invite.to !== req.session.user.id)
|
||||
return res.status(403).json({ ok: false });
|
||||
|
||||
await deleteDoc(snapshot.ref);
|
||||
const invitedByRef = await getDoc(doc(db, "users", invite.from));
|
||||
if (!invitedByRef.exists()) return res.status(404).json({ ok: false });
|
||||
|
||||
const invitedBy = { ...invitedByRef.data(), id: invitedByRef.id } as User;
|
||||
|
||||
try {
|
||||
await sendEmail(
|
||||
"respondedInvite",
|
||||
{
|
||||
corporateName: invitedBy.name,
|
||||
name: req.session.user.name,
|
||||
decision: "decline",
|
||||
},
|
||||
[invitedBy.email],
|
||||
`${req.session.user.name} has declined your invite!`,
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
} else {
|
||||
res.status(404).json(undefined);
|
||||
}
|
||||
}
|
||||
79
src/pages/api/invites/index.ts
Normal file
79
src/pages/api/invites/index.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import { sendEmail } from "@/email";
|
||||
import { app } from "@/firebase";
|
||||
import { Invite } from "@/interfaces/invite";
|
||||
import { Ticket } from "@/interfaces/ticket";
|
||||
import { User } from "@/interfaces/user";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import {
|
||||
collection,
|
||||
doc,
|
||||
getDoc,
|
||||
getDocs,
|
||||
getFirestore,
|
||||
setDoc,
|
||||
} from "firebase/firestore";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === "GET") await get(req, res);
|
||||
if (req.method === "POST") await post(req, res);
|
||||
}
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const snapshot = await getDocs(collection(db, "invites"));
|
||||
|
||||
res.status(200).json(
|
||||
snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
const body = req.body as Invite;
|
||||
|
||||
const shortUID = new ShortUniqueId();
|
||||
await setDoc(doc(db, "invites", body.id || shortUID.randomUUID(8)), body);
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
|
||||
const invitedRef = await getDoc(doc(db, "users", body.to));
|
||||
if (!invitedRef.exists()) return res.status(404).json({ ok: false });
|
||||
|
||||
const invitedByRef = await getDoc(doc(db, "users", body.from));
|
||||
if (!invitedByRef.exists()) return res.status(404).json({ ok: false });
|
||||
|
||||
const invited = { ...invitedRef.data(), id: invitedRef.id } as User;
|
||||
const invitedBy = { ...invitedByRef.data(), id: invitedByRef.id } as User;
|
||||
|
||||
try {
|
||||
await sendEmail(
|
||||
"receivedInvite",
|
||||
{
|
||||
name: invited.name,
|
||||
corporateName:
|
||||
invitedBy.type === "corporate"
|
||||
? invitedBy.corporateInformation?.companyInformation?.name ||
|
||||
invitedBy.name
|
||||
: invitedBy.name,
|
||||
},
|
||||
[invited.email],
|
||||
"You have been invited to a group!",
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user