@blackcode_sa/metaestetics-api 1.12.67 → 1.13.0

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 (47) hide show
  1. package/dist/admin/index.d.mts +801 -2
  2. package/dist/admin/index.d.ts +801 -2
  3. package/dist/admin/index.js +2332 -153
  4. package/dist/admin/index.mjs +2321 -153
  5. package/dist/backoffice/index.d.mts +40 -0
  6. package/dist/backoffice/index.d.ts +40 -0
  7. package/dist/backoffice/index.js +118 -18
  8. package/dist/backoffice/index.mjs +118 -20
  9. package/dist/index.d.mts +1097 -2
  10. package/dist/index.d.ts +1097 -2
  11. package/dist/index.js +4224 -2091
  12. package/dist/index.mjs +3941 -1821
  13. package/package.json +1 -1
  14. package/src/admin/aggregation/appointment/appointment.aggregation.service.ts +140 -0
  15. package/src/admin/analytics/analytics.admin.service.ts +278 -0
  16. package/src/admin/analytics/index.ts +2 -0
  17. package/src/admin/index.ts +6 -0
  18. package/src/backoffice/services/README.md +17 -0
  19. package/src/backoffice/services/analytics.service.proposal.md +863 -0
  20. package/src/backoffice/services/analytics.service.summary.md +143 -0
  21. package/src/backoffice/services/category.service.ts +49 -6
  22. package/src/backoffice/services/subcategory.service.ts +50 -6
  23. package/src/backoffice/services/technology.service.ts +53 -6
  24. package/src/services/analytics/ARCHITECTURE.md +199 -0
  25. package/src/services/analytics/CLOUD_FUNCTIONS.md +225 -0
  26. package/src/services/analytics/GROUPED_ANALYTICS.md +501 -0
  27. package/src/services/analytics/QUICK_START.md +393 -0
  28. package/src/services/analytics/README.md +287 -0
  29. package/src/services/analytics/SUMMARY.md +141 -0
  30. package/src/services/analytics/USAGE_GUIDE.md +518 -0
  31. package/src/services/analytics/analytics-cloud.service.ts +222 -0
  32. package/src/services/analytics/analytics.service.ts +1632 -0
  33. package/src/services/analytics/index.ts +3 -0
  34. package/src/services/analytics/utils/appointment-filtering.utils.ts +138 -0
  35. package/src/services/analytics/utils/cost-calculation.utils.ts +154 -0
  36. package/src/services/analytics/utils/grouping.utils.ts +394 -0
  37. package/src/services/analytics/utils/stored-analytics.utils.ts +347 -0
  38. package/src/services/analytics/utils/time-calculation.utils.ts +186 -0
  39. package/src/services/appointment/appointment.service.ts +50 -6
  40. package/src/services/index.ts +1 -0
  41. package/src/services/procedure/procedure.service.ts +3 -3
  42. package/src/types/analytics/analytics.types.ts +500 -0
  43. package/src/types/analytics/grouped-analytics.types.ts +148 -0
  44. package/src/types/analytics/index.ts +4 -0
  45. package/src/types/analytics/stored-analytics.types.ts +137 -0
  46. package/src/types/index.ts +3 -0
  47. package/src/types/notifications/index.ts +21 -0
@@ -0,0 +1,199 @@
1
+ # Analytics Service Architecture
2
+
3
+ ## Overview
4
+
5
+ The Analytics Service uses a **hybrid approach** combining **pre-computed analytics** (stored in Firestore) with **on-demand calculation** (fallback). This architecture optimizes for:
6
+
7
+ - **Performance**: Fast reads from pre-computed data
8
+ - **Cost**: Reduced Firestore reads and compute time
9
+ - **Flexibility**: Can still calculate on-demand when needed
10
+
11
+ ## Architecture Components
12
+
13
+ ### 1. Cloud Functions (Pre-computation)
14
+
15
+ **Location**: `Cloud/functions/src/analytics/computeAnalytics.ts`
16
+
17
+ **Schedule**: Runs every 12 hours via Cloud Scheduler
18
+
19
+ **What it does**:
20
+ - Queries all appointments for each clinic
21
+ - Computes analytics for multiple periods (daily, weekly, monthly, yearly, all_time)
22
+ - Stores computed analytics in Firestore subcollections
23
+
24
+ **Storage Structure**:
25
+ ```
26
+ clinics/{clinicBranchId}/
27
+ └── analytics/
28
+ ├── dashboard/
29
+ │ └── {period}/
30
+ │ └── current
31
+ ├── clinic/
32
+ │ └── {period}/
33
+ │ └── current
34
+ ├── practitioners/
35
+ │ └── {period}/
36
+ │ └── {practitionerId}
37
+ ├── procedures/
38
+ │ └── {period}/
39
+ │ └── {procedureId}
40
+ ├── time_efficiency/
41
+ │ └── {period}/
42
+ │ └── current
43
+ ├── revenue/
44
+ │ └── {period}/
45
+ │ └── current
46
+ ├── cancellations/
47
+ │ └── {period}/
48
+ │ └── clinic
49
+ └── no_shows/
50
+ └── {period}/
51
+ └── clinic
52
+ ```
53
+
54
+ ### 2. Analytics Service (Client-side)
55
+
56
+ **Location**: `Api/src/services/analytics/analytics.service.ts`
57
+
58
+ **Behavior**:
59
+ 1. **First**: Tries to read from stored analytics
60
+ 2. **Checks**: If data is fresh (within maxCacheAgeHours, default 12 hours)
61
+ 3. **Falls back**: Calculates on-demand if:
62
+ - No stored data exists
63
+ - Data is stale (older than maxCacheAgeHours)
64
+ - `useCache: false` is specified
65
+
66
+ ### 3. Storage Types
67
+
68
+ **Location**: `Api/src/types/analytics/stored-analytics.types.ts`
69
+
70
+ Defines types for stored analytics documents, including metadata about when and how they were computed.
71
+
72
+ ## Data Flow
73
+
74
+ ### Pre-computation Flow (Cloud Function)
75
+
76
+ ```
77
+ Cloud Scheduler (every 12 hours)
78
+
79
+ computeAnalyticsForAllClinics()
80
+
81
+ For each clinic:
82
+ ├── Compute dashboard analytics
83
+ ├── Compute clinic analytics
84
+ ├── Compute practitioner analytics (for each practitioner)
85
+ ├── Compute procedure analytics (for each procedure)
86
+ ├── Compute time efficiency metrics
87
+ ├── Compute revenue metrics
88
+ ├── Compute cancellation metrics
89
+ └── Compute no-show metrics
90
+
91
+ Store in Firestore subcollections
92
+ ```
93
+
94
+ ### Client Read Flow
95
+
96
+ ```
97
+ Client requests analytics
98
+
99
+ AnalyticsService.getDashboardData()
100
+
101
+ Check stored analytics?
102
+ ├── Yes → Read from Firestore
103
+ │ ├── Fresh? → Return cached data ✅
104
+ │ └── Stale? → Calculate on-demand
105
+ └── No → Calculate on-demand
106
+ ```
107
+
108
+ ## Benefits
109
+
110
+ ### Performance
111
+ - **Fast reads**: Single document read vs. querying hundreds/thousands of appointments
112
+ - **Reduced latency**: Pre-computed data returns instantly
113
+ - **Scalable**: Works efficiently even with large datasets
114
+
115
+ ### Cost Optimization
116
+ - **Fewer reads**: 1 read vs. potentially hundreds/thousands
117
+ - **Reduced compute**: Calculations run server-side, not on every client request
118
+ - **Predictable costs**: Fixed cost per clinic per period
119
+
120
+ ### Flexibility
121
+ - **On-demand fallback**: Can still calculate when needed
122
+ - **Custom date ranges**: Can override cached data for specific queries
123
+ - **Real-time option**: Can disable caching for live data
124
+
125
+ ## Usage Examples
126
+
127
+ ### Using Pre-computed Analytics (Default)
128
+
129
+ ```typescript
130
+ // Automatically uses cached data if available and fresh
131
+ const dashboard = await analyticsService.getDashboardData(
132
+ { clinicBranchId: 'clinic-123' },
133
+ { start: new Date('2024-01-01'), end: new Date('2024-12-31') }
134
+ );
135
+ ```
136
+
137
+ ### Forcing On-demand Calculation
138
+
139
+ ```typescript
140
+ // Bypass cache and calculate on-demand
141
+ const dashboard = await analyticsService.getDashboardData(
142
+ { clinicBranchId: 'clinic-123' },
143
+ { start: new Date('2024-01-01'), end: new Date('2024-12-31') },
144
+ { useCache: false }
145
+ );
146
+ ```
147
+
148
+ ### Custom Cache Age
149
+
150
+ ```typescript
151
+ // Use cache if data is less than 6 hours old (instead of default 12)
152
+ const dashboard = await analyticsService.getDashboardData(
153
+ { clinicBranchId: 'clinic-123' },
154
+ { start: new Date('2024-01-01'), end: new Date('2024-12-31') },
155
+ { maxCacheAgeHours: 6 }
156
+ );
157
+ ```
158
+
159
+ ## Configuration
160
+
161
+ ### Cloud Function Schedule
162
+
163
+ Edit `Cloud/functions/src/analytics/computeAnalytics.ts`:
164
+
165
+ ```typescript
166
+ schedule: "every 12 hours" // Change to "every 6 hours", "every 24 hours", etc.
167
+ ```
168
+
169
+ ### Default Cache Age
170
+
171
+ Edit `Api/src/services/analytics/utils/stored-analytics.utils.ts`:
172
+
173
+ ```typescript
174
+ maxCacheAgeHours = 12 // Change default cache age
175
+ ```
176
+
177
+ ## Monitoring
178
+
179
+ ### Cloud Function Logs
180
+
181
+ Check Cloud Function logs for:
182
+ - Computation success/failure
183
+ - Processing time per clinic
184
+ - Errors during computation
185
+
186
+ ### Firestore Usage
187
+
188
+ Monitor Firestore reads:
189
+ - Pre-computed reads: 1 per analytics request
190
+ - On-demand reads: Variable (depends on appointment count)
191
+
192
+ ## Future Enhancements
193
+
194
+ 1. **Incremental Updates**: Only recompute changed periods
195
+ 2. **Real-time Triggers**: Update analytics when appointments change
196
+ 3. **Aggregated Views**: Pre-compute common dashboard views
197
+ 4. **Historical Snapshots**: Keep historical analytics for trend analysis
198
+ 5. **Multi-clinic Aggregation**: Aggregate analytics across clinic groups
199
+
@@ -0,0 +1,225 @@
1
+ # Analytics Cloud Functions Guide
2
+
3
+ ## Overview
4
+
5
+ The Analytics Service supports **on-demand calculation via Cloud Functions**, allowing you to trigger fresh analytics calculations server-side and optionally cache the results.
6
+
7
+ ## Available Cloud Functions
8
+
9
+ ### 1. `calculateAnalyticsOnDemand` (Callable)
10
+
11
+ A callable Cloud Function that calculates analytics on-demand and optionally stores results in cache.
12
+
13
+ **Location**: `Cloud/functions/src/analytics/calculateAnalyticsOnDemand.ts`
14
+
15
+ ## Usage
16
+
17
+ ### Using the Client Service Helper
18
+
19
+ ```typescript
20
+ import { AnalyticsCloudService } from '@blackcode_sa/metaestetics-api';
21
+ import { getApp } from 'firebase/app';
22
+
23
+ const app = getApp();
24
+ const cloudService = new AnalyticsCloudService(app);
25
+
26
+ // Calculate dashboard analytics on-demand
27
+ const dashboard = await cloudService.calculateDashboard(
28
+ { clinicBranchId: 'clinic-123' },
29
+ { start: new Date('2024-01-01'), end: new Date('2024-12-31') },
30
+ { storeInCache: true } // Store result for future use
31
+ );
32
+ ```
33
+
34
+ ### Direct Callable Function Usage
35
+
36
+ ```typescript
37
+ import { getFunctions, httpsCallable } from 'firebase/functions';
38
+ import { getApp } from 'firebase/app';
39
+
40
+ const functions = getFunctions(getApp(), 'europe-west6');
41
+ const calculateAnalytics = httpsCallable(functions, 'calculateAnalyticsOnDemand');
42
+
43
+ // Calculate revenue metrics grouped by practitioner
44
+ const result = await calculateAnalytics({
45
+ analyticsType: 'revenueByEntity',
46
+ groupBy: 'practitioner',
47
+ filters: { clinicBranchId: 'clinic-123' },
48
+ dateRange: {
49
+ start: '2024-01-01T00:00:00Z',
50
+ end: '2024-12-31T23:59:59Z',
51
+ },
52
+ options: {
53
+ storeInCache: true, // Store in cache after calculation
54
+ useCache: false, // Force fresh calculation
55
+ },
56
+ });
57
+
58
+ const revenueByPractitioner = result.data.data;
59
+ ```
60
+
61
+ ## Supported Analytics Types
62
+
63
+ ### Top-Level Analytics
64
+
65
+ - `dashboard` - Complete dashboard analytics
66
+ - `practitioner` - Individual practitioner analytics (requires `entityId`)
67
+ - `procedure` - Individual procedure analytics (requires `entityId`)
68
+ - `clinic` - Clinic-level analytics
69
+ - `timeEfficiency` - Time efficiency metrics
70
+ - `revenue` - Revenue metrics
71
+ - `productUsage` - Product usage metrics
72
+ - `patient` - Patient analytics (optional `entityId`)
73
+
74
+ ### Grouped Analytics
75
+
76
+ - `revenueByEntity` - Revenue grouped by clinic/practitioner/procedure/patient/technology (requires `groupBy`)
77
+ - `productUsageByEntity` - Product usage grouped by entity (requires `groupBy`)
78
+ - `timeEfficiencyByEntity` - Time efficiency grouped by entity (requires `groupBy`)
79
+ - `patientBehaviorByEntity` - Patient behavior grouped by entity (requires `groupBy`)
80
+ - `cancellation` - Cancellation metrics grouped by entity (requires `groupBy`)
81
+ - `noShow` - No-show metrics grouped by entity (requires `groupBy`)
82
+
83
+ ## Request Parameters
84
+
85
+ ```typescript
86
+ interface CalculateAnalyticsRequest {
87
+ analyticsType: string; // Required: Type of analytics to calculate
88
+ filters?: AnalyticsFilters; // Optional: Filters (clinicBranchId, etc.)
89
+ dateRange?: { // Optional: Date range
90
+ start: string; // ISO date string
91
+ end: string; // ISO date string
92
+ };
93
+ entityId?: string; // Required for practitioner/procedure/patient
94
+ groupBy?: EntityType; // Required for grouped analytics
95
+ options?: {
96
+ storeInCache?: boolean; // Default: true - Store result in cache
97
+ useCache?: boolean; // Default: false - Use cache if available
98
+ maxCacheAgeHours?: number; // Default: 12 - Max cache age
99
+ };
100
+ }
101
+ ```
102
+
103
+ ## Response Format
104
+
105
+ ```typescript
106
+ interface CalculateAnalyticsResponse {
107
+ success: boolean;
108
+ data: any; // The calculated analytics data
109
+ computedAt: string; // ISO timestamp of when it was computed
110
+ }
111
+ ```
112
+
113
+ ## Examples
114
+
115
+ ### Example 1: Calculate Dashboard Analytics
116
+
117
+ ```typescript
118
+ const dashboard = await cloudService.calculateDashboard(
119
+ { clinicBranchId: 'clinic-123' },
120
+ { start: new Date('2024-01-01'), end: new Date('2024-12-31') },
121
+ { storeInCache: true }
122
+ );
123
+ ```
124
+
125
+ ### Example 2: Calculate Revenue by Technology
126
+
127
+ ```typescript
128
+ const revenueByTechnology = await cloudService.calculateRevenueByEntity(
129
+ 'technology',
130
+ { start: new Date('2024-01-01'), end: new Date('2024-12-31') },
131
+ { clinicBranchId: 'clinic-123' },
132
+ { storeInCache: true }
133
+ );
134
+ ```
135
+
136
+ ### Example 3: Force Fresh Calculation (Bypass Cache)
137
+
138
+ ```typescript
139
+ const result = await calculateAnalytics({
140
+ analyticsType: 'dashboard',
141
+ filters: { clinicBranchId: 'clinic-123' },
142
+ dateRange: {
143
+ start: '2024-01-01T00:00:00Z',
144
+ end: '2024-12-31T23:59:59Z',
145
+ },
146
+ options: {
147
+ useCache: false, // Don't use cache
148
+ storeInCache: true, // But store the result
149
+ },
150
+ });
151
+ ```
152
+
153
+ ### Example 4: Calculate Practitioner Analytics
154
+
155
+ ```typescript
156
+ const practitionerMetrics = await cloudService.calculatePractitioner(
157
+ 'practitioner-123',
158
+ { start: new Date('2024-01-01'), end: new Date('2024-12-31') },
159
+ {
160
+ storeInCache: true,
161
+ clinicBranchId: 'clinic-123',
162
+ }
163
+ );
164
+ ```
165
+
166
+ ## Benefits
167
+
168
+ ### Performance
169
+ - **Server-side computation**: Faster than client-side for large datasets
170
+ - **Parallel processing**: Cloud Functions can handle multiple requests simultaneously
171
+ - **No client resource usage**: Computation happens in the cloud
172
+
173
+ ### Caching
174
+ - **Automatic caching**: Results can be stored automatically
175
+ - **Future reads**: Cached results can be read instantly by `AnalyticsService`
176
+ - **Configurable**: Control whether to use/store cache
177
+
178
+ ### Cost Optimization
179
+ - **Efficient**: Server-side computation is optimized
180
+ - **Scalable**: Handles large datasets without client limitations
181
+ - **Predictable**: Fixed cost per calculation
182
+
183
+ ## When to Use
184
+
185
+ ### Use Cloud Function When:
186
+ - ✅ Cache is stale and you need fresh data immediately
187
+ - ✅ Calculating analytics for large date ranges
188
+ - ✅ Need to calculate multiple analytics types
189
+ - ✅ Want to store results for future use
190
+ - ✅ Client device has limited resources
191
+
192
+ ### Use Client Service When:
193
+ - ✅ Cache is fresh (< 12 hours old)
194
+ - ✅ Small date ranges
195
+ - ✅ Quick reads from cache
196
+ - ✅ Offline-first scenarios
197
+
198
+ ## Error Handling
199
+
200
+ ```typescript
201
+ try {
202
+ const result = await cloudService.calculateDashboard(...);
203
+ } catch (error) {
204
+ if (error.code === 'invalid-argument') {
205
+ // Invalid parameters provided
206
+ } else if (error.code === 'internal') {
207
+ // Server error during calculation
208
+ }
209
+ }
210
+ ```
211
+
212
+ ## Integration with AnalyticsService
213
+
214
+ The Cloud Function works seamlessly with `AnalyticsService`:
215
+
216
+ 1. **Client checks cache first** (via `AnalyticsService`)
217
+ 2. **If cache is stale/missing**, call Cloud Function
218
+ 3. **Cloud Function calculates** and optionally stores result
219
+ 4. **Future reads** use the cached data
220
+
221
+ This creates a **hybrid approach**:
222
+ - Fast reads from cache (default)
223
+ - On-demand fresh calculations when needed
224
+ - Automatic caching for future use
225
+