58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
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 { getAuth } from "firebase-admin/auth";
|
|
import { withIronSessionApiRoute } from "iron-session/next";
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
import countryCodes from "country-codes-list";
|
|
const db = getFirestore(app);
|
|
const auth = getAuth(adminApp);
|
|
|
|
export default withIronSessionApiRoute(user, sessionOptions);
|
|
|
|
interface Contact {
|
|
name: string;
|
|
email: string;
|
|
number: string;
|
|
}
|
|
async function get(req: NextApiRequest, res: NextApiResponse) {
|
|
const { code, language = 'en' } = req.query as { code: string, language: string};
|
|
|
|
const usersQuery = query(
|
|
collection(db, "users"),
|
|
where("type", "==", "agent"),
|
|
where("demographicInformation.country", "==", code)
|
|
);
|
|
const docsUser = await getDocs(usersQuery);
|
|
|
|
const docs = docsUser.docs.map((doc) => doc.data() as AgentUser);
|
|
|
|
const entries = docs.map((user: AgentUser) => {
|
|
const newUser = {
|
|
name: user.agentInformation.companyName,
|
|
email: user.email,
|
|
number: user.demographicInformation?.phone as string,
|
|
} as Contact;
|
|
return newUser;
|
|
}) as Contact[];
|
|
|
|
const country = countryCodes.findOne("countryCode" as any, code.toUpperCase());
|
|
const key = language === 'ar' ? 'countryNameLocal' : 'countryNameEn';
|
|
|
|
res.json({
|
|
label: country[key],
|
|
entries,
|
|
});
|
|
}
|
|
|
|
async function user(req: NextApiRequest, res: NextApiResponse) {
|
|
if (req.method === "GET") return get(req, res);
|
|
}
|