Finished refactoring

This commit is contained in:
Carlos Mesquita
2024-09-07 22:39:14 +01:00
parent c2b4bb29d6
commit 6e4ef249b8
16 changed files with 169 additions and 188 deletions

View File

@@ -1,32 +1,29 @@
// updating specific user changes other users
// for example, updating the status of a corporate user should update the status of all users in the same corporate group
import {UserStatus, User} from "../interfaces/user";
import {getFirestore, collection, getDocs, getDoc, doc, setDoc, query, where} from "firebase/firestore";
import {app} from "@/firebase";
import { UserStatus, User } from "../interfaces/user";
import client from "@/lib/mongodb";
const db = getFirestore(app);
const db = client.db(process.env.MONGODB_DB);
export const propagateStatusChange = (userId: string, status: UserStatus) =>
new Promise((resolve, reject) => {
getDoc(doc(db, "users", userId))
db.collection("users").findOne<User>({ id: userId })
.then((docUser) => {
const user = docUser.data() as User;
if (!docUser) return;
const user = docUser;
// only update the status of the user's groups if the user is a corporate user
if (user.type === "corporate" || user.type === "mastercorporate") {
getDocs(query(collection(db, "groups"), where("admin", "==", userId))).then(async (userGroupsRef) => {
const userGroups = userGroupsRef.docs.map((x) => x.data());
db.collection("groups").find({ admin: userId }).toArray().then(async (userGroupsRef) => {
const userGroups = userGroupsRef.map((x) => x.data());
const targetUsers = [...new Set(userGroups.flatMap((g) => g.participants).filter((u) => u))];
Promise.all(
targetUsers.map(async (targetUserId) => {
const ref = await getDoc(doc(db, "users", targetUserId));
if (!ref.exists()) return null;
const data = ref.data() as User;
return {...data, id: targetUserId};
const ref = await db.collection("users").findOne<User>({ id: targetUserId });
if (!ref) return null;
return ref;
}),
)
.then((data) => {
@@ -40,7 +37,7 @@ export const propagateStatusChange = (userId: string, status: UserStatus) =>
return;
}
Promise.all(filtered.map((user: User) => setDoc(doc(db, "users", user.id), {status}, {merge: true})))
Promise.all(filtered.map((user: User) => db.collection("users").updateOne({ id: user.id }, { $set: { status } })))
.then(() => {
resolve(true);
})
@@ -69,25 +66,21 @@ export const propagateStatusChange = (userId: string, status: UserStatus) =>
export const propagateExpiryDateChanges = (userId: string, initialExpiryDate: Date | null | undefined, subscriptionExpirationDate: Date | null) =>
new Promise((resolve, reject) => {
getDoc(doc(db, "users", userId))
.then((docUser) => {
const user = docUser.data() as User;
db.collection("users").findOne<User>({ id: userId })
.then((user) => {
if (!user) return;
// only update the status of the user's groups if the user is a corporate user
if (user.type === "corporate") {
getDocs(query(collection(db, "groups"), where("admin", "==", userId))).then(async (userGroupsRef) => {
const userGroups = userGroupsRef.docs.map((x) => x.data());
db.collection("groups").find({ admin: userId }).toArray().then(async (userGroupsRef) => {
const userGroups = userGroupsRef.map((x) => x.data());
const targetUsers = [...new Set(userGroups.flatMap((g) => g.participants).filter((u) => u))];
Promise.all(
targetUsers.map(async (targetUserId) => {
const ref = await getDoc(doc(db, "users", targetUserId));
if (!ref.exists()) return null;
const data = ref.data() as User;
return {...data, id: ref.id};
const ref = await db.collection("users").findOne<User>({ id: targetUserId });
return ref;
}),
)
.then(async (data) => {
@@ -101,7 +94,10 @@ export const propagateExpiryDateChanges = (userId: string, initialExpiryDate: Da
if (filtered.length === 0) return;
for (const user of filtered) {
await setDoc(doc(db, "users", user.id), {subscriptionExpirationDate}, {merge: true});
await db.collection("users").updateOne(
{ id: user.id },
{ $set: { subscriptionExpirationDate } }
);
}
resolve(true);