@blackcode_sa/metaestetics-api 1.7.32 → 1.7.33
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/dist/admin/index.d.mts +161 -1
- package/dist/admin/index.d.ts +161 -1
- package/dist/admin/index.js +542 -114
- package/dist/admin/index.mjs +540 -114
- package/package.json +1 -1
- package/src/admin/aggregation/practitioner-invite/practitioner-invite.aggregation.service.ts +576 -0
- package/src/admin/index.ts +8 -0
- package/src/types/calendar/index.ts +2 -2
package/package.json
CHANGED
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
import * as admin from "firebase-admin";
|
|
2
|
+
import {
|
|
3
|
+
PractitionerInvite,
|
|
4
|
+
PractitionerInviteStatus,
|
|
5
|
+
PRACTITIONER_INVITES_COLLECTION,
|
|
6
|
+
} from "../../../types/clinic/practitioner-invite.types";
|
|
7
|
+
import { DoctorInfo } from "../../../types/clinic";
|
|
8
|
+
import {
|
|
9
|
+
Practitioner,
|
|
10
|
+
PRACTITIONERS_COLLECTION,
|
|
11
|
+
PractitionerClinicWorkingHours,
|
|
12
|
+
} from "../../../types/practitioner";
|
|
13
|
+
import { CLINICS_COLLECTION, Clinic } from "../../../types/clinic";
|
|
14
|
+
import { ClinicInfo } from "../../../types/profile";
|
|
15
|
+
import { Logger } from "../../logger";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @class PractitionerInviteAggregationService
|
|
19
|
+
* @description Handles aggregation tasks and side effects related to practitioner invite lifecycle events.
|
|
20
|
+
* This service is intended to be used primarily by background functions (e.g., Cloud Functions)
|
|
21
|
+
* triggered by changes in the practitioner-invites collection.
|
|
22
|
+
*/
|
|
23
|
+
export class PractitionerInviteAggregationService {
|
|
24
|
+
private db: admin.firestore.Firestore;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Constructor for PractitionerInviteAggregationService.
|
|
28
|
+
* @param firestore Optional Firestore instance. If not provided, it uses the default admin SDK instance.
|
|
29
|
+
*/
|
|
30
|
+
constructor(firestore?: admin.firestore.Firestore) {
|
|
31
|
+
this.db = firestore || admin.firestore();
|
|
32
|
+
Logger.info("[PractitionerInviteAggregationService] Initialized.");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Handles side effects when a practitioner invite is first created.
|
|
37
|
+
* This function would typically be called by a Firestore onCreate trigger.
|
|
38
|
+
* @param {PractitionerInvite} invite - The newly created PractitionerInvite object.
|
|
39
|
+
* @returns {Promise<void>}
|
|
40
|
+
*/
|
|
41
|
+
async handleInviteCreate(invite: PractitionerInvite): Promise<void> {
|
|
42
|
+
Logger.info(
|
|
43
|
+
`[PractitionerInviteAggService] Handling CREATE for invite: ${invite.id}, practitioner: ${invite.practitionerId}, clinic: ${invite.clinicId}, status: ${invite.status}`
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
// TODO: Add any side effects needed when an invite is created
|
|
48
|
+
// For example: Send notification emails, update counters, etc.
|
|
49
|
+
// Currently, the main email sending is handled by the mailing service
|
|
50
|
+
|
|
51
|
+
Logger.info(
|
|
52
|
+
`[PractitionerInviteAggService] Successfully processed CREATE for invite: ${invite.id}`
|
|
53
|
+
);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
Logger.error(
|
|
56
|
+
`[PractitionerInviteAggService] Error in handleInviteCreate for invite ${invite.id}:`,
|
|
57
|
+
error
|
|
58
|
+
);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Handles side effects when a practitioner invite is updated.
|
|
65
|
+
* This function would typically be called by a Firestore onUpdate trigger.
|
|
66
|
+
* @param {PractitionerInvite} before - The PractitionerInvite object before the update.
|
|
67
|
+
* @param {PractitionerInvite} after - The PractitionerInvite object after the update.
|
|
68
|
+
* @returns {Promise<void>}
|
|
69
|
+
*/
|
|
70
|
+
async handleInviteUpdate(
|
|
71
|
+
before: PractitionerInvite,
|
|
72
|
+
after: PractitionerInvite
|
|
73
|
+
): Promise<void> {
|
|
74
|
+
Logger.info(
|
|
75
|
+
`[PractitionerInviteAggService] Handling UPDATE for invite: ${after.id}. Status ${before.status} -> ${after.status}`
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
const statusChanged = before.status !== after.status;
|
|
80
|
+
|
|
81
|
+
if (statusChanged) {
|
|
82
|
+
Logger.info(
|
|
83
|
+
`[PractitionerInviteAggService] Status changed for invite ${after.id}: ${before.status} -> ${after.status}`
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// Handle PENDING -> ACCEPTED
|
|
87
|
+
if (
|
|
88
|
+
before.status === PractitionerInviteStatus.PENDING &&
|
|
89
|
+
after.status === PractitionerInviteStatus.ACCEPTED
|
|
90
|
+
) {
|
|
91
|
+
Logger.info(
|
|
92
|
+
`[PractitionerInviteAggService] Invite ${after.id} PENDING -> ACCEPTED. Adding practitioner to clinic.`
|
|
93
|
+
);
|
|
94
|
+
await this.handleInviteAccepted(after);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Handle PENDING -> REJECTED
|
|
98
|
+
else if (
|
|
99
|
+
before.status === PractitionerInviteStatus.PENDING &&
|
|
100
|
+
after.status === PractitionerInviteStatus.REJECTED
|
|
101
|
+
) {
|
|
102
|
+
Logger.info(
|
|
103
|
+
`[PractitionerInviteAggService] Invite ${after.id} PENDING -> REJECTED.`
|
|
104
|
+
);
|
|
105
|
+
await this.handleInviteRejected(after);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Handle PENDING -> CANCELLED
|
|
109
|
+
else if (
|
|
110
|
+
before.status === PractitionerInviteStatus.PENDING &&
|
|
111
|
+
after.status === PractitionerInviteStatus.CANCELLED
|
|
112
|
+
) {
|
|
113
|
+
Logger.info(
|
|
114
|
+
`[PractitionerInviteAggService] Invite ${after.id} PENDING -> CANCELLED.`
|
|
115
|
+
);
|
|
116
|
+
await this.handleInviteCancelled(after);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
Logger.info(
|
|
121
|
+
`[PractitionerInviteAggService] Successfully processed UPDATE for invite: ${after.id}`
|
|
122
|
+
);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
Logger.error(
|
|
125
|
+
`[PractitionerInviteAggService] Error in handleInviteUpdate for invite ${after.id}:`,
|
|
126
|
+
error
|
|
127
|
+
);
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Handles side effects when a practitioner invite is deleted.
|
|
134
|
+
* @param deletedInvite - The PractitionerInvite object that was deleted.
|
|
135
|
+
* @returns {Promise<void>}
|
|
136
|
+
*/
|
|
137
|
+
async handleInviteDelete(deletedInvite: PractitionerInvite): Promise<void> {
|
|
138
|
+
Logger.info(
|
|
139
|
+
`[PractitionerInviteAggService] Handling DELETE for invite: ${deletedInvite.id}`
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
// TODO: Add cleanup logic if needed
|
|
144
|
+
// For now, deleting an invite doesn't require additional aggregation actions
|
|
145
|
+
// since the practitioner-clinic relationship would already be established if the invite was accepted
|
|
146
|
+
|
|
147
|
+
Logger.info(
|
|
148
|
+
`[PractitionerInviteAggService] Successfully processed DELETE for invite: ${deletedInvite.id}`
|
|
149
|
+
);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
Logger.error(
|
|
152
|
+
`[PractitionerInviteAggService] Error in handleInviteDelete for invite ${deletedInvite.id}:`,
|
|
153
|
+
error
|
|
154
|
+
);
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// --- Private Helper Methods ---
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Handles the business logic when a practitioner accepts an invite.
|
|
163
|
+
* This includes adding the practitioner to the clinic and the clinic to the practitioner.
|
|
164
|
+
* @param {PractitionerInvite} invite - The accepted invite
|
|
165
|
+
* @returns {Promise<void>}
|
|
166
|
+
*/
|
|
167
|
+
private async handleInviteAccepted(
|
|
168
|
+
invite: PractitionerInvite
|
|
169
|
+
): Promise<void> {
|
|
170
|
+
Logger.info(
|
|
171
|
+
`[PractitionerInviteAggService] Processing accepted invite ${invite.id} for practitioner ${invite.practitionerId} and clinic ${invite.clinicId}`
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
try {
|
|
175
|
+
// Fetch the current practitioner and clinic data to ensure they exist
|
|
176
|
+
const [practitioner, clinic] = await Promise.all([
|
|
177
|
+
this.fetchPractitionerById(invite.practitionerId),
|
|
178
|
+
this.fetchClinicById(invite.clinicId),
|
|
179
|
+
]);
|
|
180
|
+
|
|
181
|
+
if (!practitioner) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`Practitioner ${invite.practitionerId} not found during invite acceptance`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!clinic) {
|
|
188
|
+
throw new Error(
|
|
189
|
+
`Clinic ${invite.clinicId} not found during invite acceptance`
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Create DoctorInfo object for aggregation
|
|
194
|
+
const doctorInfo: DoctorInfo = {
|
|
195
|
+
id: practitioner.id,
|
|
196
|
+
name: `${practitioner.basicInfo.firstName} ${practitioner.basicInfo.lastName}`,
|
|
197
|
+
description: practitioner.basicInfo.bio || undefined,
|
|
198
|
+
photo:
|
|
199
|
+
typeof practitioner.basicInfo.profileImageUrl === "object" &&
|
|
200
|
+
practitioner.basicInfo.profileImageUrl !== null
|
|
201
|
+
? (practitioner.basicInfo.profileImageUrl as any)?.url || null
|
|
202
|
+
: typeof practitioner.basicInfo.profileImageUrl === "string"
|
|
203
|
+
? practitioner.basicInfo.profileImageUrl
|
|
204
|
+
: null,
|
|
205
|
+
rating: practitioner.reviewInfo?.averageRating || 0,
|
|
206
|
+
services: practitioner.proceduresInfo?.map((proc) => proc.name) || [], // Use procedure names as services
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// Create ClinicInfo object for aggregation
|
|
210
|
+
const clinicInfo: ClinicInfo = {
|
|
211
|
+
id: clinic.id,
|
|
212
|
+
featuredPhoto:
|
|
213
|
+
typeof clinic.coverPhoto === "object" && clinic.coverPhoto !== null
|
|
214
|
+
? (clinic.coverPhoto as any)?.url || ""
|
|
215
|
+
: typeof clinic.coverPhoto === "string"
|
|
216
|
+
? clinic.coverPhoto
|
|
217
|
+
: "",
|
|
218
|
+
name: clinic.name,
|
|
219
|
+
description: clinic.description || null,
|
|
220
|
+
location: clinic.location,
|
|
221
|
+
contactInfo: clinic.contactInfo,
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// Check if practitioner is already associated with the clinic
|
|
225
|
+
const isPractitionerInClinic = clinic.doctors.includes(practitioner.id);
|
|
226
|
+
const isClinicInPractitioner = practitioner.clinics.includes(clinic.id);
|
|
227
|
+
|
|
228
|
+
// Add practitioner to clinic if not already there
|
|
229
|
+
if (!isPractitionerInClinic) {
|
|
230
|
+
Logger.info(
|
|
231
|
+
`[PractitionerInviteAggService] Adding practitioner ${practitioner.id} to clinic ${clinic.id}`
|
|
232
|
+
);
|
|
233
|
+
await this.addPractitionerToClinic(clinic.id, doctorInfo);
|
|
234
|
+
} else {
|
|
235
|
+
Logger.info(
|
|
236
|
+
`[PractitionerInviteAggService] Practitioner ${practitioner.id} already in clinic ${clinic.id}, updating info`
|
|
237
|
+
);
|
|
238
|
+
await this.updatePractitionerInfoInClinic(clinic.id, doctorInfo);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Add clinic to practitioner if not already there
|
|
242
|
+
if (!isClinicInPractitioner) {
|
|
243
|
+
Logger.info(
|
|
244
|
+
`[PractitionerInviteAggService] Adding clinic ${clinic.id} to practitioner ${practitioner.id}`
|
|
245
|
+
);
|
|
246
|
+
await this.addClinicToPractitioner(practitioner.id, clinicInfo, invite);
|
|
247
|
+
} else {
|
|
248
|
+
Logger.info(
|
|
249
|
+
`[PractitionerInviteAggService] Clinic ${clinic.id} already in practitioner ${practitioner.id}, updating working hours`
|
|
250
|
+
);
|
|
251
|
+
await this.updatePractitionerWorkingHours(practitioner.id, invite);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
Logger.info(
|
|
255
|
+
`[PractitionerInviteAggService] Successfully processed accepted invite ${invite.id}`
|
|
256
|
+
);
|
|
257
|
+
} catch (error) {
|
|
258
|
+
Logger.error(
|
|
259
|
+
`[PractitionerInviteAggService] Error processing accepted invite ${invite.id}:`,
|
|
260
|
+
error
|
|
261
|
+
);
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Handles the business logic when a practitioner rejects an invite.
|
|
268
|
+
* @param {PractitionerInvite} invite - The rejected invite
|
|
269
|
+
* @returns {Promise<void>}
|
|
270
|
+
*/
|
|
271
|
+
private async handleInviteRejected(
|
|
272
|
+
invite: PractitionerInvite
|
|
273
|
+
): Promise<void> {
|
|
274
|
+
Logger.info(
|
|
275
|
+
`[PractitionerInviteAggService] Processing rejected invite ${invite.id}`
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
// TODO: Add any side effects for rejected invites
|
|
280
|
+
// For example: Update counters, send notifications, etc.
|
|
281
|
+
|
|
282
|
+
Logger.info(
|
|
283
|
+
`[PractitionerInviteAggService] Successfully processed rejected invite ${invite.id}`
|
|
284
|
+
);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
Logger.error(
|
|
287
|
+
`[PractitionerInviteAggService] Error processing rejected invite ${invite.id}:`,
|
|
288
|
+
error
|
|
289
|
+
);
|
|
290
|
+
throw error;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Handles the business logic when an invite is cancelled by admin.
|
|
296
|
+
* @param {PractitionerInvite} invite - The cancelled invite
|
|
297
|
+
* @returns {Promise<void>}
|
|
298
|
+
*/
|
|
299
|
+
private async handleInviteCancelled(
|
|
300
|
+
invite: PractitionerInvite
|
|
301
|
+
): Promise<void> {
|
|
302
|
+
Logger.info(
|
|
303
|
+
`[PractitionerInviteAggService] Processing cancelled invite ${invite.id}`
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
try {
|
|
307
|
+
// TODO: Add any side effects for cancelled invites
|
|
308
|
+
// For example: Update counters, send notifications, etc.
|
|
309
|
+
|
|
310
|
+
Logger.info(
|
|
311
|
+
`[PractitionerInviteAggService] Successfully processed cancelled invite ${invite.id}`
|
|
312
|
+
);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
Logger.error(
|
|
315
|
+
`[PractitionerInviteAggService] Error processing cancelled invite ${invite.id}:`,
|
|
316
|
+
error
|
|
317
|
+
);
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Adds practitioner information to a clinic when an invite is accepted.
|
|
324
|
+
* @param clinicId - ID of the clinic to update
|
|
325
|
+
* @param doctorInfo - Doctor information to add to the clinic
|
|
326
|
+
* @returns {Promise<void>}
|
|
327
|
+
*/
|
|
328
|
+
private async addPractitionerToClinic(
|
|
329
|
+
clinicId: string,
|
|
330
|
+
doctorInfo: DoctorInfo
|
|
331
|
+
): Promise<void> {
|
|
332
|
+
Logger.info(
|
|
333
|
+
`[PractitionerInviteAggService] Adding practitioner ${doctorInfo.id} to clinic ${clinicId}`
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
try {
|
|
337
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
338
|
+
|
|
339
|
+
await clinicRef.update({
|
|
340
|
+
doctors: admin.firestore.FieldValue.arrayUnion(doctorInfo.id),
|
|
341
|
+
doctorsInfo: admin.firestore.FieldValue.arrayUnion(doctorInfo),
|
|
342
|
+
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
Logger.info(
|
|
346
|
+
`[PractitionerInviteAggService] Successfully added practitioner ${doctorInfo.id} to clinic ${clinicId}`
|
|
347
|
+
);
|
|
348
|
+
} catch (error) {
|
|
349
|
+
Logger.error(
|
|
350
|
+
`[PractitionerInviteAggService] Error adding practitioner ${doctorInfo.id} to clinic ${clinicId}:`,
|
|
351
|
+
error
|
|
352
|
+
);
|
|
353
|
+
throw error;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Updates practitioner information in a clinic.
|
|
359
|
+
* @param clinicId - ID of the clinic to update
|
|
360
|
+
* @param doctorInfo - Updated doctor information
|
|
361
|
+
* @returns {Promise<void>}
|
|
362
|
+
*/
|
|
363
|
+
private async updatePractitionerInfoInClinic(
|
|
364
|
+
clinicId: string,
|
|
365
|
+
doctorInfo: DoctorInfo
|
|
366
|
+
): Promise<void> {
|
|
367
|
+
Logger.info(
|
|
368
|
+
`[PractitionerInviteAggService] Updating practitioner ${doctorInfo.id} info in clinic ${clinicId}`
|
|
369
|
+
);
|
|
370
|
+
|
|
371
|
+
try {
|
|
372
|
+
const clinicRef = this.db.collection(CLINICS_COLLECTION).doc(clinicId);
|
|
373
|
+
|
|
374
|
+
// Get current clinic data to filter doctorsInfo manually
|
|
375
|
+
const clinicDoc = await clinicRef.get();
|
|
376
|
+
if (clinicDoc.exists) {
|
|
377
|
+
const clinicData = clinicDoc.data() as Clinic;
|
|
378
|
+
const currentDoctorsInfo = clinicData?.doctorsInfo || [];
|
|
379
|
+
|
|
380
|
+
// Filter out the doctor info with matching ID
|
|
381
|
+
const filteredDoctorsInfo = currentDoctorsInfo.filter(
|
|
382
|
+
(doctor: any) => doctor.id !== doctorInfo.id
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
// Add the updated doctor info to the filtered array
|
|
386
|
+
const updatedDoctorsInfo = [...filteredDoctorsInfo, doctorInfo];
|
|
387
|
+
|
|
388
|
+
// Update with the complete new array
|
|
389
|
+
await clinicRef.update({
|
|
390
|
+
doctorsInfo: updatedDoctorsInfo,
|
|
391
|
+
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
Logger.info(
|
|
395
|
+
`[PractitionerInviteAggService] Successfully updated practitioner ${doctorInfo.id} info in clinic ${clinicId}`
|
|
396
|
+
);
|
|
397
|
+
} else {
|
|
398
|
+
Logger.warn(
|
|
399
|
+
`[PractitionerInviteAggService] Clinic ${clinicId} not found for doctor info update`
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
} catch (error) {
|
|
403
|
+
Logger.error(
|
|
404
|
+
`[PractitionerInviteAggService] Error updating practitioner ${doctorInfo.id} info in clinic ${clinicId}:`,
|
|
405
|
+
error
|
|
406
|
+
);
|
|
407
|
+
throw error;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Adds a clinic to a practitioner's profile with working hours from the invite.
|
|
413
|
+
* @param {string} practitionerId - The practitioner ID
|
|
414
|
+
* @param {ClinicInfo} clinicInfo - The clinic information
|
|
415
|
+
* @param {PractitionerInvite} invite - The accepted invite containing working hours
|
|
416
|
+
* @returns {Promise<void>}
|
|
417
|
+
*/
|
|
418
|
+
private async addClinicToPractitioner(
|
|
419
|
+
practitionerId: string,
|
|
420
|
+
clinicInfo: ClinicInfo,
|
|
421
|
+
invite: PractitionerInvite
|
|
422
|
+
): Promise<void> {
|
|
423
|
+
Logger.info(
|
|
424
|
+
`[PractitionerInviteAggService] Adding clinic ${clinicInfo.id} to practitioner ${practitionerId}`
|
|
425
|
+
);
|
|
426
|
+
|
|
427
|
+
try {
|
|
428
|
+
const practitionerRef = this.db
|
|
429
|
+
.collection(PRACTITIONERS_COLLECTION)
|
|
430
|
+
.doc(practitionerId);
|
|
431
|
+
|
|
432
|
+
// Create working hours object from the invite's proposed working hours
|
|
433
|
+
const workingHours: PractitionerClinicWorkingHours = {
|
|
434
|
+
clinicId: clinicInfo.id,
|
|
435
|
+
workingHours: invite.proposedWorkingHours,
|
|
436
|
+
isActive: true,
|
|
437
|
+
createdAt: admin.firestore.Timestamp.now() as any,
|
|
438
|
+
updatedAt: admin.firestore.Timestamp.now() as any,
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
// Update practitioner document with new clinic and working hours
|
|
442
|
+
await practitionerRef.update({
|
|
443
|
+
clinics: admin.firestore.FieldValue.arrayUnion(clinicInfo.id),
|
|
444
|
+
clinicsInfo: admin.firestore.FieldValue.arrayUnion(clinicInfo),
|
|
445
|
+
clinicWorkingHours: admin.firestore.FieldValue.arrayUnion(workingHours),
|
|
446
|
+
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
Logger.info(
|
|
450
|
+
`[PractitionerInviteAggService] Successfully added clinic ${clinicInfo.id} to practitioner ${practitionerId}`
|
|
451
|
+
);
|
|
452
|
+
} catch (error) {
|
|
453
|
+
Logger.error(
|
|
454
|
+
`[PractitionerInviteAggService] Error adding clinic to practitioner:`,
|
|
455
|
+
error
|
|
456
|
+
);
|
|
457
|
+
throw error;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Updates the working hours for an existing practitioner-clinic relationship.
|
|
463
|
+
* @param {string} practitionerId - The practitioner ID
|
|
464
|
+
* @param {PractitionerInvite} invite - The accepted invite containing new working hours
|
|
465
|
+
* @returns {Promise<void>}
|
|
466
|
+
*/
|
|
467
|
+
private async updatePractitionerWorkingHours(
|
|
468
|
+
practitionerId: string,
|
|
469
|
+
invite: PractitionerInvite
|
|
470
|
+
): Promise<void> {
|
|
471
|
+
Logger.info(
|
|
472
|
+
`[PractitionerInviteAggService] Updating working hours for practitioner ${practitionerId} at clinic ${invite.clinicId}`
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
try {
|
|
476
|
+
const practitionerRef = this.db
|
|
477
|
+
.collection(PRACTITIONERS_COLLECTION)
|
|
478
|
+
.doc(practitionerId);
|
|
479
|
+
|
|
480
|
+
// Get current practitioner data
|
|
481
|
+
const practitionerDoc = await practitionerRef.get();
|
|
482
|
+
if (!practitionerDoc.exists) {
|
|
483
|
+
throw new Error(`Practitioner ${practitionerId} not found`);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const practitionerData = practitionerDoc.data() as Practitioner;
|
|
487
|
+
const currentWorkingHours = practitionerData.clinicWorkingHours || [];
|
|
488
|
+
|
|
489
|
+
// Find and update the working hours for this clinic
|
|
490
|
+
const updatedWorkingHours = currentWorkingHours.map((wh) => {
|
|
491
|
+
if (wh.clinicId === invite.clinicId) {
|
|
492
|
+
return {
|
|
493
|
+
...wh,
|
|
494
|
+
workingHours: invite.proposedWorkingHours,
|
|
495
|
+
isActive: true,
|
|
496
|
+
updatedAt: admin.firestore.Timestamp.now() as any,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
return wh;
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
// If working hours for this clinic don't exist, add them
|
|
503
|
+
if (!updatedWorkingHours.some((wh) => wh.clinicId === invite.clinicId)) {
|
|
504
|
+
updatedWorkingHours.push({
|
|
505
|
+
clinicId: invite.clinicId,
|
|
506
|
+
workingHours: invite.proposedWorkingHours,
|
|
507
|
+
isActive: true,
|
|
508
|
+
createdAt: admin.firestore.Timestamp.now() as any,
|
|
509
|
+
updatedAt: admin.firestore.Timestamp.now() as any,
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Update the practitioner document
|
|
514
|
+
await practitionerRef.update({
|
|
515
|
+
clinicWorkingHours: updatedWorkingHours,
|
|
516
|
+
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
Logger.info(
|
|
520
|
+
`[PractitionerInviteAggService] Successfully updated working hours for practitioner ${practitionerId} at clinic ${invite.clinicId}`
|
|
521
|
+
);
|
|
522
|
+
} catch (error) {
|
|
523
|
+
Logger.error(
|
|
524
|
+
`[PractitionerInviteAggService] Error updating practitioner working hours:`,
|
|
525
|
+
error
|
|
526
|
+
);
|
|
527
|
+
throw error;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// --- Data Fetching Helpers ---
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Fetches a practitioner by ID.
|
|
535
|
+
* @param practitionerId The practitioner ID.
|
|
536
|
+
* @returns {Promise<Practitioner | null>} The practitioner or null if not found.
|
|
537
|
+
*/
|
|
538
|
+
private async fetchPractitionerById(
|
|
539
|
+
practitionerId: string
|
|
540
|
+
): Promise<Practitioner | null> {
|
|
541
|
+
try {
|
|
542
|
+
const doc = await this.db
|
|
543
|
+
.collection(PRACTITIONERS_COLLECTION)
|
|
544
|
+
.doc(practitionerId)
|
|
545
|
+
.get();
|
|
546
|
+
return doc.exists ? (doc.data() as Practitioner) : null;
|
|
547
|
+
} catch (error) {
|
|
548
|
+
Logger.error(
|
|
549
|
+
`[PractitionerInviteAggService] Error fetching practitioner ${practitionerId}:`,
|
|
550
|
+
error
|
|
551
|
+
);
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Fetches a clinic by ID.
|
|
558
|
+
* @param clinicId The clinic ID.
|
|
559
|
+
* @returns {Promise<Clinic | null>} The clinic or null if not found.
|
|
560
|
+
*/
|
|
561
|
+
private async fetchClinicById(clinicId: string): Promise<Clinic | null> {
|
|
562
|
+
try {
|
|
563
|
+
const doc = await this.db
|
|
564
|
+
.collection(CLINICS_COLLECTION)
|
|
565
|
+
.doc(clinicId)
|
|
566
|
+
.get();
|
|
567
|
+
return doc.exists ? (doc.data() as Clinic) : null;
|
|
568
|
+
} catch (error) {
|
|
569
|
+
Logger.error(
|
|
570
|
+
`[PractitionerInviteAggService] Error fetching clinic ${clinicId}:`,
|
|
571
|
+
error
|
|
572
|
+
);
|
|
573
|
+
return null;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
package/src/admin/index.ts
CHANGED
|
@@ -11,6 +11,10 @@ import {
|
|
|
11
11
|
PractitionerToken,
|
|
12
12
|
PractitionerTokenStatus,
|
|
13
13
|
} from "../types/practitioner";
|
|
14
|
+
import {
|
|
15
|
+
PractitionerInvite,
|
|
16
|
+
PractitionerInviteStatus,
|
|
17
|
+
} from "../types/clinic/practitioner-invite.types";
|
|
14
18
|
import { DoctorInfo } from "../types/clinic";
|
|
15
19
|
import { Procedure, ProcedureSummaryInfo } from "../types/procedure";
|
|
16
20
|
import { PatientProfile } from "../types/patient";
|
|
@@ -19,6 +23,7 @@ import { Appointment, AppointmentStatus } from "../types/appointment";
|
|
|
19
23
|
// Explicitly import the services to re-export them by name
|
|
20
24
|
import { ClinicAggregationService } from "./aggregation/clinic/clinic.aggregation.service";
|
|
21
25
|
import { PractitionerAggregationService } from "./aggregation/practitioner/practitioner.aggregation.service";
|
|
26
|
+
import { PractitionerInviteAggregationService } from "./aggregation/practitioner-invite/practitioner-invite.aggregation.service";
|
|
22
27
|
import { ProcedureAggregationService } from "./aggregation/procedure/procedure.aggregation.service";
|
|
23
28
|
import { PatientAggregationService } from "./aggregation/patient/patient.aggregation.service";
|
|
24
29
|
import { AppointmentAggregationService } from "./aggregation/appointment/appointment.aggregation.service";
|
|
@@ -48,6 +53,7 @@ export type {
|
|
|
48
53
|
export type { Clinic, ClinicLocation } from "../types/clinic";
|
|
49
54
|
export type { ClinicInfo } from "../types/profile";
|
|
50
55
|
export type { Practitioner, PractitionerToken } from "../types/practitioner";
|
|
56
|
+
export type { PractitionerInvite } from "../types/clinic/practitioner-invite.types";
|
|
51
57
|
export type { DoctorInfo } from "../types/clinic";
|
|
52
58
|
export type { Procedure, ProcedureSummaryInfo } from "../types/procedure";
|
|
53
59
|
export type { PatientProfile as Patient } from "../types/patient";
|
|
@@ -63,6 +69,7 @@ export {
|
|
|
63
69
|
} from "../types/notifications";
|
|
64
70
|
export { UserRole } from "../types";
|
|
65
71
|
export { PractitionerTokenStatus } from "../types/practitioner";
|
|
72
|
+
export { PractitionerInviteStatus } from "../types/clinic/practitioner-invite.types";
|
|
66
73
|
export { AppointmentStatus } from "../types/appointment";
|
|
67
74
|
|
|
68
75
|
// Export admin classes/services explicitly by name
|
|
@@ -70,6 +77,7 @@ export {
|
|
|
70
77
|
NotificationsAdmin,
|
|
71
78
|
ClinicAggregationService,
|
|
72
79
|
PractitionerAggregationService,
|
|
80
|
+
PractitionerInviteAggregationService,
|
|
73
81
|
ProcedureAggregationService,
|
|
74
82
|
PatientAggregationService,
|
|
75
83
|
AppointmentAggregationService,
|
|
@@ -233,11 +233,11 @@ export interface SearchCalendarEventsParams {
|
|
|
233
233
|
*/
|
|
234
234
|
export interface CreateBlockingEventParams {
|
|
235
235
|
entityType: "practitioner" | "clinic";
|
|
236
|
-
entityId: string;
|
|
236
|
+
entityId: string; // Profile ID of clinic or practitioner, not userRef
|
|
237
237
|
eventName: string;
|
|
238
238
|
eventTime: CalendarEventTime;
|
|
239
239
|
eventType:
|
|
240
|
-
| CalendarEventType.BLOCKING
|
|
240
|
+
| CalendarEventType.BLOCKING // Let's only use this enum for doctor's app, then handle more information through description (eg "Holiday", "Break", "Free Day", "Other")
|
|
241
241
|
| CalendarEventType.BREAK
|
|
242
242
|
| CalendarEventType.FREE_DAY
|
|
243
243
|
| CalendarEventType.OTHER;
|