Merged in feature-newPaymentOnUserUpdate (pull request #3)
Feature newPaymentOnUserUpdate Approved-by: Tiago Ribeiro
This commit is contained in:
@@ -60,7 +60,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
const [paymentValue, setPaymentValue] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.value : 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 [paymentCurrency, setPaymentCurrency] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.currency : "EUR");
|
||||||
const [monthlyDuration, setMonthlyDuration] = useState(user.type === "corporate" ? user.corporateInformation?.monthlyDuration : undefined);
|
const [monthlyDuration, setMonthlyDuration] = useState(user.type === "corporate" ? user.corporateInformation?.monthlyDuration : undefined);
|
||||||
|
const [commissionValue, setCommission] = useState(user.type === "corporate" ? user.corporateInformation?.payment?.commission : undefined);
|
||||||
const {stats} = useStats(user.id);
|
const {stats} = useStats(user.id);
|
||||||
const {users} = useUsers();
|
const {users} = useUsers();
|
||||||
|
|
||||||
@@ -106,6 +106,7 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
payment: {
|
payment: {
|
||||||
value: paymentValue,
|
value: paymentValue,
|
||||||
currency: paymentCurrency,
|
currency: paymentCurrency,
|
||||||
|
...referralAgent === '' ? {} : { commission: commissionValue }
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
: undefined,
|
: undefined,
|
||||||
@@ -194,8 +195,31 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
placeholder="Enter monthly duration"
|
placeholder="Enter monthly duration"
|
||||||
defaultValue={monthlyDuration}
|
defaultValue={monthlyDuration}
|
||||||
/>
|
/>
|
||||||
|
<div className="flex flex-col gap-3 w-full lg:col-span-2">
|
||||||
<div className="flex flex-col gap-3 w-full">
|
<label className="font-normal text-base text-mti-gray-dim">Pricing</label>
|
||||||
|
<div className="w-full grid grid-cols-5 gap-2">
|
||||||
|
<Input
|
||||||
|
name="paymentValue"
|
||||||
|
onChange={(e) => setPaymentValue(e ? parseInt(e) : undefined)}
|
||||||
|
type="number"
|
||||||
|
defaultValue={paymentValue || 0}
|
||||||
|
className="col-span-3"
|
||||||
|
/>
|
||||||
|
<select
|
||||||
|
defaultValue={paymentCurrency}
|
||||||
|
onChange={(e) => setPaymentCurrency(e.target.value)}
|
||||||
|
className="p-6 col-span-2 w-full min-h-[70px] flex justify-center text-sm font-normal rounded-full border focus:outline-none cursor-pointer bg-white">
|
||||||
|
{CURRENCIES.map(({label, currency}) => (
|
||||||
|
<option value={currency} key={currency}>
|
||||||
|
{label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-3 w-full">
|
||||||
|
<div className="flex flex-col gap-3 w-8/12">
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Country Manager</label>
|
<label className="font-normal text-base text-mti-gray-dim">Country Manager</label>
|
||||||
{referralAgentLabel && (
|
{referralAgentLabel && (
|
||||||
<Select
|
<Select
|
||||||
@@ -228,28 +252,19 @@ const UserCard = ({user, loggedInUser, onClose, onViewStudents, onViewTeachers,
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex flex-col gap-3 w-4/12">
|
||||||
<div className="flex flex-col gap-3 w-full lg:col-span-2">
|
{referralAgent !== '' ? (
|
||||||
<label className="font-normal text-base text-mti-gray-dim">Pricing</label>
|
<>
|
||||||
<div className="w-full grid grid-cols-5 gap-2">
|
<label className="font-normal text-base text-mti-gray-dim">Commission</label>
|
||||||
<Input
|
<Input
|
||||||
name="paymentValue"
|
name="commissionValue"
|
||||||
onChange={(e) => setPaymentValue(e ? parseInt(e) : undefined)}
|
onChange={(e) => setCommission(e ? parseInt(e) : undefined)}
|
||||||
type="number"
|
type="number"
|
||||||
defaultValue={paymentValue || 0}
|
defaultValue={commissionValue || 0}
|
||||||
className="col-span-3"
|
className="col-span-3"
|
||||||
/>
|
/>
|
||||||
<select
|
</>
|
||||||
defaultValue={paymentCurrency}
|
) : <div />}
|
||||||
onChange={(e) => setPaymentCurrency(e.target.value)}
|
|
||||||
className="p-6 col-span-2 w-full min-h-[70px] flex justify-center text-sm font-normal rounded-full border focus:outline-none cursor-pointer bg-white">
|
|
||||||
{CURRENCIES.map(({label, currency}) => (
|
|
||||||
<option value={currency} key={currency}>
|
|
||||||
{label}
|
|
||||||
</option>
|
|
||||||
))}
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Divider className="w-full !m-0" />
|
<Divider className="w-full !m-0" />
|
||||||
|
|||||||
@@ -31,5 +31,5 @@ export interface Payment {
|
|||||||
currency: string;
|
currency: string;
|
||||||
value: number;
|
value: number;
|
||||||
isPaid: boolean;
|
isPaid: boolean;
|
||||||
date: Date;
|
date: Date | string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ export interface CorporateInformation {
|
|||||||
payment?: {
|
payment?: {
|
||||||
value: number;
|
value: number;
|
||||||
currency: string;
|
currency: string;
|
||||||
|
commission: number;
|
||||||
};
|
};
|
||||||
referralAgent?: string;
|
referralAgent?: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||||
import type {NextApiRequest, NextApiResponse} from "next";
|
import type {NextApiRequest, NextApiResponse} from "next";
|
||||||
import {app} from "@/firebase";
|
import {app} from "@/firebase";
|
||||||
import {getFirestore, collection, getDocs, getDoc, doc, setDoc} from "firebase/firestore";
|
import {getFirestore, collection, getDocs, getDoc, doc, setDoc, query, where} from "firebase/firestore";
|
||||||
import {withIronSessionApiRoute} from "iron-session/next";
|
import {withIronSessionApiRoute} from "iron-session/next";
|
||||||
import {sessionOptions} from "@/lib/session";
|
import {sessionOptions} from "@/lib/session";
|
||||||
import {User} from "@/interfaces/user";
|
import {User} from "@/interfaces/user";
|
||||||
import {getDownloadURL, getStorage, ref, uploadBytes} from "firebase/storage";
|
import {getDownloadURL, getStorage, ref, uploadBytes} from "firebase/storage";
|
||||||
import {getAuth, signInWithEmailAndPassword, updateEmail, updatePassword} from "firebase/auth";
|
import {getAuth, signInWithEmailAndPassword, updateEmail, updatePassword} from "firebase/auth";
|
||||||
import {errorMessages} from "@/constants/errors";
|
import {errorMessages} from "@/constants/errors";
|
||||||
|
import moment from "moment";
|
||||||
|
import ShortUniqueId from "short-unique-id";
|
||||||
|
import {Payment} from "@/interfaces/paypal";
|
||||||
|
|
||||||
const db = getFirestore(app);
|
const db = getFirestore(app);
|
||||||
const storage = getStorage(app);
|
const storage = getStorage(app);
|
||||||
@@ -15,6 +18,56 @@ const auth = getAuth(app);
|
|||||||
|
|
||||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||||
|
|
||||||
|
// TODO: Data is set as any as data cannot be parsed to Payment
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
const managePaymentRecords = async (user: User, userId: string | undefined): Promise<boolean> => {
|
||||||
|
try {
|
||||||
|
if(user.type === 'corporate' && userId) {
|
||||||
|
const shortUID = new ShortUniqueId();
|
||||||
|
const data: Payment = {
|
||||||
|
id: shortUID.randomUUID(8),
|
||||||
|
corporate: userId,
|
||||||
|
agent: user.corporateInformation.referralAgent,
|
||||||
|
agentCommission: user.corporateInformation.payment!.commission,
|
||||||
|
agentValue: (user.corporateInformation.payment!.commission / 100) * user.corporateInformation.payment!.value,
|
||||||
|
currency: user.corporateInformation.payment!.currency,
|
||||||
|
value: user.corporateInformation.payment!.value,
|
||||||
|
isPaid: false,
|
||||||
|
date: new Date().toISOString(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const corporatePayments = await getDocs(query(collection(db, "payments"), where("corporate", "==", userId)));
|
||||||
|
if(corporatePayments.docs.length === 0) {
|
||||||
|
await addPaymentRecord(data);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasPaymentPaidAndExpiring = corporatePayments.docs.filter((doc) => {
|
||||||
|
const data = doc.data();
|
||||||
|
return data.isPaid
|
||||||
|
&& moment().isAfter(moment(user.subscriptionExpirationDate).subtract(30, "days"))
|
||||||
|
&& moment().isBefore(moment(user.subscriptionExpirationDate));
|
||||||
|
});
|
||||||
|
|
||||||
|
if(hasPaymentPaidAndExpiring.length > 0) {
|
||||||
|
await addPaymentRecord(data);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch(e) {
|
||||||
|
// if this process fails it should not stop the rest of the process
|
||||||
|
console.log(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
if (!req.session.user) {
|
if (!req.session.user) {
|
||||||
res.status(401).json({ok: false});
|
res.status(401).json({ok: false});
|
||||||
@@ -25,7 +78,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
const updatedUser = req.body as User & {password?: string; newPassword?: string};
|
const updatedUser = req.body as User & {password?: string; newPassword?: string};
|
||||||
|
|
||||||
if (!!req.query.id) {
|
if (!!req.query.id) {
|
||||||
await setDoc(userRef, updatedUser, {merge: true});
|
const user = await setDoc(userRef, updatedUser, {merge: true});
|
||||||
|
await managePaymentRecords(updatedUser, updatedUser.id);
|
||||||
res.status(200).json({ok: true});
|
res.status(200).json({ok: true});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -74,6 +128,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
await req.session.save();
|
await req.session.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await managePaymentRecords(user, req.query.id);
|
||||||
|
|
||||||
res.status(200).json({user});
|
res.status(200).json({user});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user