ENCOA-109: Keep the same balance after deleting a user
This commit is contained in:
@@ -6,6 +6,7 @@ import {sessionOptions} from "@/lib/session";
|
|||||||
import {v4} from "uuid";
|
import {v4} from "uuid";
|
||||||
import {CorporateUser, Group} from "@/interfaces/user";
|
import {CorporateUser, Group} from "@/interfaces/user";
|
||||||
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
import {createUserWithEmailAndPassword, getAuth} from "firebase/auth";
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
|
|
||||||
const DEFAULT_DESIRED_LEVELS = {
|
const DEFAULT_DESIRED_LEVELS = {
|
||||||
reading: 9,
|
reading: 9,
|
||||||
@@ -73,7 +74,22 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
subscriptionExpirationDate: expiryDate || null,
|
subscriptionExpirationDate: expiryDate || null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uid = new ShortUniqueId();
|
||||||
|
const code = uid.randomUUID(6);
|
||||||
|
|
||||||
await setDoc(doc(db, "users", userId), user);
|
await setDoc(doc(db, "users", userId), user);
|
||||||
|
await setDoc(doc(db, "codes", code), {
|
||||||
|
code,
|
||||||
|
creator: maker.id,
|
||||||
|
expiryDate,
|
||||||
|
type,
|
||||||
|
creationDate: new Date(),
|
||||||
|
userId,
|
||||||
|
email: email.toLowerCase(),
|
||||||
|
name: req.body.name,
|
||||||
|
passport_id,
|
||||||
|
});
|
||||||
|
|
||||||
if (type === "corporate") {
|
if (type === "corporate") {
|
||||||
const defaultTeachersGroup: Group = {
|
const defaultTeachersGroup: Group = {
|
||||||
admin: userId,
|
admin: userId,
|
||||||
@@ -110,6 +126,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
|
|
||||||
if (!corporateSnapshot.empty) {
|
if (!corporateSnapshot.empty) {
|
||||||
const corporateUser = corporateSnapshot.docs[0].data() as CorporateUser;
|
const corporateUser = corporateSnapshot.docs[0].data() as CorporateUser;
|
||||||
|
await setDoc(doc(db, "codes", code), {creator: corporateUser.id}, {merge: true});
|
||||||
|
|
||||||
const q = query(
|
const q = query(
|
||||||
collection(db, "groups"),
|
collection(db, "groups"),
|
||||||
@@ -124,7 +141,7 @@ async function post(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const participants: string[] = doc.get("participants");
|
const participants: string[] = doc.get("participants");
|
||||||
|
|
||||||
if (!participants.includes(userId)) {
|
if (!participants.includes(userId)) {
|
||||||
updateDoc(doc.ref, {
|
await updateDoc(doc.ref, {
|
||||||
participants: [...participants, userId],
|
participants: [...participants, userId],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ export async function getUserBalance(user: User) {
|
|||||||
const groups = await getGroupsForUser(user.id);
|
const groups = await getGroupsForUser(user.id);
|
||||||
const participants = uniq(groups.flatMap((x) => x.participants));
|
const participants = uniq(groups.flatMap((x) => x.participants));
|
||||||
|
|
||||||
if (user.type === "corporate") return participants.length + codes.length;
|
if (user.type === "corporate") return participants.length + codes.filter((x) => !participants.includes(x.userId || "")).length;
|
||||||
|
|
||||||
const participantUsers = await Promise.all(participants.map(getUser));
|
const participantUsers = await Promise.all(participants.map(getUser));
|
||||||
const corporateUsers = participantUsers.filter((x) => x.type === "corporate") as CorporateUser[];
|
const corporateUsers = participantUsers.filter((x) => x.type === "corporate") as CorporateUser[];
|
||||||
@@ -50,6 +50,6 @@ export async function getUserBalance(user: User) {
|
|||||||
return (
|
return (
|
||||||
corporateUsers.reduce((acc, curr) => acc + curr.corporateInformation?.companyInformation?.userAmount || 0, 0) +
|
corporateUsers.reduce((acc, curr) => acc + curr.corporateInformation?.companyInformation?.userAmount || 0, 0) +
|
||||||
corporateUsers.length +
|
corporateUsers.length +
|
||||||
codes.length
|
codes.filter((x) => !participants.includes(x.userId || "") && !corporateUsers.map((u) => u.id).includes(x.userId || "")).length
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user