Updated the code to name the field companyArabName and made it so it returns it when arabic

This commit is contained in:
Tiago Ribeiro
2024-04-21 00:37:08 +01:00
parent a958e2ff0d
commit 1a17689cd2
5 changed files with 80 additions and 51 deletions

View File

@@ -23,12 +23,15 @@ interface Contact {
number: string;
}
async function get(req: NextApiRequest, res: NextApiResponse) {
const { code, language = 'en' } = req.query as { code: string, language: string};
const { code, language = "en" } = req.query as {
code: string;
language: string;
};
const usersQuery = query(
collection(db, "users"),
where("type", "==", "agent"),
where("demographicInformation.country", "==", code)
where("demographicInformation.country", "==", code),
);
const docsUser = await getDocs(usersQuery);
@@ -36,15 +39,22 @@ async function get(req: NextApiRequest, res: NextApiResponse) {
const entries = docs.map((user: AgentUser) => {
const newUser = {
name: user.agentInformation.companyName,
name:
(language === "en"
? user.agentInformation?.companyName
: user.agentInformation?.companyArabName ||
user.agentInformation?.companyName) || user.name,
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';
const country = countryCodes.findOne(
"countryCode" as any,
code.toUpperCase(),
);
const key = language === "ar" ? "countryNameLocal" : "countryNameEn";
res.json({
label: country[key],

View File

@@ -1,10 +1,16 @@
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 { 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);
@@ -12,46 +18,59 @@ const auth = getAuth(adminApp);
export default withIronSessionApiRoute(user, sessionOptions);
interface Contact {
name: string;
email: string;
number: string;
name: string;
email: string;
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 docsUser = await getDocs(usersQuery);
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 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 || user.name,
email: user.email,
number: user.demographicInformation?.phone as string,
} as Contact;
return {
...acc,
[countryCode]: [...currentValues, newUser],
};
}, {}) as Record<string, Contact[]>;
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:
(language === "en"
? user.agentInformation?.companyName
: user.agentInformation?.companyArabName ||
user.agentInformation?.companyName) || user.name,
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.toUpperCase());
if (!country) return null;
const key = language === "ar" ? "countryNameLocal" : "countryNameEn";
return {
label: country[key],
key: code,
entries: data[code],
};
});
const result = Object.keys(data).map((code) => {
const country = countryCodes.findOne(
"countryCode" as any,
code.toUpperCase(),
);
if (!country) return null;
const key = language === "ar" ? "countryNameLocal" : "countryNameEn";
return {
label: country[key],
key: code,
entries: data[code],
};
});
res.json(result.filter((x) => !!x));
res.json(result.filter((x) => !!x));
}
async function user(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "GET") return get(req, res);
if (req.method === "GET") return get(req, res);
}

View File

@@ -151,7 +151,7 @@ function UserProfile({ user, mutateUser }: Props) {
: undefined,
);
const [arabName, setArabName] = useState<string | undefined>(
user.type === "agent" ? user.agentInformation?.arabName : undefined,
user.type === "agent" ? user.agentInformation?.companyArabName : undefined,
);
const [timezone, setTimezone] = useState<string>(