Merge branch 'develop' into feature-paypal-simple
This commit is contained in:
30
src/pages/api/payments/paypal.ts
Normal file
30
src/pages/api/payments/paypal.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { app } from "@/firebase";
|
||||
import {
|
||||
getFirestore,
|
||||
getDocs,
|
||||
collection,
|
||||
} from "firebase/firestore";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const payments = await getDocs(collection(db, "paypalpayments"));
|
||||
|
||||
const data = payments.docs.map((doc) => doc.data());
|
||||
res.status(200).json(data);
|
||||
}
|
||||
|
||||
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (!req.session.user) {
|
||||
res.status(401).json({ ok: false });
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === "GET") await get(req, res);
|
||||
}
|
||||
@@ -73,6 +73,25 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
|
||||
try {
|
||||
await setDoc(
|
||||
doc(db, 'paypalpayments', v4()),
|
||||
{
|
||||
orderId: id,
|
||||
userId: req.session.user.id,
|
||||
status: request.data.status,
|
||||
createdAt: new Date().toISOString(),
|
||||
value: request.data.purchase_units[0].payments.captures[0].amount.value,
|
||||
currency: request.data.purchase_units[0].payments.captures[0].amount.currency_code,
|
||||
subscriptionDuration: duration,
|
||||
subscriptionDurationUnit: duration_unit,
|
||||
subscriptionExpirationDate: updatedExpirationDate.toISOString(),
|
||||
}
|
||||
);
|
||||
} catch(err) {
|
||||
console.error('Failed to insert paypal payment!', err);
|
||||
}
|
||||
|
||||
if (user.type === "corporate") {
|
||||
const snapshot = await getDocs(collection(db, "groups"));
|
||||
|
||||
@@ -28,7 +28,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
}
|
||||
|
||||
const audioFile = files.audio;
|
||||
const audioFileRef = ref(storage, `${fields.root}/${(audioFile as any).path.split("/").pop()!.replace("upload_", "")}`);
|
||||
const audioFileRef = ref(storage, `${fields.root}/${(audioFile as any).path.replace("upload_", "")}`);
|
||||
|
||||
const binary = fs.readFileSync((audioFile as any).path).buffer;
|
||||
const snapshot = await uploadBytes(audioFileRef, binary);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import { sendEmail } from "@/email";
|
||||
import { app } from "@/firebase";
|
||||
import { Ticket, TicketTypeLabel } from "@/interfaces/ticket";
|
||||
import { Ticket, TicketTypeLabel, TicketWithCorporate } from "@/interfaces/ticket";
|
||||
import { sessionOptions } from "@/lib/session";
|
||||
import {
|
||||
collection,
|
||||
@@ -9,11 +9,14 @@ import {
|
||||
getDocs,
|
||||
getFirestore,
|
||||
setDoc,
|
||||
where,
|
||||
query,
|
||||
} from "firebase/firestore";
|
||||
import { withIronSessionApiRoute } from "iron-session/next";
|
||||
import moment from "moment";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import ShortUniqueId from "short-unique-id";
|
||||
import { Group, CorporateUser } from "@/interfaces/user";
|
||||
|
||||
const db = getFirestore(app);
|
||||
|
||||
@@ -44,12 +47,38 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const snapshot = await getDocs(collection(db, "tickets"));
|
||||
|
||||
res.status(200).json(
|
||||
snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
}))
|
||||
);
|
||||
const docs = snapshot.docs.map((doc) => ({
|
||||
id: doc.id,
|
||||
...doc.data(),
|
||||
})) as Ticket[];
|
||||
|
||||
// fetch all groups for these users
|
||||
|
||||
const reporters = [...new Set(docs.map((d) => d.reporter.id).filter((id) => id))];
|
||||
|
||||
const groupsSnapshot = await getDocs(query(collection(db, "groups"), where("participants", "array-contains-any", reporters)));
|
||||
const groups = groupsSnapshot.docs.map((doc) => doc.data()) as Group[];
|
||||
|
||||
// based on the admin of each group, verify if it exists and it's of type corporate
|
||||
const groupsAdmins = [...new Set(groups.map((g) => g.admin).filter((id) => id))];
|
||||
const adminsSnapshot = await getDocs(query(collection(db, "users"), where("id", "in", groupsAdmins), where("type", "==", "corporate")));
|
||||
const admins = adminsSnapshot.docs.map((doc) => doc.data());
|
||||
|
||||
const docsWithAdmins = docs.map((d) => {
|
||||
const group = groups.find((g) => g.participants.includes(d.reporter.id));
|
||||
const admin = admins.find((a) => a.id === group?.admin) as CorporateUser;
|
||||
|
||||
if(admin) {
|
||||
return {
|
||||
...d,
|
||||
corporate: admin.corporateInformation?.companyInformation?.name,
|
||||
};
|
||||
}
|
||||
|
||||
return d;
|
||||
}) as TicketWithCorporate[];
|
||||
|
||||
res.status(200).json(docsWithAdmins);
|
||||
}
|
||||
|
||||
async function post(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
@@ -23,7 +23,7 @@ interface Contact {
|
||||
number: string;
|
||||
}
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { code } = req.query as { code: string };
|
||||
const { code, language = 'en' } = req.query as { code: string, language: string};
|
||||
|
||||
const usersQuery = query(
|
||||
collection(db, "users"),
|
||||
@@ -43,9 +43,11 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
return newUser;
|
||||
}) as Contact[];
|
||||
|
||||
const country = countryCodes.findOne("countryCode" as any, code);
|
||||
const country = countryCodes.findOne("countryCode" as any, code.toUpperCase());
|
||||
const key = language === 'ar' ? 'countryNameLocal' : 'countryNameEn';
|
||||
|
||||
res.json({
|
||||
label: country.countryNameEn,
|
||||
label: country[key],
|
||||
entries,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ interface Contact {
|
||||
number: string;
|
||||
}
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { language = 'en' } = req.query as { language: string };
|
||||
|
||||
const usersQuery = query(
|
||||
collection(db, "users"),
|
||||
where("type", "==", "agent")
|
||||
@@ -49,9 +51,10 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
) as Record<string, Contact[]>;
|
||||
|
||||
const result = Object.keys(data).map((code) => {
|
||||
const country = countryCodes.findOne("countryCode" as any, code);
|
||||
const country = countryCodes.findOne("countryCode" as any, code.toUpperCase());
|
||||
const key = language === 'ar' ? 'countryNameLocal' : 'countryNameEn';
|
||||
return {
|
||||
label: country.countryNameEn,
|
||||
label: country[key],
|
||||
key: code,
|
||||
entries: data[code],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user