data-primals-engine 1.4.3 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +913 -867
  2. package/client/package-lock.json +49 -0
  3. package/client/package.json +1 -0
  4. package/client/src/AddWidgetTypeModal.jsx +47 -43
  5. package/client/src/App.jsx +3 -7
  6. package/client/src/App.scss +25 -3
  7. package/client/src/AssistantChat.jsx +363 -323
  8. package/client/src/AssistantChat.scss +30 -12
  9. package/client/src/Dashboard.jsx +480 -396
  10. package/client/src/Dashboard.scss +1 -1
  11. package/client/src/DashboardHtmlViewItem.jsx +147 -0
  12. package/client/src/DashboardView.jsx +104 -19
  13. package/client/src/DataEditor.jsx +12 -5
  14. package/client/src/DataLayout.jsx +805 -762
  15. package/client/src/DataLayout.scss +14 -0
  16. package/client/src/DataTable.jsx +63 -77
  17. package/client/src/Dialog.scss +1 -1
  18. package/client/src/Field.jsx +591 -322
  19. package/client/src/FlexDataRenderer.jsx +2 -0
  20. package/client/src/FlexTreeUtils.js +1 -1
  21. package/client/src/FlexViewCard.jsx +44 -0
  22. package/client/src/HistoryDialog.jsx +47 -14
  23. package/client/src/HtmlViewBuilderModal.jsx +91 -0
  24. package/client/src/HtmlViewBuilderModal.scss +18 -0
  25. package/client/src/HtmlViewCard.jsx +44 -0
  26. package/client/src/HtmlViewCard.scss +35 -0
  27. package/client/src/KPIDialog.jsx +11 -1
  28. package/client/src/KanbanCard.jsx +1 -2
  29. package/client/src/ModelCreator.jsx +6 -6
  30. package/client/src/ModelCreatorField.jsx +74 -31
  31. package/client/src/ModelList.jsx +93 -54
  32. package/client/src/Notification.jsx +136 -136
  33. package/client/src/Notification.scss +0 -18
  34. package/client/src/Pagination.jsx +5 -3
  35. package/client/src/RelationField.jsx +354 -258
  36. package/client/src/RelationSelectorWidget.jsx +173 -0
  37. package/client/src/constants.js +1 -1
  38. package/client/src/contexts/ModelContext.jsx +10 -1
  39. package/client/src/contexts/UIContext.jsx +72 -63
  40. package/client/src/filter.js +262 -212
  41. package/client/src/hooks/useTutorials.jsx +62 -65
  42. package/client/src/hooks/useValidation.js +75 -0
  43. package/client/src/translations.js +26 -24
  44. package/package.json +3 -1
  45. package/perf/README.md +147 -0
  46. package/perf/artillery-hooks.js +37 -0
  47. package/perf/perf-shot-hardwork.yml +84 -0
  48. package/perf/perf-shot-search.yml +45 -0
  49. package/perf/setup.yml +26 -0
  50. package/server.js +1 -1
  51. package/src/constants.js +3 -28
  52. package/src/core.js +15 -1
  53. package/src/data.js +1 -1
  54. package/src/defaultModels.js +63 -7
  55. package/src/email.js +5 -2
  56. package/src/engine.js +5 -3
  57. package/src/filter.js +5 -3
  58. package/src/i18n.js +710 -10
  59. package/src/modules/assistant/assistant.js +151 -19
  60. package/src/modules/bucket.js +14 -16
  61. package/src/modules/data/data.backup.js +11 -8
  62. package/src/modules/data/data.core.js +118 -92
  63. package/src/modules/data/data.history.js +531 -492
  64. package/src/modules/data/data.js +9 -56
  65. package/src/modules/data/data.operations.js +3282 -2999
  66. package/src/modules/data/data.relations.js +686 -686
  67. package/src/modules/data/data.routes.js +118 -24
  68. package/src/modules/data/data.scheduling.js +2 -1
  69. package/src/modules/data/data.validation.js +85 -3
  70. package/src/modules/file.js +247 -236
  71. package/src/modules/user.js +4 -1
  72. package/src/modules/workflow.js +9 -10
  73. package/src/openai.jobs.js +2 -0
  74. package/src/packs.js +5482 -5461
  75. package/src/providers.js +22 -7
  76. package/test/data.integration.test.js +136 -2
  77. package/test/import_export.integration.test.js +1 -1
@@ -173,7 +173,7 @@
173
173
  display: flex;
174
174
  align-items: center;
175
175
  gap: 0.5rem;
176
- margin-bottom: 1rem;
176
+ margin-bottom: 0;
177
177
  border-bottom: 1px solid #eee;
178
178
  padding-bottom: 0.5rem;
179
179
 
@@ -0,0 +1,147 @@
1
+ import React, { useEffect, useMemo } from 'react';
2
+ import { useQuery } from 'react-query';
3
+ import { useTranslation } from 'react-i18next';
4
+ import { FaSpinner } from 'react-icons/fa';
5
+ import Handlebars from 'handlebars';
6
+ import './HtmlViewCard.scss';
7
+ import { useModelContext } from "./contexts/ModelContext.jsx";
8
+
9
+ /**
10
+ * Pre-processes data to make it compatible with templates generated by the AI.
11
+ * - Adds a `.value` property to translated fields (string_t, richtext_t).
12
+ * - Recursively processes relations.
13
+ * @param {object|Array} data - The data to process.
14
+ * @param {object} model - The model definition for the data.
15
+ * @param {Array} allModels - All available models.
16
+ * @param {object} tr - The translation object { t, i18n }.
17
+ * @returns {object|Array} The processed data.
18
+ */
19
+ const preprocessDataForHandlebars = (data, model, allModels, tr) => {
20
+ if (!data || !model) return data;
21
+
22
+ if (Array.isArray(data)) {
23
+ return data.map(item => preprocessDataForHandlebars(item, model, allModels, tr));
24
+ }
25
+
26
+ // Create a shallow copy to avoid mutating the original data from react-query cache
27
+ const processedData = { ...data };
28
+
29
+ for (const key in processedData) {
30
+ if (!Object.prototype.hasOwnProperty.call(processedData, key)) continue;
31
+
32
+ const fieldDef = model.fields.find(f => f.name === key);
33
+ if (!fieldDef) continue;
34
+
35
+ const value = processedData[key];
36
+
37
+ if ((fieldDef.type === 'string_t' || fieldDef.type === 'richtext_t') && typeof value === 'object' && value !== null) {
38
+ const lang = (tr.i18n.resolvedLanguage || tr.i18n.language).split(/[-_]/)?.[0];
39
+ // Add the `.value` property the AI expects, making it non-enumerable to avoid issues
40
+ Object.defineProperty(value, 'value', {
41
+ value: value[lang] || value.en || value.fr || Object.values(value)[0] || '',
42
+ writable: true,
43
+ configurable: true,
44
+ enumerable: false // Important!
45
+ });
46
+ } else if (fieldDef.type === 'relation' && value) {
47
+ const relatedModel = allModels.find(m => m.name === fieldDef.relation);
48
+ if (relatedModel) {
49
+ // Recursively process the related data
50
+ processedData[key] = preprocessDataForHandlebars(value, relatedModel, allModels, tr);
51
+ }
52
+ }
53
+ }
54
+ return processedData;
55
+ };
56
+
57
+ /**
58
+ * Renders a simple HTML template by substituting placeholders.
59
+ * Uses Handlebars.js for robust templating.
60
+ *
61
+ * @param {string} templateString The template with placeholders.
62
+ * @param {Array<object>} data The array of data objects to inject.
63
+ * @param {object} options - Additional options including model, allModels, and tr.
64
+ * @returns {string} The rendered HTML string.
65
+ */
66
+ export const renderHtmlTemplate = (templateString, data, options = {}) => {
67
+ const { model, allModels, tr } = options;
68
+ if (!templateString) return '';
69
+
70
+ try {
71
+ const processedData = preprocessDataForHandlebars(data, model, allModels, tr);
72
+ const template = Handlebars.compile(templateString);
73
+ return template({ data: processedData });
74
+ } catch (e) {
75
+ console.error("Handlebars rendering error:", e);
76
+ return `<div class="error-state">Template Error: ${e.message}</div>`;
77
+ }
78
+ };
79
+
80
+ /**
81
+ * Récupère les données et affiche une vue HTML personnalisée sur le tableau de bord.
82
+ * @param {object} props
83
+ * @param {object} props.config - La configuration de la vue depuis la disposition du tableau de bord.
84
+ */
85
+ const DashboardHtmlViewItem = ({ config }) => {
86
+ const { t, i18n } = useTranslation();
87
+ const { models } = useModelContext();
88
+ const { title, model: modelName, filter, sort, limit, template, css } = config;
89
+
90
+ // Génère un ID unique et stable pour ce composant
91
+ const containerId = useMemo(() => `html-view-dashboard-${Math.random().toString(36).substring(2, 9)}`, []);
92
+ const model = useMemo(() => models.find(m => m.name === modelName), [models, modelName]);
93
+
94
+ const { data: queryResult, isLoading, error } = useQuery(
95
+ ['dashboardHtmlView', modelName, filter, sort, limit],
96
+ async () => {
97
+ const response = await fetch(`/api/data/search?model=${modelName}&depth=2&limit=${limit || 10}&sort=${JSON.stringify(sort || {})}`, {
98
+ method: 'POST',
99
+ headers: { 'Content-Type': 'application/json' },
100
+ body: JSON.stringify({ filter: filter || {}}),
101
+ });
102
+ if (!response.ok) throw new Error('Échec de la récupération des données pour la vue HTML');
103
+ return response.json();
104
+ },
105
+ {
106
+ enabled: !!modelName && !!template,
107
+ refetchOnWindowFocus: false,
108
+ }
109
+ );
110
+
111
+ // Effet pour injecter et nettoyer le CSS
112
+ useEffect(() => {
113
+ if (!css) return;
114
+
115
+ const styleElement = document.createElement('style');
116
+ // Remplace le placeholder par notre ID unique pour scoper le CSS
117
+ styleElement.innerHTML = css.replace(/\{\{containerId\}\}/g, containerId);
118
+ document.head.appendChild(styleElement);
119
+
120
+ // Fonction de nettoyage : retire le style quand le composant est démonté
121
+ return () => {
122
+ document.head.removeChild(styleElement);
123
+ };
124
+ }, [css, containerId]);
125
+
126
+ const renderContent = () => {
127
+ if (isLoading) return <div className="loading-state"><FaSpinner className="spin-icon" /> {t('loading', 'Chargement...')}</div>;
128
+ if (error) return <div className="error-state">{t('error.generic', 'Erreur: {{message}}', { message: error.message })}</div>;
129
+ if (!queryResult || !queryResult.data) return <div className="empty-state">{t('dashboards.noDataForView', 'Aucune donnée à afficher.')}</div>;
130
+
131
+ const renderedHtml = renderHtmlTemplate(template, queryResult.data, {
132
+ model: model,
133
+ allModels: models,
134
+ tr: { t, i18n }
135
+ });
136
+ return <div className="html-view-content" dangerouslySetInnerHTML={{ __html: renderedHtml }} />;
137
+ };
138
+
139
+ return (
140
+ <div id={containerId} className="html-view-card dashboard-widget">
141
+ {title && <h4>{title}</h4>}
142
+ {renderContent()}
143
+ </div>
144
+ );
145
+ };
146
+
147
+ export default DashboardHtmlViewItem;
@@ -14,6 +14,8 @@ import { useAuthContext } from "./contexts/AuthContext.jsx";
14
14
  import { DialogProvider } from "./Dialog.jsx";
15
15
  import {useModelContext} from "./contexts/ModelContext.jsx";
16
16
  import {DashboardFlexViewItem} from "./DashboardFlexViewItem.jsx";
17
+ import DashboardHtmlViewItem from "./DashboardHtmlViewItem.jsx";
18
+ import HtmlViewBuilderModal from "./HtmlViewBuilderModal.jsx";
17
19
 
18
20
  // --- updateDashboardLayout (fonction utilitaire, peut rester ici ou être externalisée) ---
19
21
  async function updateDashboardLayout(dashboard, newLayoutData, username, t) {
@@ -65,6 +67,8 @@ export function DashboardView({ dashboard }) {
65
67
  const queryClient = useQueryClient();
66
68
  const { models } = useModelContext();
67
69
 
70
+ const [editingHtmlViewConfig, setEditingHtmlViewConfig] = useState(null);
71
+ const [isHtmlViewBuilderModalOpen, setIsHtmlViewBuilderModalOpen] = useState(false);
68
72
  const [layoutState, setLayoutState] = useState([]);
69
73
  const [editingSectionIndex, setEditingSectionIndex] = useState(null);
70
74
  const [originalSectionName, setOriginalSectionName] = useState('');
@@ -101,41 +105,52 @@ export function DashboardView({ dashboard }) {
101
105
  }
102
106
  );
103
107
  const processedDashboardId = useRef(null);
108
+ const processedLayoutRef = useRef(null); // Pour comparer le contenu du layout
104
109
 
105
110
  useEffect(() => {
106
111
  if (!dashboard) return;
107
- if (dashboard._id === processedDashboardId.current) return;
112
+
113
+ const layoutString = JSON.stringify(dashboard.layout);
114
+ // On ne met à jour que si l'ID du dashboard ou le contenu de son layout a changé.
115
+ if (dashboard._id === processedDashboardId.current && layoutString === processedLayoutRef.current) return;
108
116
 
109
117
  let parsedLayout = [];
110
118
 
111
- if (dashboard?.layout) {
112
- try {
113
- parsedLayout = dashboard.layout.map(section => ({
114
- ...section,
115
- kpis: section.kpis || section.kpiIds || [], // Normalisation cohérente
116
- chartConfigs: section.chartConfigs || [],
117
- flexViews: section.flexViews || []
118
- }));
119
- } catch (e) {
120
- console.error("Failed to parse layout", e);
121
- parsedLayout = [{
122
- name: t('dashboards.defaultSectionName'),
123
- kpis: [],
124
- chartConfigs: [],
125
- flexViews: []
126
- }];
127
- }
119
+ // The layout from the DB can be an object (old default) or an array of sections (new format).
120
+ // We need to handle both cases to avoid crashes.
121
+ if (dashboard?.layout && Array.isArray(dashboard.layout)) {
122
+ try {
123
+ parsedLayout = dashboard.layout.map(section => ({
124
+ ...section,
125
+ kpis: section.kpis || section.kpiIds || [], // Normalisation cohérente
126
+ chartConfigs: section.chartConfigs || [],
127
+ flexViews: section.flexViews || [],
128
+ htmlViews: section.htmlViews || []
129
+ }));
130
+ } catch (e) {
131
+ console.error("Failed to parse layout array", e);
132
+ // Fallback to a default section if mapping fails for some reason
133
+ parsedLayout = [{
134
+ name: t('dashboards.defaultSectionName'),
135
+ kpis: [],
136
+ chartConfigs: [],
137
+ flexViews: [],
138
+ htmlViews: []
139
+ }];
140
+ }
128
141
  } else {
129
142
  parsedLayout = [{
130
143
  name: t('dashboards.defaultSectionName'),
131
144
  kpis: [],
132
145
  chartConfigs: [],
133
- flexViews: []
146
+ flexViews: [],
147
+ htmlViews: []
134
148
  }];
135
149
  }
136
150
 
137
151
  setLayoutState(parsedLayout);
138
152
  processedDashboardId.current = dashboard._id;
153
+ processedLayoutRef.current = layoutString;
139
154
  }, [dashboard, t]);
140
155
 
141
156
  // Gère le rafraîchissement automatique des données du dashboard.
@@ -197,9 +212,45 @@ export function DashboardView({ dashboard }) {
197
212
  setIsChartModalOpen(true);
198
213
  } else if (type === 'FlexView') { // Gérer le type FlexView
199
214
  setIsFlexBuilderModalOpen(true);
215
+ } else if (type === 'HtmlView') {
216
+ setIsHtmlViewBuilderModalOpen(true); // On peut réutiliser le même modal pour l'instant
200
217
  }
201
218
  };
202
219
 
220
+ const handleOpenEditHtmlViewModal = (htmlViewToEdit, sectionIndex) => {
221
+ setEditingHtmlViewConfig(htmlViewToEdit);
222
+ setAddingToSectionIndex(sectionIndex);
223
+ setIsHtmlViewBuilderModalOpen(true);
224
+ };
225
+
226
+ const handleSaveHtmlViewConfig = (config) => {
227
+ if (addingToSectionIndex === null) return;
228
+ const newLayoutState = JSON.parse(JSON.stringify(layoutState));
229
+ const targetSection = newLayoutState[addingToSectionIndex];
230
+ if (!targetSection) return;
231
+ if (!Array.isArray(targetSection.htmlViews)) targetSection.htmlViews = [];
232
+
233
+ if (config.id) { // Edition
234
+ const index = targetSection.htmlViews.findIndex(v => v.id === config.id);
235
+ if (index !== -1) targetSection.htmlViews[index] = config;
236
+ } else { // Ajout
237
+ targetSection.htmlViews.push({ ...config, id: `html-${Date.now()}` });
238
+ }
239
+ mutation.mutate(newLayoutState);
240
+ handleCloseFlexBuilderModal();
241
+ setIsHtmlViewBuilderModalOpen(false);
242
+ setEditingHtmlViewConfig(null);
243
+ };
244
+
245
+ const handleRemoveHtmlView = (htmlViewId, sectionIndex) => {
246
+ const newLayoutState = JSON.parse(JSON.stringify(layoutState));
247
+ if (newLayoutState[sectionIndex]?.htmlViews) {
248
+ newLayoutState[sectionIndex].htmlViews = newLayoutState[sectionIndex].htmlViews.filter(hv => hv.id !== htmlViewId);
249
+ mutation.mutate(newLayoutState);
250
+ }
251
+ };
252
+
253
+
203
254
 
204
255
  const handleAddKpi = (kpiDefinition) => {
205
256
  if (addingToSectionIndex === null || !layoutState[addingToSectionIndex]) return;
@@ -396,6 +447,8 @@ export function DashboardView({ dashboard }) {
396
447
 
397
448
  chartConfigs: section.chartConfigs || [],
398
449
 
450
+ htmlViews: section.htmlViews || [],
451
+
399
452
  flexViews: section.flexViews || [],
400
453
  };
401
454
  });
@@ -437,6 +490,18 @@ export function DashboardView({ dashboard }) {
437
490
  models={models} // Passer les modèles pour la configuration du graphique
438
491
  />
439
492
  )}
493
+ {isHtmlViewBuilderModalOpen && (
494
+ <HtmlViewBuilderModal
495
+ isOpen={isHtmlViewBuilderModalOpen}
496
+ onClose={() => {
497
+ setIsHtmlViewBuilderModalOpen(false);
498
+ setEditingHtmlViewConfig(null);
499
+ }}
500
+ onSave={handleSaveHtmlViewConfig}
501
+ models={models}
502
+ initialConfig={editingHtmlViewConfig}
503
+ />
504
+ )}
440
505
  </DialogProvider>
441
506
 
442
507
  {isFlexBuilderModalOpen && (
@@ -450,6 +515,7 @@ export function DashboardView({ dashboard }) {
450
515
  // not for the data displayed on the dashboard itself.
451
516
  />
452
517
  )}
518
+
453
519
  {dashboard && (
454
520
  <>
455
521
  <h2>{dashboard.name.value}</h2>
@@ -500,6 +566,25 @@ export function DashboardView({ dashboard }) {
500
566
  </div>
501
567
  </div>
502
568
  ))}
569
+
570
+ {/* NOUVEAU: Affichage des vues HTML */}
571
+ {sectionData.htmlViews?.map(htmlConfig => (
572
+ <div key={htmlConfig.id} className="dashboard-item-wrapper html-view-wrapper">
573
+ <DashboardHtmlViewItem config={htmlConfig} />
574
+ <div className="item-actions">
575
+ {/* TODO: Add edit button for HTML views */}
576
+ {/* <button ...><FaPencilAlt /></button> */}
577
+ <button className="edit-item-button"
578
+ onClick={() => handleOpenEditHtmlViewModal(htmlConfig, sectionIndex)}
579
+ title={t('dashboards.editHtmlViewTitle', 'Modifier cette vue HTML')}
580
+ disabled={mutation.isLoading}><FaPencilAlt/></button>
581
+ <button className="remove-item-button"
582
+ onClick={() => handleRemoveHtmlView(htmlConfig.id, sectionIndex)}
583
+ title={t('dashboards.removeHtmlViewTitle', 'Supprimer cette vue HTML')}
584
+ disabled={mutation.isLoading}><FaTrash/></button>
585
+ </div>
586
+ </div>
587
+ ))}
503
588
  {/* Affichage des FlexViews */}
504
589
  {sectionData.flexViews?.map(flexViewConfig => (
505
590
 
@@ -60,6 +60,8 @@ export const DataEditor = forwardRef(function MyDataEditor({
60
60
  model,
61
61
  onSubmit,
62
62
  refreshTime,
63
+ onCancel,
64
+ hideNewButton,
63
65
  formData,
64
66
  setFormData, record, setRecord}, ref){
65
67
 
@@ -88,9 +90,9 @@ export const DataEditor = forwardRef(function MyDataEditor({
88
90
  // --- Le reste de la fonction renderInputField reste identique ---
89
91
  switch (field.type) {
90
92
  case 'model':
91
- return <ModelField value={value} onChange={handleChange} model={model} field={field} />;
93
+ return <ModelField value={value} onChange={handleChange} model={model} field={field} formData={formData} />;
92
94
  case 'modelField':
93
- return <ModelField fields={true} value={value} onChange={handleChange} model={model} field={field} />;
95
+ return <ModelField fields={true} value={value} onChange={handleChange} model={model} field={field} formData={formData} />;
94
96
  case 'datetime':
95
97
  case 'date':
96
98
  if( field.min )
@@ -182,6 +184,8 @@ export const DataEditor = forwardRef(function MyDataEditor({
182
184
  />)}</>
183
185
  )}</div>
184
186
  }
187
+
188
+ {console.log(formData[field.name])}
185
189
  return <CodeField key={field.name} language={field.language} name={inputProps.name}
186
190
  value={typeof (value) === 'string' ? value : JSON.stringify(value)}
187
191
  onChange={handleChange}/>
@@ -189,7 +193,7 @@ export const DataEditor = forwardRef(function MyDataEditor({
189
193
  case 'array':
190
194
  if (field.itemsType === 'file') {
191
195
  return <div key={field.name}><FileField name={field.name} maxSize={field.maxSize}
192
- mimeTypes={field.mimeTypes} value={value} onChange={(files) => {
196
+ mimeTypes={field.mimeTypes} multiple value={value} onChange={(files) => {
193
197
  handleChange({name: field.name, value: files});
194
198
  }} /></div>;
195
199
  } else {
@@ -276,6 +280,8 @@ export const DataEditor = forwardRef(function MyDataEditor({
276
280
  key={field.name}
277
281
  type={getInputType(field.type)} {...inputProps}
278
282
  value={displayValue}
283
+ mask={field.mask}
284
+ replacement={field.replacement}
279
285
  onChange={(e) => handleChange({name: field.name, value: e.target.value})} />
280
286
  }
281
287
  case 'file':
@@ -368,8 +374,9 @@ export const DataEditor = forwardRef(function MyDataEditor({
368
374
  ) : null; // Ne rien afficher si la condition n'est pas remplie
369
375
  })}
370
376
  <div className="flex flex-centered">
371
- <Button type="submit" disabled={isLoading}><Trans i18nKey="btns.save">Enregistrer</Trans></Button>
372
- <Button type="submit" onClick={handleNew}><Trans i18nKey="btns.new">Nouveau</Trans></Button>
377
+ <Button type="button" onClick={handleSubmit} disabled={isLoading}><Trans i18nKey="btns.save">Enregistrer</Trans></Button>
378
+ {onCancel && <Button type="button" className="btn-secondary" onClick={onCancel}><Trans i18nKey="btns.cancel">Annuler</Trans></Button>}
379
+ {!hideNewButton && <Button type="button" onClick={handleNew}><Trans i18nKey="btns.new">Nouveau</Trans></Button>}
373
380
  </div>
374
381
  </form>
375
382
  </div>