@blackcode_sa/metaestetics-api 1.12.72 → 1.13.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 (37) hide show
  1. package/dist/admin/index.d.mts +872 -1
  2. package/dist/admin/index.d.ts +872 -1
  3. package/dist/admin/index.js +3604 -356
  4. package/dist/admin/index.mjs +3594 -357
  5. package/dist/index.d.mts +1349 -1
  6. package/dist/index.d.ts +1349 -1
  7. package/dist/index.js +5325 -2141
  8. package/dist/index.mjs +4939 -1767
  9. package/package.json +1 -1
  10. package/src/admin/analytics/analytics.admin.service.ts +278 -0
  11. package/src/admin/analytics/index.ts +2 -0
  12. package/src/admin/index.ts +6 -0
  13. package/src/backoffice/services/analytics.service.proposal.md +4 -0
  14. package/src/services/analytics/ARCHITECTURE.md +199 -0
  15. package/src/services/analytics/CLOUD_FUNCTIONS.md +225 -0
  16. package/src/services/analytics/GROUPED_ANALYTICS.md +501 -0
  17. package/src/services/analytics/QUICK_START.md +393 -0
  18. package/src/services/analytics/README.md +304 -0
  19. package/src/services/analytics/SUMMARY.md +141 -0
  20. package/src/services/analytics/TRENDS.md +380 -0
  21. package/src/services/analytics/USAGE_GUIDE.md +518 -0
  22. package/src/services/analytics/analytics-cloud.service.ts +222 -0
  23. package/src/services/analytics/analytics.service.ts +2142 -0
  24. package/src/services/analytics/index.ts +4 -0
  25. package/src/services/analytics/review-analytics.service.ts +941 -0
  26. package/src/services/analytics/utils/appointment-filtering.utils.ts +138 -0
  27. package/src/services/analytics/utils/cost-calculation.utils.ts +182 -0
  28. package/src/services/analytics/utils/grouping.utils.ts +434 -0
  29. package/src/services/analytics/utils/stored-analytics.utils.ts +347 -0
  30. package/src/services/analytics/utils/time-calculation.utils.ts +186 -0
  31. package/src/services/analytics/utils/trend-calculation.utils.ts +200 -0
  32. package/src/services/index.ts +1 -0
  33. package/src/types/analytics/analytics.types.ts +597 -0
  34. package/src/types/analytics/grouped-analytics.types.ts +173 -0
  35. package/src/types/analytics/index.ts +4 -0
  36. package/src/types/analytics/stored-analytics.types.ts +137 -0
  37. package/src/types/index.ts +3 -0
@@ -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
+