Added API endpoints for agents load for the homepage
This commit is contained in:
@@ -35,6 +35,21 @@ const nextConfig = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
source: "/api/users/agents",
|
||||||
|
headers: [
|
||||||
|
{key: "Access-Control-Allow-Credentials", value: "false"},
|
||||||
|
{key: "Access-Control-Allow-Origin", value: websiteUrl},
|
||||||
|
{
|
||||||
|
key: "Access-Control-Allow-Methods",
|
||||||
|
value: "POST,OPTIONS",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "Access-Control-Allow-Headers",
|
||||||
|
value: "Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
55
src/pages/api/users/agents/[code].ts
Normal file
55
src/pages/api/users/agents/[code].ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
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 } = req.query as { code: 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);
|
||||||
|
res.json({
|
||||||
|
label: country.countryNameEn,
|
||||||
|
entries,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function user(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
if (req.method === "GET") return get(req, res);
|
||||||
|
}
|
||||||
65
src/pages/api/users/agents/index.ts
Normal file
65
src/pages/api/users/agents/index.ts
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
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 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 countryCode = user.demographicInformation?.country as string;
|
||||||
|
const currentValues = acc[countryCode] || ([] as Contact[]);
|
||||||
|
const newUser = {
|
||||||
|
name: user.agentInformation.companyName,
|
||||||
|
email: user.email,
|
||||||
|
number: user.demographicInformation?.phone as string,
|
||||||
|
} as Contact;
|
||||||
|
return {
|
||||||
|
...acc,
|
||||||
|
[countryCode]: [...currentValues, newUser],
|
||||||
|
};
|
||||||
|
},
|
||||||
|
{}
|
||||||
|
) as Record<string, Contact[]>;
|
||||||
|
|
||||||
|
const result = Object.keys(data).map((code) => {
|
||||||
|
const country = countryCodes.findOne("countryCode" as any, code);
|
||||||
|
return {
|
||||||
|
label: country.countryNameEn,
|
||||||
|
key: code,
|
||||||
|
entries: data[code],
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function user(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
if (req.method === "GET") return get(req, res);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user