From 334739e7a052da230845c3783a769f3e846e98f6 Mon Sep 17 00:00:00 2001 From: Ravi Gulhane <> Date: Sun, 22 Nov 2026 08:56:07 +0530 Subject: [PATCH] Insights glossary admin screens List (generic EntityList, POST delete), add/edit page with a guided formula builder (value or ratio over metrics or contact/people counts, percent switch) and a Test panel that runs the formula on live data for the admin's scope. Menu "Insights > Glossary" and the page require insights_glossary_admin. API: rapport insights glossary endpoints. Co-Authored-By: Claude Opus 5.5 --- src/layouts/menu.js | 9 + src/modules/insightsglossary.js | 24 ++ src/routes/index.jsx | 6 + src/views/insights/GlossaryForm.jsx | 403 ++++++++++++++++++++++++++++ 4 files changed, 442 insertions(+) create mode 100644 src/modules/insightsglossary.js create mode 100644 src/views/insights/GlossaryForm.jsx diff --git a/src/layouts/menu.js b/src/layouts/menu.js index 3f3836c..e0cc3c0 100644 --- a/src/layouts/menu.js +++ b/src/layouts/menu.js @@ -354,5 +354,14 @@ export function getMenuItems({ permissions = [], roles = [], domain = '', email }); } + if (permissions.includes('insights_glossary_admin')) { + items.push({ + key: 'insights', + label: 'Insights', + icon: LuChartNoAxesCombined, + children: [child('insights-glossary', 'Glossary', '/insights/glossarys', 'insights_glossary_admin')], + }); + } + return items.filter((item) => !item.children || item.children.length > 0); } diff --git a/src/modules/insightsglossary.js b/src/modules/insightsglossary.js new file mode 100644 index 0000000..3109d6d --- /dev/null +++ b/src/modules/insightsglossary.js @@ -0,0 +1,24 @@ +// Rapport Insights glossary terms (rapport/insights/functions/glossary.js). +// Listed with the generic EntityList; adding and editing use the custom +// formula builder page (src/views/insights/GlossaryForm.jsx). +const insightsglossary = { + service: 'insights', + module: 'glossary', + title: 'Glossary', + subtitle: 'Insights', + baseUrl: '/insights/glossary', + listPath: '/insights/glossarys', + deleteMethod: 'post', + listFields: [ + { key: 'actions', title: 'Actions', buttons: ['edit', 'delete'] }, + { key: 'name', title: 'Term', type: 'text' }, + { key: 'synonyms_text', title: 'Also called', type: 'text' }, + { key: 'description', title: 'Description', type: 'text' }, + { key: 'status', title: 'Status', type: 'text' }, + { key: 'updated_by_name', title: 'Updated by', type: 'text' }, + { key: 'updated_at', title: 'Updated', type: 'datetime' }, + ], + formFields: () => [], +}; + +export default insightsglossary; diff --git a/src/routes/index.jsx b/src/routes/index.jsx index 21272a8..8c1fda0 100644 --- a/src/routes/index.jsx +++ b/src/routes/index.jsx @@ -65,6 +65,7 @@ import articlebanks from '@/modules/articlebanks'; import lmsquizs from '@/modules/lmsquizs'; import sessions from '@/modules/sessions'; import tracks from '@/modules/tracks'; +import insightsglossary from '@/modules/insightsglossary'; const Login = lazy(() => import('@/views/auth/Login')); const LoginOtp = lazy(() => import('@/views/auth/LoginOtp')); @@ -99,6 +100,7 @@ const ActionPlanReport = lazy(() => import('@/views/coachingtooldashboard/Action const GoalProficiencyReport = lazy(() => import('@/views/coachingtooldashboard/GoalProficiencyReport')); const AdherencePobDriveReport = lazy(() => import('@/views/adherencepobdrivereport/Report')); const IvaReportPage = lazy(() => import('@/views/ivareport/ReportPage')); +const InsightsGlossaryForm = lazy(() => import('@/views/insights/GlossaryForm')); function moduleRoutes(config) { const routes = [ @@ -219,6 +221,10 @@ export const routes = [ ...moduleRoutes(lmsquizs), ...moduleRoutes(sessions), ...moduleRoutes(tracks), + // Rapport Insights glossary: generic list, custom add/edit page (formula builder + test). + { path: '/insights/glossarys', element: }, + { path: '/insights/glossary/addnew', element: }, + { path: '/insights/glossary/edit', element: }, { path: '/metricreports', element: }, { path: '/metricreport/report', element: }, { path: '/neblogisticreports', element: }, diff --git a/src/views/insights/GlossaryForm.jsx b/src/views/insights/GlossaryForm.jsx new file mode 100644 index 0000000..3d91af3 --- /dev/null +++ b/src/views/insights/GlossaryForm.jsx @@ -0,0 +1,403 @@ +// Rapport Insights glossary: add / edit a term with a guided formula builder +// and a Test panel that runs the formula on live data for the admin's own +// scope. API: rapport/insights/functions/glossary.js (insights_glossary_admin +// is required for everything on this page). +import { useEffect, useMemo, useState } from 'react'; +import { Link, useNavigate, useSearchParams } from 'react-router'; +import { Alert, Badge, Button, Card, Col, Container, Form, Row, Spinner, Table } from 'react-bootstrap'; +import Select from 'react-select'; +import { signedGet, signedPost, isSuccess } from '@/api/client'; +import { useAuth } from '@/auth/AuthProvider'; +import PageBreadcrumb from '@/components/PageBreadcrumb'; + +const LIST = '/insights/glossarys'; + +const AGGS = [ + { value: 'sum', label: 'Total units' }, + { value: 'count', label: 'Number of records' }, + { value: 'avg', label: 'Average units per record' }, + { value: 'distinct_contacts', label: 'Contacts reached (distinct)' }, + { value: 'distinct_users', label: 'People active (distinct)' }, +]; +const SOURCES = [ + { value: 'metric', label: 'A metric' }, + { value: 'contacts', label: 'Number of contacts' }, + { value: 'users', label: 'Number of people' }, +]; +const FILTER_OPS = [ + { value: 'eq', label: 'is' }, + { value: 'neq', label: 'is not' }, + { value: 'in', label: 'is one of' }, +]; +const PERIODS = [ + ['last_month', 'Last month'], + ['this_month', 'This month'], + ['last_quarter', 'Last quarter'], + ['this_quarter', 'This quarter'], + ['last_30_days', 'Last 30 days'], + ['ytd', 'This year so far'], + ['last_fy', 'Last financial year'], +]; +const GROUPS = [ + ['', 'No grouping (one number)'], + ['position', 'By position'], + ['user', 'By person'], + ['month', 'By month (metrics only)'], + ['week', 'By week (metrics only)'], +]; + +const emptyOperand = (source = 'metric') => (source === 'metric' ? { source, metric_ids: [], agg: 'sum' } : { source, filters: [] }); +const emptyTerm = { name: '', synonyms: '', description: '', status: 'active', formula: { kind: 'value', numerator: emptyOperand(), as_percent: false } }; + +/** The formula as the API wants it (drops empty filters, splits "is one of" values). */ +function toApiFormula(f) { + const operand = (o) => { + if (o.source === 'metric') return { source: 'metric', metric_ids: o.metric_ids, agg: o.agg }; + const filters = (o.filters || []) + .filter((x) => x.field.trim()) + .map((x) => ({ + field: x.field.trim(), + op: x.op, + value: x.op === 'in' ? String(x.value).split(',').map((v) => v.trim()).filter(Boolean) : String(x.value).trim(), + })); + return filters.length ? { source: o.source, filters } : { source: o.source }; + }; + const out = { kind: f.kind, numerator: operand(f.numerator) }; + if (f.kind === 'ratio') { + out.denominator = operand(f.denominator || emptyOperand('contacts')); + out.as_percent = Boolean(f.as_percent); + } + return out; +} + +/** API formula → editable state. */ +function fromApiFormula(f) { + const operand = (o) => + !o ? emptyOperand('contacts') : o.source === 'metric' ? { ...o } : { source: o.source, filters: (o.filters || []).map((x) => ({ ...x, value: Array.isArray(x.value) ? x.value.join(', ') : x.value ?? '' })) }; + return { kind: f?.kind || 'value', numerator: operand(f?.numerator), denominator: f?.kind === 'ratio' ? operand(f.denominator) : undefined, as_percent: Boolean(f?.as_percent) }; +} + +function OperandEditor({ label, value, onChange, metrics, metricsLoading }) { + const metricOptions = useMemo(() => metrics.map((m) => ({ value: m.id, label: m.name })), [metrics]); + const selected = metricOptions.filter((o) => (value.metric_ids || []).includes(o.value)); + const unknown = (value.metric_ids || []).filter((id) => !metricOptions.some((o) => o.value === id)); + const setSource = (source) => onChange(emptyOperand(source)); + const setFilter = (i, patch) => onChange({ ...value, filters: value.filters.map((f, j) => (j === i ? { ...f, ...patch } : f)) }); + + return ( + + +
{label}
+ + Count what + setSource(e.target.value)} aria-label={`${label}: source`}> + {SOURCES.map((s) => ( + + ))} + + + {value.source === 'metric' ? ( + + + + Metric(s) +