|
|
|
|
@@ -1,17 +1,17 @@
|
|
|
|
|
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
|
|
|
|
import type {NextApiRequest, NextApiResponse} from "next";
|
|
|
|
|
import {app, storage} from "@/firebase";
|
|
|
|
|
import {getFirestore, getDoc, doc, updateDoc, deleteField, setDoc} from "firebase/firestore";
|
|
|
|
|
import {withIronSessionApiRoute} from "iron-session/next";
|
|
|
|
|
import {sessionOptions} from "@/lib/session";
|
|
|
|
|
import {FilesStorage} from "@/interfaces/storage.files";
|
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
import { storage } from "@/firebase";
|
|
|
|
|
import client from "@/lib/mongodb";
|
|
|
|
|
import { withIronSessionApiRoute } from "iron-session/next";
|
|
|
|
|
import { sessionOptions } from "@/lib/session";
|
|
|
|
|
import { FilesStorage } from "@/interfaces/storage.files";
|
|
|
|
|
|
|
|
|
|
import {Payment} from "@/interfaces/paypal";
|
|
|
|
|
import { Payment } from "@/interfaces/paypal";
|
|
|
|
|
import fs from "fs";
|
|
|
|
|
import {ref, uploadBytes, deleteObject, getDownloadURL} from "firebase/storage";
|
|
|
|
|
import { ref, uploadBytes, deleteObject, getDownloadURL } from "firebase/storage";
|
|
|
|
|
import formidable from "formidable-serverless";
|
|
|
|
|
|
|
|
|
|
const db = getFirestore(app);
|
|
|
|
|
const db = client.db(process.env.MONGODB_DB);
|
|
|
|
|
|
|
|
|
|
const getPaymentField = (type: FilesStorage) => {
|
|
|
|
|
switch (type) {
|
|
|
|
|
@@ -25,28 +25,36 @@ const getPaymentField = (type: FilesStorage) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDelete = async (paymentId: string, paymentField: "commissionTransfer" | "corporateTransfer") => {
|
|
|
|
|
const paymentRef = doc(db, "payments", paymentId);
|
|
|
|
|
const paymentDoc = await getDoc(paymentRef);
|
|
|
|
|
const {[paymentField]: paymentFieldPath} = paymentDoc.data() as Payment;
|
|
|
|
|
// Create a reference to the file to delete
|
|
|
|
|
const documentRef = ref(storage, paymentFieldPath);
|
|
|
|
|
await deleteObject(documentRef);
|
|
|
|
|
await updateDoc(paymentRef, {
|
|
|
|
|
[paymentField]: deleteField(),
|
|
|
|
|
isPaid: false,
|
|
|
|
|
});
|
|
|
|
|
const paymentDoc = await db.collection("payments").findOne<Payment>({ id: paymentId })
|
|
|
|
|
|
|
|
|
|
if (paymentDoc) {
|
|
|
|
|
const { [paymentField]: paymentFieldPath } = paymentDoc;
|
|
|
|
|
|
|
|
|
|
// Create a reference to the file to delete
|
|
|
|
|
const documentRef = ref(storage, paymentFieldPath);
|
|
|
|
|
await deleteObject(documentRef);
|
|
|
|
|
await db.collection("payments").deleteOne({ id: paymentId });
|
|
|
|
|
|
|
|
|
|
await db.collection("payments").updateOne(
|
|
|
|
|
{ id: paymentId },
|
|
|
|
|
{
|
|
|
|
|
$unset: { [paymentField]: "" },
|
|
|
|
|
$set: { isPaid: false }
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleUpload = async (req: NextApiRequest, paymentId: string, paymentField: "commissionTransfer" | "corporateTransfer") =>
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
const form = formidable({keepExtensions: true});
|
|
|
|
|
const form = formidable({ keepExtensions: true });
|
|
|
|
|
form.parse(req, async (err: any, fields: any, files: any) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
reject(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const {file} = files;
|
|
|
|
|
const { file } = files;
|
|
|
|
|
const fileName = Date.now() + "-" + file.name;
|
|
|
|
|
const fileRef = ref(storage, fileName);
|
|
|
|
|
|
|
|
|
|
@@ -54,11 +62,13 @@ const handleUpload = async (req: NextApiRequest, paymentId: string, paymentField
|
|
|
|
|
const snapshot = await uploadBytes(fileRef, binary);
|
|
|
|
|
fs.rmSync(file.path);
|
|
|
|
|
|
|
|
|
|
const paymentRef = doc(db, "payments", paymentId);
|
|
|
|
|
await db.collection("payments").updateOne(
|
|
|
|
|
{ id: paymentId },
|
|
|
|
|
{
|
|
|
|
|
$set: { [paymentField]: snapshot.ref.fullPath }
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await updateDoc(paymentRef, {
|
|
|
|
|
[paymentField]: snapshot.ref.fullPath,
|
|
|
|
|
});
|
|
|
|
|
resolve(snapshot.ref.fullPath);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reject(err);
|
|
|
|
|
@@ -69,7 +79,7 @@ const handleUpload = async (req: NextApiRequest, paymentId: string, paymentField
|
|
|
|
|
export default withIronSessionApiRoute(handler, sessionOptions);
|
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
if (!req.session.user) {
|
|
|
|
|
res.status(401).json({ok: false});
|
|
|
|
|
res.status(401).json({ ok: false });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -82,80 +92,89 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
const {type, paymentId} = req.query as {
|
|
|
|
|
const { type, paymentId } = req.query as {
|
|
|
|
|
type: FilesStorage;
|
|
|
|
|
paymentId: string;
|
|
|
|
|
};
|
|
|
|
|
const paymentField = getPaymentField(type);
|
|
|
|
|
|
|
|
|
|
if (paymentField === null) {
|
|
|
|
|
res.status(500).json({error: "Failed to identify payment field"});
|
|
|
|
|
res.status(500).json({ error: "Failed to identify payment field" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const paymentRef = doc(db, "payments", paymentId);
|
|
|
|
|
const {[paymentField]: paymentFieldPath} = (await getDoc(paymentRef)).data() as Payment;
|
|
|
|
|
const paymentRef = await db.collection("payments").findOne<Payment>({ id: paymentId })
|
|
|
|
|
if (paymentRef) {
|
|
|
|
|
const { [paymentField]: paymentFieldPath } = paymentRef;
|
|
|
|
|
|
|
|
|
|
// Create a reference to the file to delete
|
|
|
|
|
const documentRef = ref(storage, paymentFieldPath);
|
|
|
|
|
const url = await getDownloadURL(documentRef);
|
|
|
|
|
res.status(200).json({url, name: documentRef.name});
|
|
|
|
|
// Create a reference to the file to delete
|
|
|
|
|
const documentRef = ref(storage, paymentFieldPath);
|
|
|
|
|
const url = await getDownloadURL(documentRef);
|
|
|
|
|
res.status(200).json({ url, name: documentRef.name });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
const {type, paymentId} = req.query as {
|
|
|
|
|
const { type, paymentId } = req.query as {
|
|
|
|
|
type: FilesStorage;
|
|
|
|
|
paymentId: string;
|
|
|
|
|
};
|
|
|
|
|
const paymentField = getPaymentField(type);
|
|
|
|
|
|
|
|
|
|
if (paymentField === null) {
|
|
|
|
|
res.status(500).json({error: "Failed to identify payment field"});
|
|
|
|
|
res.status(500).json({ error: "Failed to identify payment field" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ref = await handleUpload(req, paymentId, paymentField);
|
|
|
|
|
|
|
|
|
|
const updatedDoc = (await getDoc(doc(db, "payments", paymentId))).data() as Payment;
|
|
|
|
|
if (updatedDoc.commissionTransfer && updatedDoc.corporateTransfer) {
|
|
|
|
|
await setDoc(doc(db, "payments", paymentId), {isPaid: true}, {merge: true});
|
|
|
|
|
const updatedDoc = await db.collection("payments").findOne<Payment>({ id: paymentId })
|
|
|
|
|
if (updatedDoc && updatedDoc.commissionTransfer && updatedDoc.corporateTransfer) {
|
|
|
|
|
await db.collection("payments").updateOne(
|
|
|
|
|
{ id: paymentId },
|
|
|
|
|
{ $set: { isPaid: true } }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await setDoc(doc(db, "users", updatedDoc.corporate), {status: "active"}, {merge: true});
|
|
|
|
|
await db.collection("users").updateOne(
|
|
|
|
|
{ id: updatedDoc.corporate },
|
|
|
|
|
{ $set: { status: "active" } }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
res.status(200).json({ref});
|
|
|
|
|
res.status(200).json({ ref });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
res.status(500).json({error});
|
|
|
|
|
res.status(500).json({ error });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function del(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
const {type, paymentId} = req.query as {
|
|
|
|
|
const { type, paymentId } = req.query as {
|
|
|
|
|
type: FilesStorage;
|
|
|
|
|
paymentId: string;
|
|
|
|
|
};
|
|
|
|
|
const paymentField = getPaymentField(type);
|
|
|
|
|
if (paymentField === null) {
|
|
|
|
|
res.status(500).json({error: "Failed to identify payment field"});
|
|
|
|
|
res.status(500).json({ error: "Failed to identify payment field" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await handleDelete(paymentId, paymentField);
|
|
|
|
|
res.status(200).json({ok: true});
|
|
|
|
|
res.status(200).json({ ok: true });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
res.status(500).json({error: "Failed to delete file"});
|
|
|
|
|
res.status(500).json({ error: "Failed to delete file" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function patch(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
const {type, paymentId} = req.query as {
|
|
|
|
|
const { type, paymentId } = req.query as {
|
|
|
|
|
type: FilesStorage;
|
|
|
|
|
paymentId: string;
|
|
|
|
|
};
|
|
|
|
|
const paymentField = getPaymentField(type);
|
|
|
|
|
if (paymentField === null) {
|
|
|
|
|
res.status(500).json({error: "Failed to identify payment field"});
|
|
|
|
|
res.status(500).json({ error: "Failed to identify payment field" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -163,15 +182,15 @@ async function patch(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
await handleDelete(paymentId, paymentField);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
res.status(500).json({error: "Failed to delete file"});
|
|
|
|
|
res.status(500).json({ error: "Failed to delete file" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ref = await handleUpload(req, paymentId, paymentField);
|
|
|
|
|
res.status(200).json({ref});
|
|
|
|
|
res.status(200).json({ ref });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
res.status(500).json({error: "Failed to upload file"});
|
|
|
|
|
res.status(500).json({ error: "Failed to upload file" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|