From 1a17689cd22d2154fd765edebfaf914410523285 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Sun, 21 Apr 2024 00:37:08 +0100 Subject: [PATCH] Updated the code to name the field companyArabName and made it so it returns it when arabic --- src/components/UserCard.tsx | 10 +-- src/interfaces/user.ts | 2 +- src/pages/api/users/agents/[code].ts | 20 ++++-- src/pages/api/users/agents/index.ts | 97 +++++++++++++++++----------- src/pages/profile.tsx | 2 +- 5 files changed, 80 insertions(+), 51 deletions(-) diff --git a/src/components/UserCard.tsx b/src/components/UserCard.tsx index 5c143f57..6fdab7c0 100644 --- a/src/components/UserCard.tsx +++ b/src/components/UserCard.tsx @@ -106,7 +106,7 @@ const UserCard = ({ : undefined, ); const [arabName, setArabName] = useState( - user.type === "agent" ? user.agentInformation?.arabName : undefined, + user.type === "agent" ? user.agentInformation?.companyArabName : undefined, ); const [commercialRegistration, setCommercialRegistration] = useState( user.type === "agent" @@ -236,21 +236,21 @@ const UserCard = ({ <>
{ 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], diff --git a/src/pages/api/users/agents/index.ts b/src/pages/api/users/agents/index.ts index add7e0c1..be7c209c 100644 --- a/src/pages/api/users/agents/index.ts +++ b/src/pages/api/users/agents/index.ts @@ -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, 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; + const data = docs.reduce( + (acc: Record, 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; - 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); } diff --git a/src/pages/profile.tsx b/src/pages/profile.tsx index ea1ab049..28d3558d 100644 --- a/src/pages/profile.tsx +++ b/src/pages/profile.tsx @@ -151,7 +151,7 @@ function UserProfile({ user, mutateUser }: Props) { : undefined, ); const [arabName, setArabName] = useState( - user.type === "agent" ? user.agentInformation?.arabName : undefined, + user.type === "agent" ? user.agentInformation?.companyArabName : undefined, ); const [timezone, setTimezone] = useState(