Made it possible to add time to a specific account as well
This commit is contained in:
@@ -4,17 +4,18 @@ import {app} from "@/firebase";
|
||||
import {getFirestore, setDoc, doc, getDocs, query, collection, where} from "firebase/firestore";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {Type} from "@/interfaces/user";
|
||||
import {Type, User} from "@/interfaces/user";
|
||||
import {PERMISSIONS} from "@/constants/userPermissions";
|
||||
import {uuidv4} from "@firebase/util";
|
||||
import {prepareMailer, prepareMailOptions} from "@/email";
|
||||
import * as Stripe from "stripe";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import moment from "moment";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, expiryDate, key, checkout} = req.body as {email: string; expiryDate: Date; key: string; checkout: string};
|
||||
const {email, days, key, checkout} = req.body as {email: string; days: string; key: string; checkout: string};
|
||||
if (!key || key !== process.env.STRIPE_KEY) {
|
||||
res.status(403).json({ok: false});
|
||||
return;
|
||||
@@ -29,8 +30,35 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
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) {
|
||||
res.status(200).json({ok: true});
|
||||
return;
|
||||
}
|
||||
|
||||
const codeUserRef = await getDocs(query(collection(db, "codes"), where("userId", "==", user.id)));
|
||||
const userCode = codeUserRef.docs[0];
|
||||
|
||||
if (userCode.data().checkout && userCode.data().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 setDoc(userCode.ref, {checkout}, {merge: true});
|
||||
|
||||
res.status(200).json({ok: true});
|
||||
return;
|
||||
}
|
||||
|
||||
const codeRef = doc(db, "codes", code);
|
||||
await setDoc(codeRef, {type: "student", code, expiryDate, checkout});
|
||||
await setDoc(codeRef, {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