ENCOA-96: License Distribuition system from Master Corporate to Corporate
This commit is contained in:
@@ -11,6 +11,7 @@ interface Props {
|
|||||||
value?: string | number;
|
value?: string | number;
|
||||||
className?: string;
|
className?: string;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
|
max?: number;
|
||||||
name: string;
|
name: string;
|
||||||
onChange: (value: string) => void;
|
onChange: (value: string) => void;
|
||||||
}
|
}
|
||||||
@@ -23,6 +24,7 @@ export default function Input({
|
|||||||
required = false,
|
required = false,
|
||||||
value,
|
value,
|
||||||
defaultValue,
|
defaultValue,
|
||||||
|
max,
|
||||||
className,
|
className,
|
||||||
roundness = "full",
|
roundness = "full",
|
||||||
disabled = false,
|
disabled = false,
|
||||||
@@ -72,6 +74,7 @@ export default function Input({
|
|||||||
name={name}
|
name={name}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
value={value}
|
value={value}
|
||||||
|
max={max}
|
||||||
onChange={(e) => onChange(e.target.value)}
|
onChange={(e) => onChange(e.target.value)}
|
||||||
min={type === "number" ? 0 : undefined}
|
min={type === "number" ? 0 : undefined}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ interface Props {
|
|||||||
onViewStudents?: () => void;
|
onViewStudents?: () => void;
|
||||||
onViewTeachers?: () => void;
|
onViewTeachers?: () => void;
|
||||||
onViewCorporate?: () => void;
|
onViewCorporate?: () => void;
|
||||||
|
maxUserAmount?: number;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
disabledFields?: {
|
disabledFields?: {
|
||||||
countryManager?: boolean;
|
countryManager?: boolean;
|
||||||
@@ -72,17 +73,31 @@ const CURRENCIES_OPTIONS = CURRENCIES.map(({label, currency}) => ({
|
|||||||
label,
|
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 [expiryDate, setExpiryDate] = useState<Date | null | undefined>(user.subscriptionExpirationDate);
|
||||||
const [type, setType] = useState(user.type);
|
const [type, setType] = useState(user.type);
|
||||||
const [status, setStatus] = useState(user.status);
|
const [status, setStatus] = useState(user.status);
|
||||||
const [referralAgentLabel, setReferralAgentLabel] = useState<string>();
|
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 [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(
|
const [companyName, setCompanyName] = useState(
|
||||||
user.type === "corporate"
|
user.type === "corporate" || user.type === "mastercorporate"
|
||||||
? user.corporateInformation?.companyInformation.name
|
? user.corporateInformation?.companyInformation.name
|
||||||
: user.type === "agent"
|
: user.type === "agent"
|
||||||
? user.agentInformation?.companyName
|
? user.agentInformation?.companyName
|
||||||
@@ -92,11 +107,21 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
const [commercialRegistration, setCommercialRegistration] = useState(
|
const [commercialRegistration, setCommercialRegistration] = useState(
|
||||||
user.type === "agent" ? user.agentInformation?.commercialRegistration : undefined,
|
user.type === "agent" ? user.agentInformation?.commercialRegistration : undefined,
|
||||||
);
|
);
|
||||||
const [userAmount, setUserAmount] = useState(user.type === "corporate" ? user.corporateInformation?.companyInformation.userAmount : undefined);
|
const [userAmount, setUserAmount] = useState(
|
||||||
const [paymentValue, setPaymentValue] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.value : undefined);
|
user.type === "corporate" || user.type === "mastercorporate" ? user.corporateInformation?.companyInformation.userAmount : 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 [paymentValue, setPaymentValue] = useState(
|
||||||
const [commissionValue, setCommission] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.commission : undefined);
|
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 {stats} = useStats(user.id);
|
||||||
const {users} = useUsers();
|
const {users} = useUsers();
|
||||||
const {codes} = useCodes(user.id);
|
const {codes} = useCodes(user.id);
|
||||||
@@ -115,7 +140,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
}, [users, referralAgent]);
|
}, [users, referralAgent]);
|
||||||
|
|
||||||
const updateUser = () => {
|
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!");
|
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;
|
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 =
|
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" />,
|
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 (
|
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" && (
|
{user.type === "agent" && (
|
||||||
<>
|
<>
|
||||||
@@ -239,7 +267,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
<Divider className="w-full !m-0" />
|
<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">
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 w-full">
|
||||||
<Input
|
<Input
|
||||||
@@ -255,10 +283,17 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
label="Number of Users"
|
label="Number of Users"
|
||||||
type="number"
|
type="number"
|
||||||
name="userAmount"
|
name="userAmount"
|
||||||
|
max={maxUserAmount}
|
||||||
onChange={(e) => setUserAmount(e ? parseInt(e) : undefined)}
|
onChange={(e) => setUserAmount(e ? parseInt(e) : undefined)}
|
||||||
placeholder="Enter number of users"
|
placeholder="Enter number of users"
|
||||||
defaultValue={userAmount}
|
defaultValue={userAmount}
|
||||||
disabled={disabled || checkAccess(loggedInUser, getTypesOfUser(["developer", "admin"]))}
|
disabled={
|
||||||
|
disabled ||
|
||||||
|
checkAccess(
|
||||||
|
loggedInUser,
|
||||||
|
getTypesOfUser(["developer", "admin", ...((user.type === "corporate" ? ["mastercorporate"] : []) as Type[])]),
|
||||||
|
)
|
||||||
|
}
|
||||||
/>
|
/>
|
||||||
<Input
|
<Input
|
||||||
label="Monthly Duration"
|
label="Monthly Duration"
|
||||||
@@ -468,7 +503,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
</RadioGroup>
|
</RadioGroup>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{user.type === "corporate" && (
|
{(user.type === "corporate" || user.type === "mastercorporate") && (
|
||||||
<Input
|
<Input
|
||||||
name="position"
|
name="position"
|
||||||
onChange={setPosition}
|
onChange={setPosition}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import {createColumnHelper} from "@tanstack/react-table";
|
|||||||
import Checkbox from "@/components/Low/Checkbox";
|
import Checkbox from "@/components/Low/Checkbox";
|
||||||
import List from "@/components/List";
|
import List from "@/components/List";
|
||||||
import {getUserCompanyName} from "@/resources/user";
|
import {getUserCompanyName} from "@/resources/user";
|
||||||
|
import useUserBalance from "@/hooks/useUserBalance";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
user: CorporateUser;
|
user: CorporateUser;
|
||||||
@@ -159,13 +160,13 @@ export default function CorporateDashboard({user}: Props) {
|
|||||||
const [corporateUserToShow, setCorporateUserToShow] = useState<CorporateUser>();
|
const [corporateUserToShow, setCorporateUserToShow] = useState<CorporateUser>();
|
||||||
const [selectedAssignment, setSelectedAssignment] = useState<Assignment>();
|
const [selectedAssignment, setSelectedAssignment] = useState<Assignment>();
|
||||||
const [isCreatingAssignment, setIsCreatingAssignment] = useState(false);
|
const [isCreatingAssignment, setIsCreatingAssignment] = useState(false);
|
||||||
const [userBalance, setUserBalance] = useState(0);
|
|
||||||
|
|
||||||
const {stats} = useStats();
|
const {stats} = useStats();
|
||||||
const {users, reload, isLoading} = useUsers();
|
const {users, reload, isLoading} = useUsers();
|
||||||
const {codes} = useCodes(user.id);
|
const {codes} = useCodes(user.id);
|
||||||
const {groups} = useGroups({admin: user.id});
|
const {groups} = useGroups({admin: user.id});
|
||||||
const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({corporate: user.id});
|
const {assignments, isLoading: isAssignmentsLoading, reload: reloadAssignments} = useAssignments({corporate: user.id});
|
||||||
|
const {balance} = useUserBalance();
|
||||||
|
|
||||||
const appendUserFilters = useFilterStore((state) => state.appendUserFilter);
|
const appendUserFilters = useFilterStore((state) => state.appendUserFilter);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -174,14 +175,6 @@ export default function CorporateDashboard({user}: Props) {
|
|||||||
setShowModal(!!selectedUser && page === "");
|
setShowModal(!!selectedUser && page === "");
|
||||||
}, [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(() => {
|
useEffect(() => {
|
||||||
// in this case it fetches the master corporate account
|
// in this case it fetches the master corporate account
|
||||||
getUserCorporate(user.id).then(setCorporateUserToShow);
|
getUserCorporate(user.id).then(setCorporateUserToShow);
|
||||||
@@ -496,7 +489,7 @@ export default function CorporateDashboard({user}: Props) {
|
|||||||
<IconCard
|
<IconCard
|
||||||
Icon={BsPersonCheck}
|
Icon={BsPersonCheck}
|
||||||
label="User Balance"
|
label="User Balance"
|
||||||
value={`${userBalance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
value={`${balance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ import Select from "@/components/Low/Select";
|
|||||||
import {Menu, MenuButton, MenuItem, MenuItems} from "@headlessui/react";
|
import {Menu, MenuButton, MenuItem, MenuItems} from "@headlessui/react";
|
||||||
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
import {Popover, PopoverContent, PopoverTrigger} from "@/components/ui/popover";
|
||||||
import MasterStatistical from "./MasterStatistical";
|
import MasterStatistical from "./MasterStatistical";
|
||||||
|
import useUserBalance from "@/hooks/useUserBalance";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
user: MasterCorporateUser;
|
user: MasterCorporateUser;
|
||||||
@@ -306,8 +307,8 @@ export default function MasterCorporateDashboard({user}: Props) {
|
|||||||
|
|
||||||
const {stats} = useStats();
|
const {stats} = useStats();
|
||||||
const {users, reload} = useUsers();
|
const {users, reload} = useUsers();
|
||||||
const {codes} = useCodes(user.id);
|
|
||||||
const {groups} = useGroups({admin: user.id, userType: user.type});
|
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))];
|
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
|
<IconCard
|
||||||
Icon={BsPersonCheck}
|
Icon={BsPersonCheck}
|
||||||
label="User Balance"
|
label="User Balance"
|
||||||
value={`${codes.length}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
value={`${balance}/${user.corporateInformation?.companyInformation?.userAmount || 0}`}
|
||||||
color="purple"
|
color="purple"
|
||||||
/>
|
/>
|
||||||
<IconCard
|
<IconCard
|
||||||
@@ -774,6 +775,11 @@ export default function MasterCorporateDashboard({user}: Props) {
|
|||||||
{selectedUser && (
|
{selectedUser && (
|
||||||
<div className="w-full flex flex-col gap-8">
|
<div className="w-full flex flex-col gap-8">
|
||||||
<UserCard
|
<UserCard
|
||||||
|
maxUserAmount={
|
||||||
|
user.type === "mastercorporate"
|
||||||
|
? (user.corporateInformation?.companyInformation?.userAmount || 0) - balance
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
loggedInUser={user}
|
loggedInUser={user}
|
||||||
onClose={(shouldReload) => {
|
onClose={(shouldReload) => {
|
||||||
setSelectedUser(undefined);
|
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 {checkAccess} from "@/utils/permissions";
|
||||||
import {PermissionType} from "@/interfaces/permissions";
|
import {PermissionType} from "@/interfaces/permissions";
|
||||||
import usePermissions from "@/hooks/usePermissions";
|
import usePermissions from "@/hooks/usePermissions";
|
||||||
|
import useUserBalance from "@/hooks/useUserBalance";
|
||||||
const columnHelper = createColumnHelper<User>();
|
const columnHelper = createColumnHelper<User>();
|
||||||
const searchFields = [["name"], ["email"], ["corporateInformation", "companyInformation", "name"]];
|
const searchFields = [["name"], ["email"], ["corporateInformation", "companyInformation", "name"]];
|
||||||
|
|
||||||
@@ -58,6 +59,7 @@ export default function UserList({
|
|||||||
|
|
||||||
const {users, reload} = useUsers();
|
const {users, reload} = useUsers();
|
||||||
const {permissions} = usePermissions(user?.id || "");
|
const {permissions} = usePermissions(user?.id || "");
|
||||||
|
const {balance} = useUserBalance();
|
||||||
const {groups} = useGroups({
|
const {groups} = useGroups({
|
||||||
admin: user && ["corporate", "teacher", "mastercorporate"].includes(user?.type) ? user.id : undefined,
|
admin: user && ["corporate", "teacher", "mastercorporate"].includes(user?.type) ? user.id : undefined,
|
||||||
userType: user?.type,
|
userType: user?.type,
|
||||||
@@ -551,6 +553,9 @@ export default function UserList({
|
|||||||
return (
|
return (
|
||||||
<div className="w-full flex flex-col gap-8">
|
<div className="w-full flex flex-col gap-8">
|
||||||
<UserCard
|
<UserCard
|
||||||
|
maxUserAmount={
|
||||||
|
user.type === "mastercorporate" ? (user.corporateInformation?.companyInformation?.userAmount || 0) - balance : undefined
|
||||||
|
}
|
||||||
loggedInUser={user}
|
loggedInUser={user}
|
||||||
onViewStudents={
|
onViewStudents={
|
||||||
(selectedUser.type === "corporate" || selectedUser.type === "teacher") && studentsFromAdmin.length > 0
|
(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 {app} from "@/firebase";
|
||||||
import {
|
import {CorporateUser, Group, StudentUser, TeacherUser} from "@/interfaces/user";
|
||||||
CorporateUser,
|
import {collection, doc, getDoc, getDocs, getFirestore, query, setDoc, where} from "firebase/firestore";
|
||||||
Group,
|
|
||||||
StudentUser,
|
|
||||||
TeacherUser,
|
|
||||||
} from "@/interfaces/user";
|
|
||||||
import {
|
|
||||||
collection,
|
|
||||||
doc,
|
|
||||||
getDoc,
|
|
||||||
getDocs,
|
|
||||||
getFirestore,
|
|
||||||
query,
|
|
||||||
setDoc,
|
|
||||||
where,
|
|
||||||
} from "firebase/firestore";
|
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import { getUser } from "./users.be";
|
import {getUser} from "./users.be";
|
||||||
import { getSpecificUsers } from "./users.be";
|
import {getSpecificUsers} from "./users.be";
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
export const updateExpiryDateOnGroup = async (
|
export const updateExpiryDateOnGroup = async (participantID: string, corporateID: string) => {
|
||||||
participantID: string,
|
|
||||||
corporateID: string
|
|
||||||
) => {
|
|
||||||
const corporateRef = await getDoc(doc(db, "users", corporateID));
|
const corporateRef = await getDoc(doc(db, "users", corporateID));
|
||||||
const participantRef = await getDoc(doc(db, "users", participantID));
|
const participantRef = await getDoc(doc(db, "users", participantID));
|
||||||
|
|
||||||
@@ -33,87 +16,52 @@ export const updateExpiryDateOnGroup = async (
|
|||||||
...corporateRef.data(),
|
...corporateRef.data(),
|
||||||
id: corporateRef.id,
|
id: corporateRef.id,
|
||||||
} as CorporateUser;
|
} as CorporateUser;
|
||||||
const participant = { ...participantRef.data(), id: participantRef.id } as
|
const participant = {...participantRef.data(), id: participantRef.id} as StudentUser | TeacherUser;
|
||||||
| StudentUser
|
|
||||||
| TeacherUser;
|
|
||||||
|
|
||||||
if (
|
if (corporate.type !== "corporate" || (participant.type !== "student" && participant.type !== "teacher")) return;
|
||||||
corporate.type !== "corporate" ||
|
|
||||||
(participant.type !== "student" && participant.type !== "teacher")
|
|
||||||
)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (
|
if (!corporate.subscriptionExpirationDate || !participant.subscriptionExpirationDate) {
|
||||||
!corporate.subscriptionExpirationDate ||
|
return await setDoc(doc(db, "users", participant.id), {subscriptionExpirationDate: null}, {merge: true});
|
||||||
!participant.subscriptionExpirationDate
|
|
||||||
) {
|
|
||||||
return await setDoc(
|
|
||||||
doc(db, "users", participant.id),
|
|
||||||
{ subscriptionExpirationDate: null },
|
|
||||||
{ merge: true }
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const corporateDate = moment(corporate.subscriptionExpirationDate);
|
const corporateDate = moment(corporate.subscriptionExpirationDate);
|
||||||
const participantDate = moment(participant.subscriptionExpirationDate);
|
const participantDate = moment(participant.subscriptionExpirationDate);
|
||||||
|
|
||||||
if (corporateDate.isAfter(participantDate))
|
if (corporateDate.isAfter(participantDate))
|
||||||
return await setDoc(
|
return await setDoc(doc(db, "users", participant.id), {subscriptionExpirationDate: corporateDate.toISOString()}, {merge: true});
|
||||||
doc(db, "users", participant.id),
|
|
||||||
{ subscriptionExpirationDate: corporateDate.toISOString() },
|
|
||||||
{ merge: true }
|
|
||||||
);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getGroups = async () => {
|
export const getGroups = async () => {
|
||||||
const groupDocs = await getDocs(collection(db, "groups"));
|
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[]> => {
|
export const getUserGroups = async (id: string): Promise<Group[]> => {
|
||||||
const groupDocs = await getDocs(
|
const groupDocs = await getDocs(query(collection(db, "groups"), where("admin", "==", id)));
|
||||||
query(collection(db, "groups"), where("admin", "==", id))
|
return groupDocs.docs.map((x) => ({...x.data(), id})) as Group[];
|
||||||
);
|
|
||||||
return groupDocs.docs.map((x) => ({ ...x.data(), id })) as Group[];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAllAssignersByCorporate = async (
|
export const getAllAssignersByCorporate = async (corporateID: string): Promise<string[]> => {
|
||||||
corporateID: string
|
|
||||||
): Promise<string[]> => {
|
|
||||||
const groups = await getUserGroups(corporateID);
|
const groups = await getUserGroups(corporateID);
|
||||||
const groupUsers = (
|
const groupUsers = (await Promise.all(groups.map(async (g) => await Promise.all(g.participants.map(getUser))))).flat();
|
||||||
await Promise.all(
|
|
||||||
groups.map(async (g) => await Promise.all(g.participants.map(getUser)))
|
|
||||||
)
|
|
||||||
).flat();
|
|
||||||
const teacherPromises = await Promise.all(
|
const teacherPromises = await Promise.all(
|
||||||
groupUsers.map(async (u) =>
|
groupUsers.map(async (u) =>
|
||||||
u.type === "teacher"
|
u.type === "teacher" ? u.id : u.type === "corporate" ? [...(await getAllAssignersByCorporate(u.id)), u.id] : undefined,
|
||||||
? u.id
|
),
|
||||||
: u.type === "corporate"
|
|
||||||
? [...(await getAllAssignersByCorporate(u.id)), u.id]
|
|
||||||
: undefined
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return teacherPromises.filter((x) => !!x).flat() as string[];
|
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 {
|
try {
|
||||||
const queryConstraints = [
|
const queryConstraints = [
|
||||||
...(admin ? [where("admin", "==", admin)] : []),
|
...(admin ? [where("admin", "==", admin)] : []),
|
||||||
...(participant
|
...(participant ? [where("participants", "array-contains", participant)] : []),
|
||||||
? [where("participants", "array-contains", participant)]
|
|
||||||
: []),
|
|
||||||
];
|
];
|
||||||
const snapshot = await getDocs(
|
const snapshot = await getDocs(queryConstraints.length > 0 ? query(collection(db, "groups"), ...queryConstraints) : collection(db, "groups"));
|
||||||
queryConstraints.length > 0
|
|
||||||
? query(collection(db, "groups"), ...queryConstraints)
|
|
||||||
: collection(db, "groups")
|
|
||||||
);
|
|
||||||
const groups = snapshot.docs.map((doc) => ({
|
const groups = snapshot.docs.map((doc) => ({
|
||||||
id: doc.id,
|
id: doc.id,
|
||||||
...doc.data(),
|
...doc.data(),
|
||||||
@@ -126,23 +74,14 @@ export const getGroupsForUser = async (admin: string, participant: string) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getStudentGroupsForUsersWithoutAdmin = async (
|
export const getStudentGroupsForUsersWithoutAdmin = async (admin: string, participants: string[]) => {
|
||||||
admin: string,
|
|
||||||
participants: string[]
|
|
||||||
) => {
|
|
||||||
try {
|
try {
|
||||||
const queryConstraints = [
|
const queryConstraints = [
|
||||||
...(admin ? [where("admin", "!=", admin)] : []),
|
...(admin ? [where("admin", "!=", admin)] : []),
|
||||||
...(participants
|
...(participants ? [where("participants", "array-contains-any", participants)] : []),
|
||||||
? [where("participants", "array-contains-any", participants)]
|
|
||||||
: []),
|
|
||||||
where("name", "==", "Students"),
|
where("name", "==", "Students"),
|
||||||
];
|
];
|
||||||
const snapshot = await getDocs(
|
const snapshot = await getDocs(queryConstraints.length > 0 ? query(collection(db, "groups"), ...queryConstraints) : collection(db, "groups"));
|
||||||
queryConstraints.length > 0
|
|
||||||
? query(collection(db, "groups"), ...queryConstraints)
|
|
||||||
: collection(db, "groups")
|
|
||||||
);
|
|
||||||
const groups = snapshot.docs.map((doc) => ({
|
const groups = snapshot.docs.map((doc) => ({
|
||||||
id: doc.id,
|
id: doc.id,
|
||||||
...doc.data(),
|
...doc.data(),
|
||||||
@@ -157,17 +96,17 @@ export const getStudentGroupsForUsersWithoutAdmin = async (
|
|||||||
|
|
||||||
export const getCorporateNameForStudent = async (studentID: string) => {
|
export const getCorporateNameForStudent = async (studentID: string) => {
|
||||||
const groups = await getStudentGroupsForUsersWithoutAdmin("", [studentID]);
|
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 adminUserIds = [...new Set(groups.map((g) => g.admin))];
|
||||||
const adminUsersData = await getSpecificUsers(adminUserIds);
|
const adminUsersData = await getSpecificUsers(adminUserIds);
|
||||||
|
|
||||||
if(adminUsersData.length === 0) return '';
|
if (adminUsersData.length === 0) return "";
|
||||||
const admins = adminUsersData.filter((x) => x.type === 'corporate');
|
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 (admins[0] as CorporateUser).corporateInformation.companyInformation.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return "";
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,15 +1,10 @@
|
|||||||
import { app } from "@/firebase";
|
import {app} from "@/firebase";
|
||||||
|
|
||||||
import {
|
import {collection, doc, getDoc, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||||
collection,
|
import {CorporateUser, Group, User} from "@/interfaces/user";
|
||||||
doc,
|
import {getGroupsForUser} from "./groups.be";
|
||||||
getDoc,
|
import {uniq, uniqBy} from "lodash";
|
||||||
getDocs,
|
import {getUserCodes} from "./codes.be";
|
||||||
getFirestore,
|
|
||||||
query,
|
|
||||||
where,
|
|
||||||
} from "firebase/firestore";
|
|
||||||
import { User } from "@/interfaces/user";
|
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
|
|
||||||
export async function getUsers() {
|
export async function getUsers() {
|
||||||
@@ -24,15 +19,13 @@ export async function getUsers() {
|
|||||||
export async function getUser(id: string) {
|
export async function getUser(id: string) {
|
||||||
const userDoc = await getDoc(doc(db, "users", id));
|
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[]) {
|
export async function getSpecificUsers(ids: string[]) {
|
||||||
if (ids.length === 0) return [];
|
if (ids.length === 0) return [];
|
||||||
|
|
||||||
const snapshot = await getDocs(
|
const snapshot = await getDocs(query(collection(db, "users"), where("id", "in", ids)));
|
||||||
query(collection(db, "users"), where("id", "in", ids))
|
|
||||||
);
|
|
||||||
|
|
||||||
const groups = snapshot.docs.map((doc) => ({
|
const groups = snapshot.docs.map((doc) => ({
|
||||||
id: doc.id,
|
id: doc.id,
|
||||||
@@ -41,3 +34,22 @@ export async function getSpecificUsers(ids: string[]) {
|
|||||||
|
|
||||||
return groups;
|
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