@blackcode_sa/metaestetics-api 1.5.34 → 1.5.35

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,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.5.34",
4
+ "version": "1.5.35",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
@@ -6,6 +6,7 @@ import {
6
6
  } from "firebase/firestore";
7
7
  import { Auth } from "firebase/auth";
8
8
  import { FirebaseApp } from "firebase/app";
9
+ import { Functions, getFunctions, httpsCallable } from "firebase/functions";
9
10
  import { BaseService } from "../base.service";
10
11
  import {
11
12
  Appointment,
@@ -36,6 +37,13 @@ import {
36
37
  searchAppointmentsUtil,
37
38
  } from "./utils/appointment.utils";
38
39
 
40
+ /**
41
+ * Interface for available booking slot
42
+ */
43
+ interface AvailableSlot {
44
+ start: Date;
45
+ }
46
+
39
47
  /**
40
48
  * AppointmentService is responsible for managing appointments,
41
49
  * including creating, updating, retrieving, and searching appointments.
@@ -46,6 +54,7 @@ export class AppointmentService extends BaseService {
46
54
  private patientService: PatientService;
47
55
  private practitionerService: PractitionerService;
48
56
  private clinicService: ClinicService;
57
+ private functions: Functions;
49
58
 
50
59
  /**
51
60
  * Creates a new AppointmentService instance.
@@ -72,6 +81,95 @@ export class AppointmentService extends BaseService {
72
81
  this.patientService = patientService;
73
82
  this.practitionerService = practitionerService;
74
83
  this.clinicService = clinicService;
84
+ this.functions = getFunctions(app, "europe-west6"); // Initialize Firebase Functions with the correct region
85
+ }
86
+
87
+ /**
88
+ * Gets available booking slots for a specific clinic, practitioner, and procedure.
89
+ *
90
+ * @param clinicId ID of the clinic
91
+ * @param practitionerId ID of the practitioner
92
+ * @param procedureId ID of the procedure
93
+ * @param startDate Start date of the time range to check
94
+ * @param endDate End date of the time range to check
95
+ * @returns Array of available booking slots
96
+ */
97
+ async getAvailableBookingSlots(
98
+ clinicId: string,
99
+ practitionerId: string,
100
+ procedureId: string,
101
+ startDate: Date,
102
+ endDate: Date
103
+ ): Promise<AvailableSlot[]> {
104
+ try {
105
+ console.log(
106
+ `[APPOINTMENT_SERVICE] Getting available booking slots for clinic: ${clinicId}, practitioner: ${practitionerId}, procedure: ${procedureId}`
107
+ );
108
+
109
+ // Validate input parameters
110
+ if (
111
+ !clinicId ||
112
+ !practitionerId ||
113
+ !procedureId ||
114
+ !startDate ||
115
+ !endDate
116
+ ) {
117
+ throw new Error(
118
+ "Missing required parameters for booking slots calculation"
119
+ );
120
+ }
121
+
122
+ if (endDate <= startDate) {
123
+ throw new Error("End date must be after start date");
124
+ }
125
+
126
+ // Create a callable function reference
127
+ const getAvailableBookingSlotsFunction = httpsCallable(
128
+ this.functions,
129
+ "getAvailableBookingSlots"
130
+ );
131
+
132
+ // Call the cloud function with required parameters
133
+ const result = await getAvailableBookingSlotsFunction({
134
+ clinicId,
135
+ practitionerId,
136
+ procedureId,
137
+ timeframe: {
138
+ start: startDate.getTime(), // Convert to timestamp
139
+ end: endDate.getTime(),
140
+ },
141
+ });
142
+
143
+ // Process the response
144
+ const response = result.data as {
145
+ success: boolean;
146
+ availableSlots: { start: number }[];
147
+ error?: string;
148
+ };
149
+
150
+ if (!response.success) {
151
+ throw new Error(
152
+ response.error || "Failed to get available booking slots"
153
+ );
154
+ }
155
+
156
+ // Convert timestamp numbers to Date objects
157
+ const slots: AvailableSlot[] = response.availableSlots.map((slot) => ({
158
+ start: new Date(slot.start),
159
+ }));
160
+
161
+ console.log(
162
+ `[APPOINTMENT_SERVICE] Found ${slots.length} available booking slots`
163
+ );
164
+
165
+ return slots;
166
+ } catch (error) {
167
+ console.error(
168
+ "[APPOINTMENT_SERVICE] Error getting available booking slots:",
169
+ error
170
+ );
171
+ throw error;
172
+ }
75
173
  }
76
174
 
77
175
  /**