Solved a problem with the API call
This commit is contained in:
@@ -1,13 +1,7 @@
|
||||
import {app, adminApp} from "@/firebase";
|
||||
import {AgentUser} from "@/interfaces/user";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {
|
||||
collection,
|
||||
getDocs,
|
||||
getFirestore,
|
||||
query,
|
||||
where,
|
||||
} from "firebase/firestore";
|
||||
import {collection, getDocs, getFirestore, query, where} from "firebase/firestore";
|
||||
import {getAuth} from "firebase-admin/auth";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {NextApiRequest, NextApiResponse} from "next";
|
||||
@@ -23,22 +17,18 @@ interface Contact {
|
||||
number: string;
|
||||
}
|
||||
async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { language = 'en' } = req.query as { language: string };
|
||||
const {language = "en"} = req.query as {language: string};
|
||||
|
||||
const usersQuery = query(
|
||||
collection(db, "users"),
|
||||
where("type", "==", "agent")
|
||||
);
|
||||
const usersQuery = query(collection(db, "users"), where("type", "==", "agent"));
|
||||
const docsUser = await getDocs(usersQuery);
|
||||
|
||||
const docs = docsUser.docs.map((doc) => doc.data() as AgentUser);
|
||||
|
||||
const data = docs.reduce(
|
||||
(acc: Record<string, Contact[]>, user: AgentUser) => {
|
||||
const data = docs.reduce((acc: Record<string, Contact[]>, user: AgentUser) => {
|
||||
const countryCode = user.demographicInformation?.country as string;
|
||||
const currentValues = acc[countryCode] || ([] as Contact[]);
|
||||
const newUser = {
|
||||
name: user.agentInformation.companyName,
|
||||
name: user.agentInformation?.companyName || user.name,
|
||||
email: user.email,
|
||||
number: user.demographicInformation?.phone as string,
|
||||
} as Contact;
|
||||
@@ -46,13 +36,12 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
...acc,
|
||||
[countryCode]: [...currentValues, newUser],
|
||||
};
|
||||
},
|
||||
{}
|
||||
) as Record<string, Contact[]>;
|
||||
}, {}) as Record<string, Contact[]>;
|
||||
|
||||
const result = Object.keys(data).map((code) => {
|
||||
const country = countryCodes.findOne("countryCode" as any, code.toUpperCase());
|
||||
const key = language === 'ar' ? 'countryNameLocal' : 'countryNameEn';
|
||||
if (!country) return null;
|
||||
const key = language === "ar" ? "countryNameLocal" : "countryNameEn";
|
||||
return {
|
||||
label: country[key],
|
||||
key: code,
|
||||
@@ -60,7 +49,7 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
|
||||
};
|
||||
});
|
||||
|
||||
res.json(result);
|
||||
res.json(result.filter((x) => !!x));
|
||||
}
|
||||
|
||||
async function user(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
Reference in New Issue
Block a user