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