ENCOA-96: License Distribuition system from Master Corporate to Corporate
This commit is contained in:
@@ -11,6 +11,7 @@ interface Props {
|
||||
value?: string | number;
|
||||
className?: string;
|
||||
disabled?: boolean;
|
||||
max?: number;
|
||||
name: string;
|
||||
onChange: (value: string) => void;
|
||||
}
|
||||
@@ -23,6 +24,7 @@ export default function Input({
|
||||
required = false,
|
||||
value,
|
||||
defaultValue,
|
||||
max,
|
||||
className,
|
||||
roundness = "full",
|
||||
disabled = false,
|
||||
@@ -72,6 +74,7 @@ export default function Input({
|
||||
name={name}
|
||||
disabled={disabled}
|
||||
value={value}
|
||||
max={max}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
min={type === "number" ? 0 : undefined}
|
||||
placeholder={placeholder}
|
||||
|
||||
@@ -41,6 +41,7 @@ interface Props {
|
||||
onViewStudents?: () => void;
|
||||
onViewTeachers?: () => void;
|
||||
onViewCorporate?: () => void;
|
||||
maxUserAmount?: number;
|
||||
disabled?: boolean;
|
||||
disabledFields?: {
|
||||
countryManager?: boolean;
|
||||
@@ -72,17 +73,31 @@ const CURRENCIES_OPTIONS = CURRENCIES.map(({label, currency}) => ({
|
||||
label,
|
||||
}));
|
||||
|
||||
const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers, onViewCorporate, disabled = false, disabledFields = {}}: Props) => {
|
||||
const UserCard = ({
|
||||
user,
|
||||
loggedInUser,
|
||||
maxUserAmount,
|
||||
onClose,
|
||||
onViewStudents,
|
||||
onViewTeachers,
|
||||
onViewCorporate,
|
||||
disabled = false,
|
||||
disabledFields = {},
|
||||
}: Props) => {
|
||||
const [expiryDate, setExpiryDate] = useState<Date | null | undefined>(user.subscriptionExpirationDate);
|
||||
const [type, setType] = useState(user.type);
|
||||
const [status, setStatus] = useState(user.status);
|
||||
const [referralAgentLabel, setReferralAgentLabel] = useState<string>();
|
||||
const [position, setPosition] = useState<string | undefined>(user.type === "corporate" ? user.demographicInformation?.position : undefined);
|
||||
const [position, setPosition] = useState<string | undefined>(
|
||||
user.type === "corporate" || user.type === "mastercorporate" ? user.demographicInformation?.position : undefined,
|
||||
);
|
||||
const [studentID, setStudentID] = useState<string | undefined>(user.type === "student" ? user.studentID : undefined);
|
||||
|
||||
const [referralAgent, setReferralAgent] = useState(user.type === "corporate" ? user.corporateInformation?.referralAgent : undefined);
|
||||
const [referralAgent, setReferralAgent] = useState(
|
||||
user.type === "corporate" || user.type === "mastercorporate" ? user.corporateInformation?.referralAgent : undefined,
|
||||
);
|
||||
const [companyName, setCompanyName] = useState(
|
||||
user.type === "corporate"
|
||||
user.type === "corporate" || user.type === "mastercorporate"
|
||||
? user.corporateInformation?.companyInformation.name
|
||||
: user.type === "agent"
|
||||
? user.agentInformation?.companyName
|
||||
@@ -92,11 +107,21 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
||||
const [commercialRegistration, setCommercialRegistration] = useState(
|
||||
user.type === "agent" ? user.agentInformation?.commercialRegistration : undefined,
|
||||
);
|
||||
const [userAmount, setUserAmount] = useState(user.type === "corporate" ? user.corporateInformation?.companyInformation.userAmount : undefined);
|
||||
const [paymentValue, setPaymentValue] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.value : undefined);
|
||||
const [paymentCurrency, setPaymentCurrency] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.currency : "EUR");
|
||||
const [monthlyDuration, setMonthlyDuration] = useState(user.type === "corporate" ? user.corporateInformation?.monthlyDuration : undefined);
|
||||
const [commissionValue, setCommission] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.commission : undefined);
|
||||
const [userAmount, setUserAmount] = useState(
|
||||
user.type === "corporate" || user.type === "mastercorporate" ? user.corporateInformation?.companyInformation.userAmount : undefined,
|
||||
);
|
||||
const [paymentValue, setPaymentValue] = useState(
|
||||
user.type === "corporate" || user.type === "mastercorporate" ? user.corporateInformation?.payment?.value : undefined,
|
||||
);
|
||||
const [paymentCurrency, setPaymentCurrency] = useState(
|
||||
user.type === "corporate" || user.type === "mastercorporate" ? user.corporateInformation?.payment?.currency : "EUR",
|
||||
);
|
||||
const [monthlyDuration, setMonthlyDuration] = useState(
|
||||
user.type === "corporate" || user.type === "mastercorporate" ? user.corporateInformation?.monthlyDuration : undefined,
|
||||
);
|
||||
const [commissionValue, setCommission] = useState(
|
||||
user.type === "corporate" || user.type === "mastercorporate" ? user.corporateInformation?.payment?.commission : undefined,
|
||||
);
|
||||
const {stats} = useStats(user.id);
|
||||
const {users} = useUsers();
|
||||
const {codes} = useCodes(user.id);
|
||||
@@ -115,7 +140,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
||||
}, [users, referralAgent]);
|
||||
|
||||
const updateUser = () => {
|
||||
if (user.type === "corporate" && (!paymentValue || paymentValue < 0))
|
||||
if (user.type === "corporate" || (user.type === "mastercorporate" && (!paymentValue || paymentValue < 0)))
|
||||
return toast.error("Please set a price for the user's package before updating!");
|
||||
if (!confirm(`Are you sure you want to update ${user.name}'s account?`)) return;
|
||||
|
||||
@@ -179,7 +204,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
||||
];
|
||||
|
||||
const corporateProfileItems =
|
||||
user.type === "corporate"
|
||||
user.type === "corporate" || user.type === "mastercorporate"
|
||||
? [
|
||||
{
|
||||
icon: <BsPerson className="w-6 h-6 md:w-8 md:h-8 text-mti-red-light" />,
|
||||
@@ -200,7 +225,10 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<ProfileSummary user={user} items={user.type === "corporate" ? corporateProfileItems : generalProfileItems} />
|
||||
<ProfileSummary
|
||||
user={user}
|
||||
items={user.type === "corporate" || user.type === "mastercorporate" ? corporateProfileItems : generalProfileItems}
|
||||
/>
|
||||
|
||||
{user.type === "agent" && (
|
||||
<>
|
||||
@@ -239,7 +267,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
||||
<Divider className="w-full !m-0" />
|
||||
</>
|
||||
)}
|
||||
{user.type === "corporate" && (
|
||||
{(user.type === "corporate" || user.type === "mastercorporate") && (
|
||||
<>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 w-full">
|
||||
<Input
|
||||
@@ -255,10 +283,17 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
||||
label="Number of Users"
|
||||
type="number"
|
||||
name="userAmount"
|
||||
max={maxUserAmount}
|
||||
onChange={(e) => setUserAmount(e ? parseInt(e) : undefined)}
|
||||
placeholder="Enter number of users"
|
||||
defaultValue={userAmount}
|
||||
disabled={disabled || checkAccess(loggedInUser, getTypesOfUser(["developer", "admin"]))}
|
||||
disabled={
|
||||
disabled ||
|
||||
checkAccess(
|
||||
loggedInUser,
|
||||
getTypesOfUser(["developer", "admin", ...((user.type === "corporate" ? ["mastercorporate"] : []) as Type[])]),
|
||||
)
|
||||
}
|
||||
/>
|
||||
<Input
|
||||
label="Monthly Duration"
|
||||
@@ -468,7 +503,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
||||
</RadioGroup>
|
||||
</div>
|
||||
)}
|
||||
{user.type === "corporate" && (
|
||||
{(user.type === "corporate" || user.type === "mastercorporate") && (
|
||||
<Input
|
||||
name="position"
|
||||
onChange={setPosition}
|
||||
|
||||
@@ -49,6 +49,7 @@ import {createColumnHelper} from "@tanstack/react-table";
|
||||
import Checkbox from "@/components/Low/Checkbox";
|
||||
import List from "@/components/List";
|
||||
import {getUserCompanyName} from "@/resources/user";
|
||||
import useUserBalance from "@/hooks/useUserBalance";
|
||||
|
||||
interface Props {
|
||||
user: CorporateUser;
|
||||
@@ -159,13 +160,13 @@ export default function CorporateDashboard({user}: Props) {
|
||||
const [corporateUserToShow, setCorporateUserToShow] = useState<CorporateUser>();
|
||||
const [selectedAssignment, setSelectedAssignment] = useState<Assignment>();
|
||||
const [isCreatingAssignment, setIsCreatingAssignment] = useState(false);
|
||||
const [userBalance, setUserBalance] = useState(0);
|
||||
|
||||
const {stats} = useStats();
|
||||
const {users, reload, isLoading} = useUsers();
|
||||
const {codes} = useCodes(user.id);
|
||||
const {groups} = useGroups({admin: user.id});
|
||||
const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({corporate: user.id});
|
||||
const {balance} = useUserBalance();
|
||||
|
||||
const appendUserFilters = useFilterStore((state) => state.appendUserFilter);
|
||||
const router = useRouter();
|
||||
@@ -174,14 +175,6 @@ export default function CorporateDashboard({user}: Props) {
|
||||
setShowModal(!!selectedUser && page === "");
|
||||
}, [selectedUser, page]);
|
||||
|
||||
useEffect(() => {
|
||||
const relatedGroups = groups.filter((x) => x.name === "Students" || x.name === "Teachers" || x.name === "Corporate");
|
||||
const usersInGroups = relatedGroups.map((x) => x.participants).flat();
|
||||
const filteredCodes = codes.filter((x) => !x.userId || !usersInGroups.includes(x.userId));
|
||||
|
||||
setUserBalance(usersInGroups.length + filteredCodes.length);
|
||||
}, [codes, groups]);
|
||||
|
||||
useEffect(() => {
|
||||
// in this case it fetches the master corporate account
|
||||
getUserCorporate(user.id).then(setCorporateUserToShow);
|
||||
@@ -496,7 +489,7 @@ export default function CorporateDashboard({user}: Props) {
|
||||
<IconCard
|
||||
Icon={BsPersonCheck}
|
||||
label="User Balance"
|
||||
value={`${userBalance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
||||
value={`${balance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
||||
color="purple"
|
||||
/>
|
||||
<IconCard
|
||||
|
||||
@@ -52,6 +52,7 @@ import Select from "@/components/Low/Select";
|
||||
import {Menu, MenuButton, MenuItem, MenuItems} from "@headlessui/react";
|
||||
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
||||
import MasterStatistical from "./MasterStatistical";
|
||||
import useUserBalance from "@/hooks/useUserBalance";
|
||||
|
||||
interface Props {
|
||||
user: MasterCorporateUser;
|
||||
@@ -306,8 +307,8 @@ export default function MasterCorporateDashboard({user}: Props) {
|
||||
|
||||
const {stats} = useStats();
|
||||
const {users, reload} = useUsers();
|
||||
const {codes} = useCodes(user.id);
|
||||
const {groups} = useGroups({admin: user.id, userType: user.type});
|
||||
const {balance} = useUserBalance();
|
||||
|
||||
const masterCorporateUserGroups = [...new Set(groups.filter((u) => u.admin === user.id).flatMap((g) => g.participants))];
|
||||
|
||||
@@ -671,7 +672,7 @@ export default function MasterCorporateDashboard({user}: Props) {
|
||||
<IconCard
|
||||
Icon={BsPersonCheck}
|
||||
label="User Balance"
|
||||
value={`${codes.length}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
||||
value={`${balance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
||||
color="purple"
|
||||
/>
|
||||
<IconCard
|
||||
@@ -774,6 +775,11 @@ export default function MasterCorporateDashboard({user}: Props) {
|
||||
{selectedUser && (
|
||||
<div className="w-full flex flex-col gap-8">
|
||||
<UserCard
|
||||
maxUserAmount={
|
||||
user.type === "mastercorporate"
|
||||
? (user.corporateInformation?.companyInformation?.userAmount || 0) - balance
|
||||
: undefined
|
||||
}
|
||||
loggedInUser={user}
|
||||
onClose={(shouldReload) => {
|
||||
setSelectedUser(undefined);
|
||||
|
||||
21
src/hooks/useUserBalance.tsx
Normal file
21
src/hooks/useUserBalance.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
import {Code, Group, User} from "@/interfaces/user";
|
||||
import axios from "axios";
|
||||
import {useEffect, useState} from "react";
|
||||
|
||||
export default function useUserBalance() {
|
||||
const [balance, setBalance] = useState<number>(0);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isError, setIsError] = useState(false);
|
||||
|
||||
const getData = () => {
|
||||
setIsLoading(true);
|
||||
axios
|
||||
.get<{balance: number}>(`/api/users/balance`)
|
||||
.then((response) => setBalance(response.data.balance))
|
||||
.finally(() => setIsLoading(false));
|
||||
};
|
||||
|
||||
useEffect(getData, []);
|
||||
|
||||
return {balance, isLoading, isError, reload: getData};
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import {exportListToExcel, UserListRow} from "@/utils/users";
|
||||
import {checkAccess} from "@/utils/permissions";
|
||||
import {PermissionType} from "@/interfaces/permissions";
|
||||
import usePermissions from "@/hooks/usePermissions";
|
||||
import useUserBalance from "@/hooks/useUserBalance";
|
||||
const columnHelper = createColumnHelper<User>();
|
||||
const searchFields = [["name"], ["email"], ["corporateInformation", "companyInformation", "name"]];
|
||||
|
||||
@@ -58,6 +59,7 @@ export default function UserList({
|
||||
|
||||
const {users, reload} = useUsers();
|
||||
const {permissions} = usePermissions(user?.id || "");
|
||||
const {balance} = useUserBalance();
|
||||
const {groups} = useGroups({
|
||||
admin: user && ["corporate", "teacher", "mastercorporate"].includes(user?.type) ? user.id : undefined,
|
||||
userType: user?.type,
|
||||
@@ -551,6 +553,9 @@ export default function UserList({
|
||||
return (
|
||||
<div className="w-full flex flex-col gap-8">
|
||||
<UserCard
|
||||
maxUserAmount={
|
||||
user.type === "mastercorporate" ? (user.corporateInformation?.companyInformation?.userAmount || 0) - balance : undefined
|
||||
}
|
||||
loggedInUser={user}
|
||||
onViewStudents={
|
||||
(selectedUser.type === "corporate" || selectedUser.type === "teacher") && studentsFromAdmin.length > 0
|
||||
|
||||
17
src/pages/api/users/balance.ts
Normal file
17
src/pages/api/users/balance.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type {NextApiRequest, NextApiResponse} from "next";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {getUserBalance} from "@/utils/users.be";
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ok: false});
|
||||
return;
|
||||
}
|
||||
|
||||
const balance = await getUserBalance(req.session.user);
|
||||
res.status(200).json({balance});
|
||||
}
|
||||
10
src/utils/codes.be.ts
Normal file
10
src/utils/codes.be.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import {app} from "@/firebase";
|
||||
import {Code} from "@/interfaces/user";
|
||||
import {collection, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export const getUserCodes = async (id: string): Promise<Code[]> => {
|
||||
const codeDocs = await getDocs(query(collection(db, "codes"), where("creator", "==", id)));
|
||||
return codeDocs.docs.map((x) => ({...(x.data() as Code), id})) as Code[];
|
||||
};
|
||||
@@ -1,29 +1,12 @@
|
||||
import { app } from "@/firebase";
|
||||
import {
|
||||
CorporateUser,
|
||||
Group,
|
||||
StudentUser,
|
||||
TeacherUser,
|
||||
} from "@/interfaces/user";
|
||||
import {
|
||||
collection,
|
||||
doc,
|
||||
getDoc,
|
||||
getDocs,
|
||||
getFirestore,
|
||||
query,
|
||||
setDoc,
|
||||
where,
|
||||
} from "firebase/firestore";
|
||||
import {app} from "@/firebase";
|
||||
import {CorporateUser, Group, StudentUser, TeacherUser} from "@/interfaces/user";
|
||||
import {collection, doc, getDoc, getDocs, getFirestore, query, setDoc, where} from "firebase/firestore";
|
||||
import moment from "moment";
|
||||
import { getUser } from "./users.be";
|
||||
import { getSpecificUsers } from "./users.be";
|
||||
import {getUser} from "./users.be";
|
||||
import {getSpecificUsers} from "./users.be";
|
||||
const db = getFirestore(app);
|
||||
|
||||
export const updateExpiryDateOnGroup = async (
|
||||
participantID: string,
|
||||
corporateID: string
|
||||
) => {
|
||||
export const updateExpiryDateOnGroup = async (participantID: string, corporateID: string) => {
|
||||
const corporateRef = await getDoc(doc(db, "users", corporateID));
|
||||
const participantRef = await getDoc(doc(db, "users", participantID));
|
||||
|
||||
@@ -33,87 +16,52 @@ export const updateExpiryDateOnGroup = async (
|
||||
...corporateRef.data(),
|
||||
id: corporateRef.id,
|
||||
} as CorporateUser;
|
||||
const participant = { ...participantRef.data(), id: participantRef.id } as
|
||||
| StudentUser
|
||||
| TeacherUser;
|
||||
const participant = {...participantRef.data(), id: participantRef.id} as StudentUser | TeacherUser;
|
||||
|
||||
if (
|
||||
corporate.type !== "corporate" ||
|
||||
(participant.type !== "student" && participant.type !== "teacher")
|
||||
)
|
||||
return;
|
||||
if (corporate.type !== "corporate" || (participant.type !== "student" && participant.type !== "teacher")) return;
|
||||
|
||||
if (
|
||||
!corporate.subscriptionExpirationDate ||
|
||||
!participant.subscriptionExpirationDate
|
||||
) {
|
||||
return await setDoc(
|
||||
doc(db, "users", participant.id),
|
||||
{ subscriptionExpirationDate: null },
|
||||
{ merge: true }
|
||||
);
|
||||
if (!corporate.subscriptionExpirationDate || !participant.subscriptionExpirationDate) {
|
||||
return await setDoc(doc(db, "users", participant.id), {subscriptionExpirationDate: null}, {merge: true});
|
||||
}
|
||||
|
||||
const corporateDate = moment(corporate.subscriptionExpirationDate);
|
||||
const participantDate = moment(participant.subscriptionExpirationDate);
|
||||
|
||||
if (corporateDate.isAfter(participantDate))
|
||||
return await setDoc(
|
||||
doc(db, "users", participant.id),
|
||||
{ subscriptionExpirationDate: corporateDate.toISOString() },
|
||||
{ merge: true }
|
||||
);
|
||||
return await setDoc(doc(db, "users", participant.id), {subscriptionExpirationDate: corporateDate.toISOString()}, {merge: true});
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
export const getGroups = async () => {
|
||||
const groupDocs = await getDocs(collection(db, "groups"));
|
||||
return groupDocs.docs.map((x) => ({ ...x.data(), id: x.id })) as Group[];
|
||||
return groupDocs.docs.map((x) => ({...x.data(), id: x.id})) as Group[];
|
||||
};
|
||||
|
||||
export const getUserGroups = async (id: string): Promise<Group[]> => {
|
||||
const groupDocs = await getDocs(
|
||||
query(collection(db, "groups"), where("admin", "==", id))
|
||||
);
|
||||
return groupDocs.docs.map((x) => ({ ...x.data(), id })) as Group[];
|
||||
const groupDocs = await getDocs(query(collection(db, "groups"), where("admin", "==", id)));
|
||||
return groupDocs.docs.map((x) => ({...x.data(), id})) as Group[];
|
||||
};
|
||||
|
||||
export const getAllAssignersByCorporate = async (
|
||||
corporateID: string
|
||||
): Promise<string[]> => {
|
||||
export const getAllAssignersByCorporate = async (corporateID: string): Promise<string[]> => {
|
||||
const groups = await getUserGroups(corporateID);
|
||||
const groupUsers = (
|
||||
await Promise.all(
|
||||
groups.map(async (g) => await Promise.all(g.participants.map(getUser)))
|
||||
)
|
||||
).flat();
|
||||
const groupUsers = (await Promise.all(groups.map(async (g) => await Promise.all(g.participants.map(getUser))))).flat();
|
||||
const teacherPromises = await Promise.all(
|
||||
groupUsers.map(async (u) =>
|
||||
u.type === "teacher"
|
||||
? u.id
|
||||
: u.type === "corporate"
|
||||
? [...(await getAllAssignersByCorporate(u.id)), u.id]
|
||||
: undefined
|
||||
)
|
||||
u.type === "teacher" ? u.id : u.type === "corporate" ? [...(await getAllAssignersByCorporate(u.id)), u.id] : undefined,
|
||||
),
|
||||
);
|
||||
|
||||
return teacherPromises.filter((x) => !!x).flat() as string[];
|
||||
};
|
||||
|
||||
export const getGroupsForUser = async (admin: string, participant: string) => {
|
||||
export const getGroupsForUser = async (admin: string, participant?: string) => {
|
||||
try {
|
||||
const queryConstraints = [
|
||||
...(admin ? [where("admin", "==", admin)] : []),
|
||||
...(participant
|
||||
? [where("participants", "array-contains", participant)]
|
||||
: []),
|
||||
...(participant ? [where("participants", "array-contains", participant)] : []),
|
||||
];
|
||||
const snapshot = await getDocs(
|
||||
queryConstraints.length > 0
|
||||
? query(collection(db, "groups"), ...queryConstraints)
|
||||
: collection(db, "groups")
|
||||
);
|
||||
const snapshot = await getDocs(queryConstraints.length > 0 ? query(collection(db, "groups"), ...queryConstraints) : collection(db, "groups"));
|
||||
const groups = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
@@ -126,23 +74,14 @@ export const getGroupsForUser = async (admin: string, participant: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getStudentGroupsForUsersWithoutAdmin = async (
|
||||
admin: string,
|
||||
participants: string[]
|
||||
) => {
|
||||
export const getStudentGroupsForUsersWithoutAdmin = async (admin: string, participants: string[]) => {
|
||||
try {
|
||||
const queryConstraints = [
|
||||
...(admin ? [where("admin", "!=", admin)] : []),
|
||||
...(participants
|
||||
? [where("participants", "array-contains-any", participants)]
|
||||
: []),
|
||||
...(participants ? [where("participants", "array-contains-any", participants)] : []),
|
||||
where("name", "==", "Students"),
|
||||
];
|
||||
const snapshot = await getDocs(
|
||||
queryConstraints.length > 0
|
||||
? query(collection(db, "groups"), ...queryConstraints)
|
||||
: collection(db, "groups")
|
||||
);
|
||||
const snapshot = await getDocs(queryConstraints.length > 0 ? query(collection(db, "groups"), ...queryConstraints) : collection(db, "groups"));
|
||||
const groups = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
@@ -157,17 +96,17 @@ export const getStudentGroupsForUsersWithoutAdmin = async (
|
||||
|
||||
export const getCorporateNameForStudent = async (studentID: string) => {
|
||||
const groups = await getStudentGroupsForUsersWithoutAdmin("", [studentID]);
|
||||
if (groups.length === 0) return '';
|
||||
if (groups.length === 0) return "";
|
||||
|
||||
const adminUserIds = [...new Set(groups.map((g) => g.admin))];
|
||||
const adminUsersData = await getSpecificUsers(adminUserIds);
|
||||
|
||||
if(adminUsersData.length === 0) return '';
|
||||
const admins = adminUsersData.filter((x) => x.type === 'corporate');
|
||||
if (adminUsersData.length === 0) return "";
|
||||
const admins = adminUsersData.filter((x) => x.type === "corporate");
|
||||
|
||||
if(admins.length > 0) {
|
||||
if (admins.length > 0) {
|
||||
return (admins[0] as CorporateUser).corporateInformation.companyInformation.name;
|
||||
}
|
||||
|
||||
return '';
|
||||
return "";
|
||||
};
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
import { app } from "@/firebase";
|
||||
import {app} from "@/firebase";
|
||||
|
||||
import {
|
||||
collection,
|
||||
doc,
|
||||
getDoc,
|
||||
getDocs,
|
||||
getFirestore,
|
||||
query,
|
||||
where,
|
||||
} from "firebase/firestore";
|
||||
import { User } from "@/interfaces/user";
|
||||
import {collection, doc, getDoc, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||
import {CorporateUser, Group, User} from "@/interfaces/user";
|
||||
import {getGroupsForUser} from "./groups.be";
|
||||
import {uniq, uniqBy} from "lodash";
|
||||
import {getUserCodes} from "./codes.be";
|
||||
const db = getFirestore(app);
|
||||
|
||||
export async function getUsers() {
|
||||
@@ -24,15 +19,13 @@ export async function getUsers() {
|
||||
export async function getUser(id: string) {
|
||||
const userDoc = await getDoc(doc(db, "users", id));
|
||||
|
||||
return { ...userDoc.data(), id } as User;
|
||||
return {...userDoc.data(), id} as User;
|
||||
}
|
||||
|
||||
export async function getSpecificUsers(ids: string[]) {
|
||||
if (ids.length === 0) return [];
|
||||
|
||||
const snapshot = await getDocs(
|
||||
query(collection(db, "users"), where("id", "in", ids))
|
||||
);
|
||||
const snapshot = await getDocs(query(collection(db, "users"), where("id", "in", ids)));
|
||||
|
||||
const groups = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
@@ -41,3 +34,22 @@ export async function getSpecificUsers(ids: string[]) {
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
export async function getUserBalance(user: User) {
|
||||
const codes = await getUserCodes(user.id);
|
||||
if (user.type !== "corporate" && user.type !== "mastercorporate") return codes.length;
|
||||
|
||||
const groups = await getGroupsForUser(user.id);
|
||||
const participants = uniq(groups.flatMap((x) => x.participants));
|
||||
|
||||
if (user.type === "corporate") return participants.length + codes.length;
|
||||
|
||||
const participantUsers = await Promise.all(participants.map(getUser));
|
||||
const corporateUsers = participantUsers.filter((x) => x.type === "corporate") as CorporateUser[];
|
||||
|
||||
return (
|
||||
corporateUsers.reduce((acc, curr) => acc + curr.corporateInformation?.companyInformation?.userAmount || 0, 0) +
|
||||
corporateUsers.length +
|
||||
codes.length
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user