@uxland/primary-shell 5.6.5 → 5.6.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxland/primary-shell",
3
- "version": "5.6.5",
3
+ "version": "5.6.6",
4
4
  "description": "Primaria Shell",
5
5
  "author": "UXLand <dev@uxland.es>",
6
6
  "homepage": "https://github.com/uxland/harmonix/tree/app#readme",
@@ -2,25 +2,37 @@ import { IActivityHistoryItem, IActivityHistoryGroup, IActivityHistorySubGroup }
2
2
  import { areSameDiagnostics } from "../../domain/validation/diagnostics/are-same-diagnostics";
3
3
  import { hasValidDiagnostics } from "../../domain/validation/diagnostics/has-valid-diagnostics";
4
4
 
5
- const isSameVisit = (item1: IActivityHistoryItem, item2: IActivityHistoryItem): boolean => {
6
- const sameProfessional =
7
- item1.professional?.id === item2.professional?.id &&
8
- item1.professional?.role?.id === item2.professional?.role?.id &&
9
- item1.professional?.speciality?.id === item2.professional?.speciality?.id;
10
- const sameLocation =
11
- item1.ep?.id === item2.ep?.id &&
12
- item1.up?.id === item2.up?.id &&
13
- item1.center?.id === item2.center?.id &&
14
- item1.service?.id === item2.service?.id;
15
-
16
- const sameDay = new Date(item1.date).toDateString() === new Date(item2.date).toDateString();
17
-
18
- return sameProfessional && sameLocation && sameDay;
5
+ // Cache para evitar recalcular fechas
6
+ const dateStringCache = new Map<string, string>();
7
+ const dateObjectCache = new Map<string, Date>();
8
+
9
+ const getDateString = (dateStr: string): string => {
10
+ if (!dateStringCache.has(dateStr)) {
11
+ dateStringCache.set(dateStr, new Date(dateStr).toDateString());
12
+ }
13
+ return dateStringCache.get(dateStr)!;
14
+ };
15
+
16
+ const getDateObject = (dateStr: string): Date => {
17
+ if (!dateObjectCache.has(dateStr)) {
18
+ dateObjectCache.set(dateStr, new Date(dateStr));
19
+ }
20
+ return dateObjectCache.get(dateStr)!;
21
+ };
22
+
23
+ // Crear una clave única para cada combinación de visita
24
+ const createVisitKey = (item: IActivityHistoryItem): string => {
25
+ const professionalKey = `${item.professional?.id || 'null'}-${item.professional?.role?.id || 'null'}-${item.professional?.speciality?.id || 'null'}`;
26
+ const locationKey = `${item.ep?.id || 'null'}-${item.up?.id || 'null'}-${item.center?.id || 'null'}-${item.service?.id || 'null'}`;
27
+ const dayKey = getDateString(item.date);
28
+
29
+ return `${professionalKey}|${locationKey}|${dayKey}`;
19
30
  };
20
31
 
21
32
  const withinEightHours = (date1: string, date2: string): boolean => {
22
- const timeDifference = Math.abs(new Date(date1).getTime() - new Date(date2).getTime());
23
- return timeDifference <= 8 * 60 * 60 * 1000; // 8 hours in milliseconds
33
+ const time1 = getDateObject(date1).getTime();
34
+ const time2 = getDateObject(date2).getTime();
35
+ return Math.abs(time1 - time2) <= 8 * 60 * 60 * 1000; // 8 hours in milliseconds
24
36
  };
25
37
 
26
38
  function groupByValidDiagnostics(groups: IActivityHistoryGroup[]) {
@@ -30,12 +42,12 @@ function groupByValidDiagnostics(groups: IActivityHistoryGroup[]) {
30
42
 
31
43
  group.items.forEach((item) => {
32
44
  const diagnostics = item.diagnostics;
33
-
34
45
  const allDiagnosticsValid = diagnostics?.length > 0 && hasValidDiagnostics(diagnostics);
35
46
 
36
47
  if (allDiagnosticsValid) {
37
48
  let addedToSubGroup = false;
38
49
 
50
+ // Optimización: usar for...of con break para salir temprano
39
51
  for (const subGroup of subGroups) {
40
52
  const firstSubGroupItem = subGroup.items[0];
41
53
 
@@ -63,38 +75,59 @@ function groupByValidDiagnostics(groups: IActivityHistoryGroup[]) {
63
75
  }
64
76
 
65
77
  const groupActivityHistoryItems = (items: IActivityHistoryItem[]): IActivityHistoryGroup[] => {
66
- const groups: IActivityHistoryGroup[] = [];
78
+ if (!items?.length) return [];
67
79
 
68
- items?.forEach((item) => {
69
- let added = false;
80
+ // Limpiar caches al inicio
81
+ dateStringCache.clear();
82
+ dateObjectCache.clear();
70
83
 
71
- for (const group of groups) {
72
- const firstItem = group.items[0];
73
- const lastItem = group.items[group.items.length - 1];
84
+ // Mapa para agrupar por clave de visita
85
+ const visitGroups = new Map<string, IActivityHistoryGroup>();
86
+
87
+ items.forEach((item) => {
88
+ const visitKey = createVisitKey(item);
89
+
90
+ if (!visitGroups.has(visitKey)) {
91
+ // Crear nuevo grupo
92
+ visitGroups.set(visitKey, {
93
+ idGroup: Math.random().toString(36).substr(2, 9),
94
+ items: [item],
95
+ });
96
+ } else {
97
+ // Verificar si está dentro del rango de 8 horas
98
+ const existingGroup = visitGroups.get(visitKey)!;
99
+ const firstItem = existingGroup.items[0];
100
+ const lastItem = existingGroup.items[existingGroup.items.length - 1];
74
101
 
75
102
  if (
76
- isSameVisit(firstItem, item) &&
77
103
  withinEightHours(firstItem.date, item.date) &&
78
104
  withinEightHours(lastItem.date, item.date)
79
105
  ) {
80
- group.items.push(item);
81
- added = true;
82
- break;
106
+ existingGroup.items.push(item);
107
+ } else {
108
+ // Si no está dentro del rango, crear un nuevo grupo con un sufijo
109
+ let counter = 1;
110
+ let newKey = `${visitKey}_${counter}`;
111
+
112
+ while (visitGroups.has(newKey)) {
113
+ counter++;
114
+ newKey = `${visitKey}_${counter}`;
115
+ }
116
+
117
+ visitGroups.set(newKey, {
118
+ idGroup: Math.random().toString(36).substr(2, 9),
119
+ items: [item],
120
+ });
83
121
  }
84
122
  }
85
-
86
- if (!added) {
87
- groups.push({
88
- idGroup: Math.random().toString(36).substr(2, 9),
89
- items: [item],
90
- });
91
- }
92
123
  });
93
124
 
94
- // Crear subgrupos por diagnósticos iguales. Los items que no tienen diagnostico, se quedan fuera de subgrupos (remaining items)
125
+ const groups = Array.from(visitGroups.values());
126
+
127
+ // Crear subgrupos por diagnósticos iguales
95
128
  groupByValidDiagnostics(groups);
96
129
 
97
130
  return groups;
98
131
  };
99
132
 
100
- export { groupActivityHistoryItems };
133
+ export { groupActivityHistoryItems };