@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,173 @@
1
+ import { EntityType } from './analytics.types';
2
+
3
+ /**
4
+ * Base interface for grouped analytics results
5
+ * All grouped analytics share common entity identification
6
+ */
7
+ export interface GroupedAnalyticsBase {
8
+ entityId: string;
9
+ entityName: string;
10
+ entityType: EntityType;
11
+ }
12
+
13
+ /**
14
+ * Review metrics for grouped analytics
15
+ * Note: This is a simplified version for grouped analytics.
16
+ * For full review analytics, see ReviewAnalyticsService.ReviewMetrics
17
+ */
18
+ export interface ReviewMetrics {
19
+ totalReviews: number;
20
+ averageRating: number;
21
+ recommendationRate: number; // % that would recommend
22
+ // Comparison to overall average
23
+ ratingDifference?: number; // Positive = above average, negative = below
24
+ recommendationDifference?: number; // % difference
25
+ }
26
+
27
+ /**
28
+ * Grouped Revenue Metrics
29
+ * Revenue analytics grouped by clinic, practitioner, procedure, or patient
30
+ */
31
+ export interface GroupedRevenueMetrics extends GroupedAnalyticsBase {
32
+ totalRevenue: number;
33
+ averageRevenuePerAppointment: number;
34
+ totalAppointments: number;
35
+ completedAppointments: number;
36
+ currency: string;
37
+ unpaidRevenue: number;
38
+ refundedRevenue: number;
39
+ totalTax: number;
40
+ totalSubtotal: number;
41
+ // Practitioner info when grouping by procedure
42
+ practitionerId?: string;
43
+ practitionerName?: string;
44
+ // Review metrics (optional)
45
+ reviewMetrics?: ReviewMetrics;
46
+ }
47
+
48
+ /**
49
+ * Grouped Product Usage Metrics
50
+ * Product usage analytics grouped by clinic, practitioner, procedure, or patient
51
+ */
52
+ export interface GroupedProductUsageMetrics extends GroupedAnalyticsBase {
53
+ totalProductsUsed: number;
54
+ uniqueProducts: number;
55
+ totalProductRevenue: number;
56
+ totalProductQuantity: number;
57
+ averageProductsPerAppointment: number;
58
+ topProducts: Array<{
59
+ productId: string;
60
+ productName: string;
61
+ brandName: string;
62
+ totalQuantity: number;
63
+ totalRevenue: number;
64
+ usageCount: number;
65
+ }>;
66
+ // Practitioner info when grouping by procedure
67
+ practitionerId?: string;
68
+ practitionerName?: string;
69
+ }
70
+
71
+ /**
72
+ * Grouped Patient Retention Metrics
73
+ * Patient retention analytics grouped by clinic, practitioner, or procedure
74
+ */
75
+ export interface GroupedPatientRetentionMetrics extends GroupedAnalyticsBase {
76
+ totalPatients: number;
77
+ newPatients: number;
78
+ returningPatients: number;
79
+ retentionRate: number; // percentage
80
+ averageAppointmentsPerPatient: number;
81
+ patientsByAppointmentCount: Array<{
82
+ appointmentCount: number;
83
+ patientCount: number;
84
+ percentage: number;
85
+ }>;
86
+ }
87
+
88
+ /**
89
+ * Grouped Time Efficiency Metrics
90
+ * Time efficiency analytics grouped by clinic, practitioner, procedure, or patient
91
+ */
92
+ export interface GroupedTimeEfficiencyMetrics extends GroupedAnalyticsBase {
93
+ totalAppointments: number;
94
+ appointmentsWithActualTime: number;
95
+ averageBookedDuration: number; // minutes
96
+ averageActualDuration: number; // minutes
97
+ averageEfficiency: number; // percentage
98
+ totalOverrun: number; // minutes
99
+ totalUnderutilization: number; // minutes
100
+ averageOverrun: number; // minutes
101
+ averageUnderutilization: number; // minutes
102
+ // Practitioner info when grouping by procedure
103
+ practitionerId?: string;
104
+ practitionerName?: string;
105
+ }
106
+
107
+ /**
108
+ * Grouped Patient Behavior Metrics
109
+ * Patient behavior (no-show, cancellation) grouped by clinic, practitioner, or procedure
110
+ */
111
+ export interface GroupedPatientBehaviorMetrics extends GroupedAnalyticsBase {
112
+ totalPatients: number;
113
+ patientsWithNoShows: number;
114
+ patientsWithCancellations: number;
115
+ averageNoShowRate: number; // percentage (average across patients)
116
+ averageCancellationRate: number; // percentage (average across patients)
117
+ topNoShowPatients: Array<{
118
+ patientId: string;
119
+ patientName: string;
120
+ noShowCount: number;
121
+ totalAppointments: number;
122
+ noShowRate: number;
123
+ }>;
124
+ topCancellationPatients: Array<{
125
+ patientId: string;
126
+ patientName: string;
127
+ cancellationCount: number;
128
+ totalAppointments: number;
129
+ cancellationRate: number;
130
+ }>;
131
+ }
132
+
133
+ /**
134
+ * Grouped Procedure Performance Metrics
135
+ * Procedure performance grouped by clinic or practitioner
136
+ */
137
+ export interface GroupedProcedurePerformanceMetrics extends GroupedAnalyticsBase {
138
+ totalProcedures: number;
139
+ totalAppointments: number;
140
+ completedAppointments: number;
141
+ totalRevenue: number;
142
+ averageRevenuePerProcedure: number;
143
+ topProcedures: Array<{
144
+ procedureId: string;
145
+ procedureName: string;
146
+ appointmentCount: number;
147
+ revenue: number;
148
+ cancellationRate: number;
149
+ noShowRate: number;
150
+ }>;
151
+ }
152
+
153
+ /**
154
+ * Grouped Practitioner Performance Metrics
155
+ * Practitioner performance grouped by clinic
156
+ */
157
+ export interface GroupedPractitionerPerformanceMetrics extends GroupedAnalyticsBase {
158
+ totalPractitioners: number;
159
+ totalAppointments: number;
160
+ completedAppointments: number;
161
+ totalRevenue: number;
162
+ averageRevenuePerPractitioner: number;
163
+ topPractitioners: Array<{
164
+ practitionerId: string;
165
+ practitionerName: string;
166
+ appointmentCount: number;
167
+ revenue: number;
168
+ cancellationRate: number;
169
+ noShowRate: number;
170
+ timeEfficiency: number;
171
+ }>;
172
+ }
173
+
@@ -0,0 +1,4 @@
1
+ export * from './analytics.types';
2
+ export * from './stored-analytics.types';
3
+ export * from './grouped-analytics.types';
4
+
@@ -0,0 +1,137 @@
1
+ import { Timestamp } from 'firebase/firestore';
2
+ import {
3
+ PractitionerAnalytics,
4
+ ProcedureAnalytics,
5
+ TimeEfficiencyMetrics,
6
+ CancellationMetrics,
7
+ NoShowMetrics,
8
+ RevenueMetrics,
9
+ DashboardAnalytics,
10
+ ClinicAnalytics,
11
+ AnalyticsDateRange,
12
+ } from './analytics.types';
13
+
14
+ /**
15
+ * Period type for analytics snapshots
16
+ */
17
+ export type AnalyticsPeriod = 'daily' | 'weekly' | 'monthly' | 'yearly' | 'all_time';
18
+
19
+ /**
20
+ * Analytics document metadata
21
+ */
22
+ export interface AnalyticsMetadata {
23
+ clinicBranchId: string;
24
+ period: AnalyticsPeriod;
25
+ periodStart: Date;
26
+ periodEnd: Date;
27
+ computedAt: Timestamp;
28
+ computedBy: 'cloud_function' | 'manual';
29
+ version: string; // Schema version for future migrations
30
+ }
31
+
32
+ /**
33
+ * Stored Practitioner Analytics
34
+ * Stored in: clinics/{clinicBranchId}/analytics/practitioners/{practitionerId}/{period}
35
+ */
36
+ export interface StoredPractitionerAnalytics extends PractitionerAnalytics {
37
+ metadata: AnalyticsMetadata;
38
+ }
39
+
40
+ /**
41
+ * Stored Procedure Analytics
42
+ * Stored in: clinics/{clinicBranchId}/analytics/procedures/{procedureId}/{period}
43
+ */
44
+ export interface StoredProcedureAnalytics extends ProcedureAnalytics {
45
+ metadata: AnalyticsMetadata;
46
+ }
47
+
48
+ /**
49
+ * Stored Clinic Analytics
50
+ * Stored in: clinics/{clinicBranchId}/analytics/clinic/{period}
51
+ */
52
+ export interface StoredClinicAnalytics extends ClinicAnalytics {
53
+ metadata: AnalyticsMetadata;
54
+ }
55
+
56
+ /**
57
+ * Stored Dashboard Analytics
58
+ * Stored in: clinics/{clinicBranchId}/analytics/dashboard/{period}
59
+ */
60
+ export interface StoredDashboardAnalytics extends DashboardAnalytics {
61
+ metadata: AnalyticsMetadata;
62
+ }
63
+
64
+ /**
65
+ * Stored Time Efficiency Metrics
66
+ * Stored in: clinics/{clinicBranchId}/analytics/time_efficiency/{period}
67
+ */
68
+ export interface StoredTimeEfficiencyMetrics extends TimeEfficiencyMetrics {
69
+ metadata: AnalyticsMetadata;
70
+ }
71
+
72
+ /**
73
+ * Stored Cancellation Metrics
74
+ * Stored in: clinics/{clinicBranchId}/analytics/cancellations/{entityType}/{period}
75
+ */
76
+ export interface StoredCancellationMetrics extends CancellationMetrics {
77
+ metadata: AnalyticsMetadata;
78
+ }
79
+
80
+ /**
81
+ * Stored No-Show Metrics
82
+ * Stored in: clinics/{clinicBranchId}/analytics/no_shows/{entityType}/{period}
83
+ */
84
+ export interface StoredNoShowMetrics extends NoShowMetrics {
85
+ metadata: AnalyticsMetadata;
86
+ }
87
+
88
+ /**
89
+ * Stored Revenue Metrics
90
+ * Stored in: clinics/{clinicBranchId}/analytics/revenue/{period}
91
+ */
92
+ export interface StoredRevenueMetrics extends RevenueMetrics {
93
+ metadata: AnalyticsMetadata;
94
+ }
95
+
96
+ /**
97
+ * Collection names for stored analytics
98
+ */
99
+ export const ANALYTICS_COLLECTION = 'analytics';
100
+ export const PRACTITIONER_ANALYTICS_SUBCOLLECTION = 'practitioners';
101
+ export const PROCEDURE_ANALYTICS_SUBCOLLECTION = 'procedures';
102
+ export const CLINIC_ANALYTICS_SUBCOLLECTION = 'clinic';
103
+ export const DASHBOARD_ANALYTICS_SUBCOLLECTION = 'dashboard';
104
+ export const TIME_EFFICIENCY_ANALYTICS_SUBCOLLECTION = 'time_efficiency';
105
+ export const CANCELLATION_ANALYTICS_SUBCOLLECTION = 'cancellations';
106
+ export const NO_SHOW_ANALYTICS_SUBCOLLECTION = 'no_shows';
107
+ export const REVENUE_ANALYTICS_SUBCOLLECTION = 'revenue';
108
+
109
+ /**
110
+ * Options for reading stored analytics
111
+ */
112
+ export interface ReadStoredAnalyticsOptions {
113
+ /**
114
+ * Whether to use cached/pre-computed data
115
+ * @default true
116
+ */
117
+ useCache?: boolean;
118
+ /**
119
+ * Maximum age of cached data in hours before recalculating
120
+ * @default 12
121
+ */
122
+ maxCacheAgeHours?: number;
123
+ /**
124
+ * Period to read
125
+ * @default 'all_time'
126
+ */
127
+ period?: AnalyticsPeriod;
128
+ /**
129
+ * Date range for the period (calculated if not provided)
130
+ */
131
+ dateRange?: AnalyticsDateRange;
132
+ /**
133
+ * Clinic branch ID (required for reading stored analytics)
134
+ */
135
+ clinicBranchId?: string;
136
+ }
137
+
@@ -40,5 +40,8 @@ export * from "./profile";
40
40
  // Reviews types
41
41
  export * from "./reviews";
42
42
 
43
+ // Analytics types
44
+ export * from "./analytics";
45
+
43
46
  // User types
44
47
  export * from "./user";