diff --git a/src/app/dashboard/cms/page.tsx b/src/app/dashboard/cms/page.tsx index 473317d..8f10496 100644 --- a/src/app/dashboard/cms/page.tsx +++ b/src/app/dashboard/cms/page.tsx @@ -270,26 +270,55 @@ export default function CmsPage() { const subtitle = agent?.agentType?.name || agent?.headline || ''; const imageUrl = profile?.avatar || ''; + // Helper to format snake_case to Title Case + const formatLabel = (v: string) => + v.split('_').map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join(' '); + + // Helper to stringify a field value + const valueToString = (val: any): string => { + if (val == null) return ''; + if (Array.isArray(val)) { + return val.map((v) => (typeof v === 'object' ? (v.label || v.name || JSON.stringify(v)) : formatLabel(String(v)))).join(', '); + } + if (typeof val === 'object') return val.label || val.name || JSON.stringify(val); + return formatLabel(String(val)); + }; + // Fetch full agent profile with field values for location/experience/expertise let location = [profile?.city, profile?.state].filter(Boolean).join(', '); let experience = agent?.yearsOfExperience ? `${agent.yearsOfExperience}+ years in real estate.` : ''; let expertise: string[] = []; + // Specific slugs to look for (matches profile field seed) + const EXPERIENCE_SLUGS = ['years_in_business', 'years_of_experience']; + const EXPERTISE_SLUGS = ['about_me_expertise', 'expertise_areas']; + if (agent?.id) { try { const res = await api.get(`/agents/${agent.id}/field-values`); const fieldValues = res.data?.data?.fieldValues || res.data?.data || []; for (const fv of fieldValues) { - const slug = fv.field?.slug || fv.fieldSlug || ''; - if (!location && (slug === 'city' || slug === 'state')) { - const vals = Array.isArray(fv.jsonValue) ? fv.jsonValue.join(', ') : (fv.textValue || ''); - if (vals) location = location ? `${location}, ${vals}` : vals; + const slug: string = (fv.fieldSlug || fv.field?.slug || '').toLowerCase(); + const value = fv.value ?? fv.jsonValue ?? fv.textValue; + if (value == null || value === '') continue; + + // Location: city/state fields + if ((slug === 'city' || slug === 'state') && !location) { + location = Array.isArray(value) ? value.join(', ') : String(value); } - if (!experience && (slug === 'years_in_business' || slug === 'experience')) { - experience = fv.textValue ? `${fv.textValue} years in real estate.` : ''; + + // Experience: only specific known slugs (backend already resolves option labels) + if (!experience && EXPERIENCE_SLUGS.includes(slug)) { + experience = Array.isArray(value) ? value.join(', ') : String(value); } - if (slug === 'about_me_expertise' || slug === 'expertise_areas') { - if (Array.isArray(fv.jsonValue)) expertise = [...expertise, ...fv.jsonValue]; + + // Expertise: only specific known slugs (backend already resolves option labels) + if (EXPERTISE_SLUGS.includes(slug)) { + if (Array.isArray(value)) { + expertise.push(...value.map((v: any) => typeof v === 'object' ? (v.label || v.name || String(v)) : String(v))); + } else if (typeof value === 'string' && value.trim()) { + expertise.push(value); + } } } } catch { @@ -314,22 +343,39 @@ export default function CmsPage() { const role = agent?.agentType?.name || agent?.headline || ''; const imageUrl = profile?.avatar || ''; + // Helper to format snake_case to Title Case + const formatLabel = (v: string) => + v.split('_').map(w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join(' '); + + const valueToString = (val: any): string => { + if (val == null) return ''; + if (Array.isArray(val)) { + return val.map((v) => (typeof v === 'object' ? (v.label || v.name || JSON.stringify(v)) : formatLabel(String(v)))).join(', '); + } + if (typeof val === 'object') return val.label || val.name || JSON.stringify(val); + return formatLabel(String(val)); + }; + let location = [profile?.city, profile?.state].filter(Boolean).join(', '); - let experience = agent?.yearsOfExperience ? `${agent.yearsOfExperience}+ years in real estate.` : ''; - const rating = ''; + let experience = agent?.yearsOfExperience ? `${agent.yearsOfExperience}+ years` : ''; + const rating = agent?.averageRating ? String(agent.averageRating) : ''; + + const EXPERIENCE_SLUGS = ['years_in_business', 'years_of_experience']; if (agent?.id) { try { const res = await api.get(`/agents/${agent.id}/field-values`); const fieldValues = res.data?.data?.fieldValues || res.data?.data || []; for (const fv of fieldValues) { - const slug = fv.field?.slug || fv.fieldSlug || ''; - if (!location && (slug === 'city' || slug === 'state')) { - const vals = Array.isArray(fv.jsonValue) ? fv.jsonValue.join(', ') : (fv.textValue || ''); - if (vals) location = location ? `${location}, ${vals}` : vals; + const slug: string = (fv.fieldSlug || fv.field?.slug || '').toLowerCase(); + const value = fv.value ?? fv.jsonValue ?? fv.textValue; + if (value == null || value === '') continue; + + if ((slug === 'city' || slug === 'state') && !location) { + location = Array.isArray(value) ? value.join(', ') : String(value); } - if (!experience && (slug === 'years_in_business' || slug === 'experience')) { - experience = fv.textValue ? `${fv.textValue} years in real estate.` : ''; + if (!experience && EXPERIENCE_SLUGS.includes(slug)) { + experience = Array.isArray(value) ? value.join(', ') : String(value); } } } catch { diff --git a/src/services/users.service.ts b/src/services/users.service.ts index a5a84dc..a00ea6f 100644 --- a/src/services/users.service.ts +++ b/src/services/users.service.ts @@ -37,6 +37,9 @@ export interface AgentProfileDetails { verificationNote?: string | null; verifiedAt?: string | null; verifiedBy?: string | null; + // Stats + averageRating?: number | null; + totalReviews?: number | null; } export interface User {