Refactored /api/users
This commit is contained in:
@@ -1,17 +1,10 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { app } from "@/firebase";
|
||||
import {
|
||||
getFirestore,
|
||||
collection,
|
||||
getDocs,
|
||||
getDoc,
|
||||
doc,
|
||||
} from "firebase/firestore";
|
||||
import client from "@/lib/mongodb";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
|
||||
const db = getFirestore(app);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
@@ -22,8 +15,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
const { id } = req.query as { id: string };
|
||||
const snapshot = await getDoc(doc(db, "users", id));
|
||||
if (!snapshot.exists()) return res.status(404).json({ ok: false });
|
||||
const snapshot = await db.collection("users").findOne({ id: id });
|
||||
|
||||
res.status(200).json({ ...snapshot.data(), id: snapshot.id });
|
||||
if (!snapshot) return res.status(404).json({ ok: false });
|
||||
res.status(200).json(snapshot);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
import { app, adminApp } from "@/firebase";
|
||||
import { AgentUser } from "@/interfaces/user";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import {
|
||||
collection,
|
||||
getDocs,
|
||||
getFirestore,
|
||||
query,
|
||||
where,
|
||||
} from "firebase/firestore";
|
||||
import { getAuth } from "firebase-admin/auth";
|
||||
import client from "@/lib/mongodb";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import countryCodes from "country-codes-list";
|
||||
const db = getFirestore(app);
|
||||
const auth = getAuth(adminApp);
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(user, sessionOptions);
|
||||
|
||||
@@ -28,14 +21,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
language: string;
|
||||
};
|
||||
|
||||
const usersQuery = query(
|
||||
collection(db, "users"),
|
||||
where("type", "==", "agent"),
|
||||
where("demographicInformation.country", "==", code),
|
||||
);
|
||||
const docsUser = await getDocs(usersQuery);
|
||||
|
||||
const docs = docsUser.docs.map((doc) => doc.data() as AgentUser);
|
||||
const docs = await db.collection("users").find<AgentUser>({
|
||||
type: "agent",
|
||||
"demographicInformation.country": code
|
||||
}).toArray();
|
||||
|
||||
const entries = docs.map((user: AgentUser) => {
|
||||
const newUser = {
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
import { app, adminApp } from "@/firebase";
|
||||
import { AgentUser } from "@/interfaces/user";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import {
|
||||
collection,
|
||||
getDocs,
|
||||
getFirestore,
|
||||
query,
|
||||
where,
|
||||
} from "firebase/firestore";
|
||||
import { getAuth } from "firebase-admin/auth";
|
||||
import client from "@/lib/mongodb";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import countryCodes from "country-codes-list";
|
||||
const db = getFirestore(app);
|
||||
const auth = getAuth(adminApp);
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
|
||||
export default withIronSessionApiRoute(user, sessionOptions);
|
||||
|
||||
@@ -25,13 +18,7 @@ interface Contact {
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { language = "en" } = req.query as { language: string };
|
||||
|
||||
const usersQuery = query(
|
||||
collection(db, "users"),
|
||||
where("type", "==", "agent"),
|
||||
);
|
||||
const docsUser = await getDocs(usersQuery);
|
||||
|
||||
const docs = docsUser.docs.map((doc) => doc.data() as AgentUser);
|
||||
const docs = await db.collection("users").find<AgentUser>({type: "agent"}).toArray();
|
||||
|
||||
const data = docs.reduce(
|
||||
(acc: Record<string, Contact[]>, user: AgentUser) => {
|
||||
|
||||
@@ -13,8 +13,9 @@ import ShortUniqueId from "short-unique-id";
|
||||
import { Payment } from "@/interfaces/paypal";
|
||||
import { toFixedNumber } from "@/utils/number";
|
||||
import { propagateExpiryDateChanges, propagateStatusChange } from "@/utils/propagate.user.changes";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const db = getFirestore(app);
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
const auth = getAuth(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
@@ -23,7 +24,7 @@ export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
// because the id is not a par of the hash and payment expects date to be of type Date
|
||||
// but if it is not inserted as a string, some UI components will not work (Invalid Date)
|
||||
const addPaymentRecord = async (data: any) => {
|
||||
await setDoc(doc(db, "payments", data.id), data);
|
||||
await db.collection("payments").insertOne(data);
|
||||
};
|
||||
const managePaymentRecords = async (user: User, userId: string | undefined): Promise<boolean> => {
|
||||
try {
|
||||
@@ -41,16 +42,15 @@ const managePaymentRecords = async (user: User, userId: string | undefined): Pro
|
||||
date: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const corporatePayments = await getDocs(query(collection(db, "payments"), where("corporate", "==", userId)));
|
||||
if (corporatePayments.docs.length === 0) {
|
||||
const corporatePayments = await db.collection("payments").find({ corporate: userId }).toArray();
|
||||
if (corporatePayments.length === 0) {
|
||||
await addPaymentRecord(data);
|
||||
return true;
|
||||
}
|
||||
|
||||
const hasPaymentPaidAndExpiring = corporatePayments.docs.filter((doc) => {
|
||||
const data = doc.data();
|
||||
const hasPaymentPaidAndExpiring = corporatePayments.filter((doc) => {
|
||||
return (
|
||||
data.isPaid &&
|
||||
doc.isPaid &&
|
||||
moment().isAfter(moment(user.subscriptionExpirationDate).subtract(30, "days")) &&
|
||||
moment().isBefore(moment(user.subscriptionExpirationDate))
|
||||
);
|
||||
@@ -78,19 +78,20 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const queryId = req.query.id as string;
|
||||
|
||||
const userRef = doc(db, "users", queryId ? (queryId as string) : req.session.user.id);
|
||||
const userSnapshot = await getDoc(userRef);
|
||||
let user = await db.collection("users").findOne<User>({ id: queryId ? (queryId as string) : req.session.user.id });
|
||||
const updatedUser = req.body as User & { password?: string; newPassword?: string };
|
||||
|
||||
if (!!queryId) {
|
||||
await setDoc(userRef, updatedUser, {merge: true});
|
||||
await managePaymentRecords(updatedUser, updatedUser.id);
|
||||
const user = {...userSnapshot.data(), id: userSnapshot.id} as User;
|
||||
await db.collection("users").updateOne(
|
||||
{ id: queryId },
|
||||
{ $set: updatedUser }
|
||||
);
|
||||
|
||||
await managePaymentRecords(updatedUser, updatedUser.id);
|
||||
if (updatedUser.status || updatedUser.type === "corporate") {
|
||||
// there's no await as this does not affect the user
|
||||
propagateStatusChange(queryId, updatedUser.status);
|
||||
propagateExpiryDateChanges(queryId, user.subscriptionExpirationDate, updatedUser.subscriptionExpirationDate || null);
|
||||
propagateExpiryDateChanges(queryId, user?.subscriptionExpirationDate, updatedUser.subscriptionExpirationDate || null);
|
||||
}
|
||||
|
||||
res.status(200).json({ ok: true });
|
||||
@@ -120,8 +121,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
if (updatedUser.email !== req.session.user.email && updatedUser.password) {
|
||||
try {
|
||||
const usersWithSameEmail = await getDocs(query(collection(db, "users"), where("email", "==", updatedUser.email.toLowerCase())));
|
||||
if (usersWithSameEmail.docs.length > 0) {
|
||||
const usersWithSameEmail = await db.collection("users").find({ email: updatedUser.email.toLowerCase() }).toArray();
|
||||
|
||||
if (usersWithSameEmail.length > 0) {
|
||||
res.status(400).json({ error: "E003", message: errorMessages.E003 });
|
||||
return;
|
||||
}
|
||||
@@ -130,18 +132,17 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
await updateEmail(credential.user, updatedUser.email);
|
||||
|
||||
if (req.session.user.type === "student") {
|
||||
const corporateAdmins = ((await getDocs(collection(db, "users"))).docs.map((x) => ({...x.data(), id: x.id})) as User[])
|
||||
.filter((x) => x.type === "corporate")
|
||||
.map((x) => x.id);
|
||||
const groups = ((await getDocs(collection(db, "groups"))).docs.map((x) => ({...x.data(), id: x.id})) as Group[]).filter(
|
||||
(x) => x.participants.includes(req.session.user!.id) && corporateAdmins.includes(x.admin),
|
||||
);
|
||||
const corporateAdmins = (await db.collection("users").find<User>({ type: "corporate" }).toArray()).map((x) => x.id);
|
||||
|
||||
const groups = await db.collection("groups").find<Group>({
|
||||
participants: req.session.user!.id,
|
||||
admin: { $in: corporateAdmins }
|
||||
}).toArray();
|
||||
|
||||
groups.forEach(async (group) => {
|
||||
await setDoc(
|
||||
doc(db, "groups", group.id),
|
||||
{participants: group.participants.filter((x) => x !== req.session.user!.id)},
|
||||
{merge: true},
|
||||
await db.collection("groups").updateOne(
|
||||
{ id: group.id },
|
||||
{ $set: { participants: group.participants.filter((x) => x !== req.session.user!.id) } }
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -159,18 +160,21 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
delete updatedUser.password;
|
||||
delete updatedUser.newPassword;
|
||||
|
||||
await setDoc(userRef, updatedUser, {merge: true});
|
||||
await db.collection("users").updateOne(
|
||||
{ id: queryId },
|
||||
{ $set: updatedUser }
|
||||
);
|
||||
|
||||
const docUser = await getDoc(doc(db, "users", req.session.user.id));
|
||||
const user = docUser.data() as User;
|
||||
user = await db.collection("users").findOne<User>({ id: req.session.user.id });
|
||||
|
||||
if (!queryId) {
|
||||
req.session.user = {...user, id: req.session.user.id};
|
||||
req.session.user = user ? user : null;
|
||||
await req.session.save();
|
||||
}
|
||||
|
||||
if (user) {
|
||||
await managePaymentRecords(user, queryId);
|
||||
|
||||
}
|
||||
res.status(200).json({ user });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user