Finished refactoring
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {app} from "@/firebase";
|
||||
import {getFirestore, setDoc, doc, getDocs, query, collection, where} from "firebase/firestore";
|
||||
import client from "@/lib/mongodb";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {Type, User} from "@/interfaces/user";
|
||||
@@ -12,7 +11,8 @@ import * as Stripe from "stripe";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import moment from "moment";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, days, key, checkout} = req.body as {email: string; days: string; key: string; checkout: string};
|
||||
@@ -24,41 +24,44 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const uid = new ShortUniqueId();
|
||||
const code = uid.randomUUID(6);
|
||||
|
||||
const codeCheckerRef = await getDocs(query(collection(db, "codes"), where("checkout", "==", checkout)));
|
||||
if (codeCheckerRef.docs.length !== 0) {
|
||||
const codeCheckerRef = await db.collection("codes").find({ checkout: checkout }).toArray();
|
||||
if (codeCheckerRef.length !== 0) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const emailCheckerRef = await getDocs(query(collection(db, "users"), where("email", "==", email)));
|
||||
if (emailCheckerRef.docs.length !== 0) {
|
||||
const user = emailCheckerRef.docs[0];
|
||||
if (!user.data().subscriptionExpirationDate) {
|
||||
const emailCheckerRef = await db.collection("users").find({ email: email }).toArray();
|
||||
if (emailCheckerRef.length !== 0) {
|
||||
const user = emailCheckerRef[0];
|
||||
if (!user.subscriptionExpirationDate) {
|
||||
res.status(200).json({ok: true});
|
||||
return;
|
||||
}
|
||||
|
||||
const codeUserRef = await getDocs(query(collection(db, "codes"), where("userId", "==", user.id)));
|
||||
const userCode = codeUserRef.docs[0];
|
||||
const codeUserRef = await db.collection("codes").find({ userId: user.id }).toArray();
|
||||
const userCode = codeUserRef[0];
|
||||
|
||||
if (userCode.data().checkout && userCode.data().checkout === checkout) {
|
||||
if (userCode.checkout && userCode.checkout === checkout) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
await setDoc(
|
||||
user.ref,
|
||||
{subscriptionExpirationDate: moment(user.data().subscriptionExpirationDate).add(days, "days").toISOString()},
|
||||
{merge: true},
|
||||
await db.collection("users").updateOne(
|
||||
{ id: user.id },
|
||||
{ $set: {subscriptionExpirationDate: moment(user.subscriptionExpirationDate).add(days, "days").toISOString()} }
|
||||
);
|
||||
await setDoc(userCode.ref, {checkout}, {merge: true});
|
||||
await db.collection("codes").updateOne(
|
||||
{ id: userCode.id },
|
||||
{ $set: {checkout} }
|
||||
);
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
return;
|
||||
}
|
||||
|
||||
const codeRef = doc(db, "codes", code);
|
||||
await setDoc(codeRef, {type: "student", code, expiryDate: moment(new Date()).add(days, "days").toISOString(), checkout});
|
||||
await db.collection("codes").updateOne(
|
||||
{ id: code },
|
||||
{ $set: {type: "student", code, expiryDate: moment(new Date()).add(days, "days").toISOString(), checkout} }
|
||||
);
|
||||
|
||||
const transport = prepareMailer();
|
||||
const mailOptions = prepareMailOptions(
|
||||
|
||||
Reference in New Issue
Block a user