@blackcode_sa/metaestetics-api 1.14.57 → 1.14.59

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.
@@ -429,6 +429,7 @@ var NotificationType = /* @__PURE__ */ ((NotificationType2) => {
429
429
  NotificationType2["APPOINTMENT_REMINDER"] = "appointmentReminder";
430
430
  NotificationType2["APPOINTMENT_STATUS_CHANGE"] = "appointmentStatusChange";
431
431
  NotificationType2["APPOINTMENT_RESCHEDULED_PROPOSAL"] = "appointmentRescheduledProposal";
432
+ NotificationType2["APPOINTMENT_RESCHEDULED_REMINDER"] = "appointmentRescheduledReminder";
432
433
  NotificationType2["APPOINTMENT_CANCELLED"] = "appointmentCancelled";
433
434
  NotificationType2["PRE_REQUIREMENT_INSTRUCTION_DUE"] = "preRequirementInstructionDue";
434
435
  NotificationType2["POST_REQUIREMENT_INSTRUCTION_DUE"] = "postRequirementInstructionDue";
@@ -1124,6 +1125,50 @@ var NotificationsAdmin = class {
1124
1125
  return null;
1125
1126
  }
1126
1127
  }
1128
+ /**
1129
+ * Sends a reminder push notification for a pending reschedule request.
1130
+ * Used when a clinic has proposed a reschedule and the patient hasn't responded.
1131
+ * @param appointment The appointment with pending reschedule.
1132
+ * @param patientUserId The ID of the patient.
1133
+ * @param patientExpoTokens Array of Expo push tokens for the patient.
1134
+ * @param reminderCount Optional count of reminders already sent (for tracking).
1135
+ */
1136
+ async sendRescheduleReminderPush(appointment, patientUserId, patientExpoTokens, reminderCount) {
1137
+ if (!patientExpoTokens || patientExpoTokens.length === 0) {
1138
+ console.log(
1139
+ `[NotificationsAdmin] No expo tokens for patient ${patientUserId} for appointment ${appointment.id} reschedule reminder. Skipping push.`
1140
+ );
1141
+ return null;
1142
+ }
1143
+ const title = "Reminder: Reschedule Request Pending";
1144
+ const body = `You have a pending reschedule request for your ${appointment.procedureInfo.name} appointment. Please respond in the app.`;
1145
+ const notificationTimestampForDb = admin2.firestore.Timestamp.now();
1146
+ const notificationData = {
1147
+ userId: patientUserId,
1148
+ userRole: "patient" /* PATIENT */,
1149
+ notificationType: "appointmentRescheduledReminder" /* APPOINTMENT_RESCHEDULED_REMINDER */,
1150
+ notificationTime: notificationTimestampForDb,
1151
+ notificationTokens: patientExpoTokens,
1152
+ title,
1153
+ body,
1154
+ appointmentId: appointment.id
1155
+ };
1156
+ try {
1157
+ const notificationId = await this.createNotification(
1158
+ notificationData
1159
+ );
1160
+ console.log(
1161
+ `[NotificationsAdmin] Created APPOINTMENT_RESCHEDULED_REMINDER notification ${notificationId} for patient ${patientUserId}. Reminder count: ${reminderCount != null ? reminderCount : 1}.`
1162
+ );
1163
+ return notificationId;
1164
+ } catch (error) {
1165
+ console.error(
1166
+ `[NotificationsAdmin] Error creating APPOINTMENT_RESCHEDULED_REMINDER notification for patient ${patientUserId}:`,
1167
+ error
1168
+ );
1169
+ return null;
1170
+ }
1171
+ }
1127
1172
  };
1128
1173
 
1129
1174
  // src/admin/requirements/patient-requirements.admin.service.ts
@@ -2287,233 +2332,1055 @@ var clinicAppointmentRequestedTemplate = `
2287
2332
  </body>
2288
2333
  </html>
2289
2334
  `;
2290
- var AppointmentMailingService = class extends BaseMailingService {
2291
- constructor(firestore19, mailgunClient) {
2292
- super(firestore19, mailgunClient);
2293
- this.DEFAULT_MAILGUN_DOMAIN = "mg.metaesthetics.net";
2294
- Logger.info("[AppointmentMailingService] Initialized.");
2295
- }
2296
- /**
2297
- * Formats a Firestore Timestamp in the clinic's timezone
2298
- * @param timestamp - Firestore Timestamp (UTC)
2299
- * @param clinicTimezone - IANA timezone string (e.g., "Europe/Zurich")
2300
- * @param format - Format type: 'date', 'time', or 'datetime'
2301
- * @returns Formatted string in clinic's local timezone
2302
- */
2303
- formatTimestampInClinicTimezone(timestamp, clinicTimezone, format = "datetime") {
2304
- try {
2305
- const dateTimeInClinicTz = DateTime.fromMillis(timestamp.toMillis(), {
2306
- zone: clinicTimezone
2307
- });
2308
- switch (format) {
2309
- case "date":
2310
- return dateTimeInClinicTz.toLocaleString(DateTime.DATE_FULL);
2311
- case "time":
2312
- return dateTimeInClinicTz.toLocaleString(DateTime.TIME_SIMPLE);
2313
- case "datetime":
2314
- return dateTimeInClinicTz.toLocaleString(DateTime.DATETIME_FULL);
2315
- default:
2316
- return dateTimeInClinicTz.toLocaleString(DateTime.DATETIME_FULL);
2317
- }
2318
- } catch (error) {
2319
- Logger.error("[AppointmentMailingService] Error formatting timestamp in clinic timezone:", {
2320
- error: error instanceof Error ? error.message : String(error),
2321
- clinicTimezone
2322
- });
2323
- return timestamp.toDate().toLocaleString();
2324
- }
2325
- }
2326
- /**
2327
- * Gets a user-friendly display name for a timezone
2328
- * @param timezone - IANA timezone string (e.g., "Europe/Zurich")
2329
- * @returns User-friendly timezone name (e.g., "Clinic Time - Europe/Zurich")
2330
- */
2331
- getTimezoneDisplayName(timezone) {
2332
- try {
2333
- const timezoneMap = {
2334
- "Europe/Zurich": "Clinic Time - Central European Time",
2335
- "Europe/London": "Clinic Time - GMT/BST",
2336
- "America/New_York": "Clinic Time - Eastern Time",
2337
- "America/Los_Angeles": "Clinic Time - Pacific Time",
2338
- "Asia/Dubai": "Clinic Time - Gulf Standard Time",
2339
- "Asia/Karachi": "Clinic Time - Pakistan Standard Time",
2340
- "Asia/Kolkata": "Clinic Time - India Standard Time",
2341
- "Australia/Sydney": "Clinic Time - Australian Eastern Time"
2342
- };
2343
- return timezoneMap[timezone] || `Clinic Time - ${timezone}`;
2344
- } catch (error) {
2345
- Logger.error("[AppointmentMailingService] Error getting timezone display name:", {
2346
- error: error instanceof Error ? error.message : String(error),
2347
- timezone
2348
- });
2349
- return `Clinic Time - ${timezone}`;
2350
- }
2351
- }
2352
- async sendAppointmentConfirmedEmail(data) {
2353
- var _a, _b, _c, _d, _e;
2354
- Logger.info(
2355
- `[AppointmentMailingService] Preparing to send appointment confirmation email to ${data.recipientRole}: ${data.recipientProfile.id}`
2356
- );
2357
- const recipientEmail = data.recipientProfile.email;
2358
- if (!recipientEmail) {
2359
- Logger.error("[AppointmentMailingService] Recipient email not found for confirmation.", {
2360
- recipientId: data.recipientProfile.id,
2361
- role: data.recipientRole
2362
- });
2363
- throw new Error("Recipient email address is missing.");
2364
- }
2365
- const clinicTimezone = data.appointment.clinic_tz || "UTC";
2366
- Logger.debug("[AppointmentMailingService] Formatting appointment time", {
2367
- clinicTimezone,
2368
- utcTime: data.appointment.appointmentStartTime.toDate().toISOString()
2369
- });
2370
- const formattedTime = this.formatTimestampInClinicTimezone(
2371
- data.appointment.appointmentStartTime,
2372
- clinicTimezone,
2373
- "time"
2374
- );
2375
- const timezoneName = this.getTimezoneDisplayName(clinicTimezone);
2376
- const templateVariables = {
2377
- patientName: data.appointment.patientInfo.fullName,
2378
- procedureName: data.appointment.procedureInfo.name,
2379
- appointmentDate: this.formatTimestampInClinicTimezone(
2380
- data.appointment.appointmentStartTime,
2381
- clinicTimezone,
2382
- "date"
2383
- ),
2384
- appointmentTime: `${formattedTime} (${timezoneName})`,
2385
- practitionerName: data.appointment.practitionerInfo.name,
2386
- clinicName: data.appointment.clinicInfo.name
2387
- };
2388
- const html = this.renderTemplate(patientAppointmentConfirmedTemplate, templateVariables);
2389
- const subject = ((_a = data.options) == null ? void 0 : _a.customSubject) || "Your Appointment is Confirmed!";
2390
- const fromAddress = ((_b = data.options) == null ? void 0 : _b.fromAddress) || `MetaEstetics <no-reply@${((_c = data.options) == null ? void 0 : _c.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN}>`;
2391
- const domainToSendFrom = ((_d = data.options) == null ? void 0 : _d.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN;
2392
- const mailgunSendData = {
2393
- to: recipientEmail,
2394
- from: fromAddress,
2395
- subject,
2396
- html
2397
- };
2398
- try {
2399
- const result = await this.sendEmail(domainToSendFrom, mailgunSendData);
2400
- await this.logEmailAttempt(
2401
- { to: recipientEmail, subject, templateName: "appointment_confirmed" },
2402
- true
2403
- );
2404
- return result;
2405
- } catch (error) {
2406
- await this.logEmailAttempt(
2407
- {
2408
- to: recipientEmail,
2409
- subject: ((_e = data.options) == null ? void 0 : _e.customSubject) || "Your Appointment is Confirmed!",
2410
- templateName: "appointment_confirmed"
2411
- },
2412
- false,
2413
- error
2414
- );
2415
- throw error;
2416
- }
2417
- }
2418
- async sendAppointmentRequestedEmailToClinic(data) {
2419
- var _a, _b, _c, _d, _e, _f;
2420
- Logger.info(
2421
- `[AppointmentMailingService] Preparing to send appointment requested email to clinic: ${data.clinicProfile.id}`
2422
- );
2423
- const clinicEmail = (_a = data.clinicProfile.contactInfo) == null ? void 0 : _a.email;
2424
- if (!clinicEmail) {
2425
- Logger.error(
2426
- "[AppointmentMailingService] Clinic contact email not found for request notification.",
2427
- { clinicId: data.clinicProfile.id }
2428
- );
2429
- throw new Error("Clinic contact email address is missing.");
2430
- }
2431
- const clinicTimezone = data.appointment.clinic_tz || "UTC";
2432
- Logger.debug("[AppointmentMailingService] Formatting appointment time for clinic", {
2433
- clinicTimezone,
2434
- utcTime: data.appointment.appointmentStartTime.toDate().toISOString()
2435
- });
2436
- const formattedTime = this.formatTimestampInClinicTimezone(
2437
- data.appointment.appointmentStartTime,
2438
- clinicTimezone,
2439
- "time"
2440
- );
2441
- const timezoneName = this.getTimezoneDisplayName(clinicTimezone);
2442
- const templateVariables = {
2443
- clinicName: data.clinicProfile.name,
2444
- patientName: data.appointment.patientInfo.fullName,
2445
- procedureName: data.appointment.procedureInfo.name,
2446
- appointmentDate: this.formatTimestampInClinicTimezone(
2447
- data.appointment.appointmentStartTime,
2448
- clinicTimezone,
2449
- "date"
2450
- ),
2451
- appointmentTime: `${formattedTime} (${timezoneName})`,
2452
- practitionerName: data.appointment.practitionerInfo.name
2453
- };
2454
- const html = this.renderTemplate(clinicAppointmentRequestedTemplate, templateVariables);
2455
- const subject = ((_b = data.options) == null ? void 0 : _b.customSubject) || "New Appointment Request Received";
2456
- const fromAddress = ((_c = data.options) == null ? void 0 : _c.fromAddress) || `MetaEstetics <no-reply@${((_d = data.options) == null ? void 0 : _d.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN}>`;
2457
- const domainToSendFrom = ((_e = data.options) == null ? void 0 : _e.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN;
2458
- const mailgunSendData = {
2459
- to: clinicEmail,
2460
- from: fromAddress,
2461
- subject,
2462
- html
2463
- };
2464
- try {
2465
- const result = await this.sendEmail(domainToSendFrom, mailgunSendData);
2466
- await this.logEmailAttempt(
2467
- {
2468
- to: clinicEmail,
2469
- subject,
2470
- templateName: "appointment_requested_clinic"
2471
- },
2472
- true
2473
- );
2474
- return result;
2475
- } catch (error) {
2476
- await this.logEmailAttempt(
2477
- {
2478
- to: clinicEmail,
2479
- subject: ((_f = data.options) == null ? void 0 : _f.customSubject) || "New Appointment Request Received",
2480
- templateName: "appointment_requested_clinic"
2481
- },
2482
- false,
2483
- error
2484
- );
2485
- throw error;
2486
- }
2487
- }
2488
- async sendAppointmentCancelledEmail(data) {
2489
- Logger.info(
2490
- `[AppointmentMailingService] Placeholder for sendAppointmentCancelledEmail for ${data.recipientRole}: ${data.recipientProfile.id}`
2491
- );
2492
- return Promise.resolve();
2493
- }
2494
- async sendAppointmentRescheduledProposalEmail(data) {
2495
- Logger.info(
2496
- `[AppointmentMailingService] Placeholder for sendAppointmentRescheduledProposalEmail to patient: ${data.patientProfile.id}`
2497
- );
2498
- return Promise.resolve();
2499
- }
2500
- async sendReviewRequestEmail(data) {
2501
- Logger.info(
2502
- `[AppointmentMailingService] Placeholder for sendReviewRequestEmail to patient: ${data.patientProfile.id}`
2503
- );
2504
- return Promise.resolve();
2505
- }
2506
- async sendReviewAddedEmail(data) {
2507
- Logger.info(
2508
- `[AppointmentMailingService] Placeholder for sendReviewAddedEmail to ${data.recipientRole}: ${data.recipientProfile.id}`
2509
- );
2510
- return Promise.resolve();
2511
- }
2512
- };
2513
-
2514
- // src/admin/aggregation/appointment/appointment.aggregation.service.ts
2515
- var AppointmentAggregationService = class {
2516
- /**
2335
+ var appointmentCancelledTemplate = `
2336
+ <!DOCTYPE html>
2337
+ <html lang="en">
2338
+ <head>
2339
+ <meta charset="UTF-8">
2340
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
2341
+ <title>Appointment Cancelled</title>
2342
+ <style>
2343
+ body {
2344
+ margin: 0;
2345
+ padding: 0;
2346
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
2347
+ background: linear-gradient(135deg, #d4736c 0%, #b85450 100%);
2348
+ min-height: 100vh;
2349
+ }
2350
+ .email-container {
2351
+ max-width: 600px;
2352
+ margin: 0 auto;
2353
+ background: #ffffff;
2354
+ border-radius: 20px;
2355
+ overflow: hidden;
2356
+ box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
2357
+ margin-top: 40px;
2358
+ margin-bottom: 40px;
2359
+ }
2360
+ .header {
2361
+ background: linear-gradient(135deg, #d4736c 0%, #b85450 100%);
2362
+ padding: 40px 30px;
2363
+ text-align: center;
2364
+ color: white;
2365
+ }
2366
+ .header h1 {
2367
+ margin: 0;
2368
+ font-size: 28px;
2369
+ font-weight: 300;
2370
+ letter-spacing: 1px;
2371
+ }
2372
+ .header .subtitle {
2373
+ margin: 10px 0 0 0;
2374
+ font-size: 16px;
2375
+ opacity: 0.9;
2376
+ font-weight: 300;
2377
+ }
2378
+ .content {
2379
+ padding: 40px 30px;
2380
+ }
2381
+ .greeting {
2382
+ font-size: 18px;
2383
+ color: #333;
2384
+ margin-bottom: 25px;
2385
+ font-weight: 400;
2386
+ }
2387
+ .cancellation-notice {
2388
+ background: linear-gradient(135deg, #ffebee 0%, #ffcdd2 100%);
2389
+ border-radius: 15px;
2390
+ padding: 25px;
2391
+ margin: 25px 0;
2392
+ border-left: 5px solid #d4736c;
2393
+ }
2394
+ .cancellation-notice p {
2395
+ margin: 0;
2396
+ color: #c62828;
2397
+ font-size: 15px;
2398
+ font-weight: 500;
2399
+ line-height: 1.6;
2400
+ }
2401
+ .cancelled-by-info {
2402
+ background: #fafafa;
2403
+ border-radius: 10px;
2404
+ padding: 15px 20px;
2405
+ margin-top: 15px;
2406
+ }
2407
+ .cancelled-by-info .label {
2408
+ font-size: 12px;
2409
+ color: #757575;
2410
+ text-transform: uppercase;
2411
+ letter-spacing: 0.5px;
2412
+ margin-bottom: 5px;
2413
+ }
2414
+ .cancelled-by-info .value {
2415
+ font-size: 14px;
2416
+ color: #424242;
2417
+ font-weight: 500;
2418
+ }
2419
+ .reason-box {
2420
+ background: linear-gradient(135deg, #fff8e1 0%, #ffecb3 100%);
2421
+ border-radius: 15px;
2422
+ padding: 20px;
2423
+ margin: 20px 0;
2424
+ border-left: 5px solid #ffa000;
2425
+ }
2426
+ .reason-box .label {
2427
+ font-size: 14px;
2428
+ font-weight: 600;
2429
+ color: #e65100;
2430
+ margin-bottom: 8px;
2431
+ }
2432
+ .reason-box .reason-text {
2433
+ font-size: 15px;
2434
+ color: #424242;
2435
+ line-height: 1.6;
2436
+ font-style: italic;
2437
+ }
2438
+ .appointment-card {
2439
+ background: linear-gradient(135deg, #f5f5f5 0%, #eeeeee 100%);
2440
+ border-radius: 15px;
2441
+ padding: 30px;
2442
+ margin: 25px 0;
2443
+ border-left: 5px solid #9e9e9e;
2444
+ opacity: 0.9;
2445
+ }
2446
+ .appointment-title {
2447
+ font-size: 20px;
2448
+ color: #757575;
2449
+ margin-bottom: 20px;
2450
+ font-weight: 600;
2451
+ }
2452
+ .appointment-details {
2453
+ display: grid;
2454
+ gap: 15px;
2455
+ }
2456
+ .detail-row {
2457
+ display: flex;
2458
+ align-items: center;
2459
+ padding: 8px 0;
2460
+ }
2461
+ .detail-label {
2462
+ font-weight: 600;
2463
+ color: #757575;
2464
+ min-width: 120px;
2465
+ font-size: 14px;
2466
+ }
2467
+ .detail-value {
2468
+ color: #616161;
2469
+ font-size: 16px;
2470
+ font-weight: 500;
2471
+ text-decoration: line-through;
2472
+ }
2473
+ .procedure-name {
2474
+ color: #757575;
2475
+ font-weight: 600;
2476
+ }
2477
+ .clinic-name {
2478
+ color: #9e9e9e;
2479
+ font-weight: 600;
2480
+ }
2481
+ .rebook-section {
2482
+ background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
2483
+ border-radius: 15px;
2484
+ padding: 25px;
2485
+ margin: 30px 0;
2486
+ text-align: center;
2487
+ border-left: 5px solid #4caf50;
2488
+ }
2489
+ .rebook-section h3 {
2490
+ margin: 0 0 15px 0;
2491
+ color: #2e7d32;
2492
+ font-weight: 600;
2493
+ font-size: 18px;
2494
+ }
2495
+ .rebook-section p {
2496
+ margin: 0;
2497
+ color: #555;
2498
+ font-size: 15px;
2499
+ line-height: 1.6;
2500
+ }
2501
+ .support-section {
2502
+ background: #f8f9fa;
2503
+ border-radius: 15px;
2504
+ padding: 20px;
2505
+ margin: 25px 0;
2506
+ text-align: center;
2507
+ }
2508
+ .support-section h4 {
2509
+ margin: 0 0 10px 0;
2510
+ color: #555;
2511
+ font-weight: 600;
2512
+ font-size: 16px;
2513
+ }
2514
+ .support-section p {
2515
+ margin: 0;
2516
+ color: #757575;
2517
+ font-size: 14px;
2518
+ line-height: 1.6;
2519
+ }
2520
+ .footer {
2521
+ background: #f8f9fa;
2522
+ padding: 25px 30px;
2523
+ text-align: center;
2524
+ color: #666;
2525
+ font-size: 14px;
2526
+ border-top: 1px solid #eee;
2527
+ }
2528
+ .logo {
2529
+ font-size: 24px;
2530
+ font-weight: 700;
2531
+ color: white;
2532
+ margin-bottom: 5px;
2533
+ }
2534
+ .divider {
2535
+ height: 2px;
2536
+ background: linear-gradient(90deg, #d4736c, #b85450);
2537
+ margin: 25px 0;
2538
+ border-radius: 1px;
2539
+ }
2540
+ .icon {
2541
+ text-align: center;
2542
+ margin: 20px 0;
2543
+ font-size: 48px;
2544
+ }
2545
+ </style>
2546
+ </head>
2547
+ <body>
2548
+ <div class="email-container">
2549
+ <div class="header">
2550
+ <div class="logo">MetaEstetics</div>
2551
+ <h1>Appointment Cancelled</h1>
2552
+ <div class="subtitle">We're Sorry to See This Change</div>
2553
+ </div>
2554
+
2555
+ <div class="content">
2556
+ <div class="icon">&#10060;</div>
2557
+
2558
+ <div class="greeting">
2559
+ Dear <strong>{{recipientName}}</strong>,
2560
+ </div>
2561
+
2562
+ <div class="cancellation-notice">
2563
+ <p><strong>Your appointment has been cancelled.</strong> We wanted to let you know that the following appointment is no longer scheduled.</p>
2564
+ <div class="cancelled-by-info">
2565
+ <div class="label">Cancelled By</div>
2566
+ <div class="value">{{cancelledByDisplay}}</div>
2567
+ </div>
2568
+ </div>
2569
+
2570
+ {{#if cancellationReason}}
2571
+ <div class="reason-box">
2572
+ <div class="label">Reason for Cancellation</div>
2573
+ <div class="reason-text">"{{cancellationReason}}"</div>
2574
+ </div>
2575
+ {{/if}}
2576
+
2577
+ <div class="appointment-card">
2578
+ <div class="appointment-title">Cancelled Appointment Details</div>
2579
+ <div class="appointment-details">
2580
+ <div class="detail-row">
2581
+ <div class="detail-label">Procedure:</div>
2582
+ <div class="detail-value procedure-name">{{procedureName}}</div>
2583
+ </div>
2584
+ <div class="detail-row">
2585
+ <div class="detail-label">Date:</div>
2586
+ <div class="detail-value">{{appointmentDate}}</div>
2587
+ </div>
2588
+ <div class="detail-row">
2589
+ <div class="detail-label">Time:</div>
2590
+ <div class="detail-value">{{appointmentTime}}</div>
2591
+ </div>
2592
+ <div class="detail-row">
2593
+ <div class="detail-label">Practitioner:</div>
2594
+ <div class="detail-value">{{practitionerName}}</div>
2595
+ </div>
2596
+ <div class="detail-row">
2597
+ <div class="detail-label">Location:</div>
2598
+ <div class="detail-value clinic-name">{{clinicName}}</div>
2599
+ </div>
2600
+ </div>
2601
+ </div>
2602
+
2603
+ <div class="divider"></div>
2604
+
2605
+ <div class="rebook-section">
2606
+ <h3>Would You Like to Reschedule?</h3>
2607
+ <p>
2608
+ We'd love to see you! If you'd like to book a new appointment,
2609
+ simply open the MetaEstetics app and browse available times that work for you.
2610
+ </p>
2611
+ </div>
2612
+
2613
+ <div class="support-section">
2614
+ <h4>Need Assistance?</h4>
2615
+ <p>
2616
+ If you have any questions about this cancellation or need help rebooking,
2617
+ please contact {{clinicName}} directly through the app or reach out to our support team.
2618
+ </p>
2619
+ </div>
2620
+ </div>
2621
+
2622
+ <div class="footer">
2623
+ <p style="margin: 0 0 10px 0;">
2624
+ <strong>MetaEstetics</strong> - Premium Aesthetic Services
2625
+ </p>
2626
+ <p style="margin: 0; font-size: 12px; color: #999;">
2627
+ This is an automated message. Please do not reply to this email.
2628
+ </p>
2629
+ </div>
2630
+ </div>
2631
+ </body>
2632
+ </html>
2633
+ `;
2634
+ var appointmentRescheduledProposalTemplate = `
2635
+ <!DOCTYPE html>
2636
+ <html lang="en">
2637
+ <head>
2638
+ <meta charset="UTF-8">
2639
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
2640
+ <title>Appointment Reschedule Proposal</title>
2641
+ <style>
2642
+ body {
2643
+ margin: 0;
2644
+ padding: 0;
2645
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
2646
+ background: linear-gradient(135deg, #a48a76 0%, #67574A 100%);
2647
+ min-height: 100vh;
2648
+ }
2649
+ .email-container {
2650
+ max-width: 600px;
2651
+ margin: 0 auto;
2652
+ background: #ffffff;
2653
+ border-radius: 20px;
2654
+ overflow: hidden;
2655
+ box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
2656
+ margin-top: 40px;
2657
+ margin-bottom: 40px;
2658
+ }
2659
+ .header {
2660
+ background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
2661
+ padding: 40px 30px;
2662
+ text-align: center;
2663
+ color: white;
2664
+ }
2665
+ .header h1 {
2666
+ margin: 0;
2667
+ font-size: 28px;
2668
+ font-weight: 300;
2669
+ letter-spacing: 1px;
2670
+ }
2671
+ .header .subtitle {
2672
+ margin: 10px 0 0 0;
2673
+ font-size: 16px;
2674
+ opacity: 0.9;
2675
+ font-weight: 300;
2676
+ }
2677
+ .content {
2678
+ padding: 40px 30px;
2679
+ }
2680
+ .greeting {
2681
+ font-size: 18px;
2682
+ color: #333;
2683
+ margin-bottom: 25px;
2684
+ font-weight: 400;
2685
+ }
2686
+ .info-box {
2687
+ background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);
2688
+ border-radius: 15px;
2689
+ padding: 25px;
2690
+ margin: 25px 0;
2691
+ border-left: 5px solid #ff9800;
2692
+ }
2693
+ .info-box p {
2694
+ margin: 0;
2695
+ color: #e65100;
2696
+ font-size: 15px;
2697
+ font-weight: 500;
2698
+ line-height: 1.6;
2699
+ }
2700
+ .time-comparison {
2701
+ display: grid;
2702
+ gap: 20px;
2703
+ margin: 25px 0;
2704
+ }
2705
+ .time-card {
2706
+ background: linear-gradient(135deg, #f8f6f5 0%, #f5f3f2 100%);
2707
+ border-radius: 15px;
2708
+ padding: 25px;
2709
+ border-left: 5px solid #a48a76;
2710
+ }
2711
+ .time-card.old-time {
2712
+ border-left-color: #9e9e9e;
2713
+ opacity: 0.8;
2714
+ }
2715
+ .time-card.new-time {
2716
+ border-left-color: #ff9800;
2717
+ background: linear-gradient(135deg, #fff8e1 0%, #ffe0b2 100%);
2718
+ }
2719
+ .time-label {
2720
+ font-size: 14px;
2721
+ font-weight: 600;
2722
+ color: #666;
2723
+ text-transform: uppercase;
2724
+ letter-spacing: 0.5px;
2725
+ margin-bottom: 10px;
2726
+ }
2727
+ .time-label.old {
2728
+ color: #757575;
2729
+ }
2730
+ .time-label.new {
2731
+ color: #f57c00;
2732
+ }
2733
+ .appointment-card {
2734
+ background: linear-gradient(135deg, #f8f6f5 0%, #f5f3f2 100%);
2735
+ border-radius: 15px;
2736
+ padding: 30px;
2737
+ margin: 25px 0;
2738
+ border-left: 5px solid #a48a76;
2739
+ }
2740
+ .appointment-title {
2741
+ font-size: 20px;
2742
+ color: #a48a76;
2743
+ margin-bottom: 20px;
2744
+ font-weight: 600;
2745
+ }
2746
+ .appointment-details {
2747
+ display: grid;
2748
+ gap: 15px;
2749
+ }
2750
+ .detail-row {
2751
+ display: flex;
2752
+ align-items: center;
2753
+ padding: 8px 0;
2754
+ }
2755
+ .detail-label {
2756
+ font-weight: 600;
2757
+ color: #555;
2758
+ min-width: 120px;
2759
+ font-size: 14px;
2760
+ }
2761
+ .detail-value {
2762
+ color: #333;
2763
+ font-size: 16px;
2764
+ font-weight: 500;
2765
+ }
2766
+ .procedure-name {
2767
+ color: #67574A;
2768
+ font-weight: 600;
2769
+ }
2770
+ .clinic-name {
2771
+ color: #a48a76;
2772
+ font-weight: 600;
2773
+ }
2774
+ .action-section {
2775
+ background: linear-gradient(135deg, #e8f5e9 0%, #c8e6c9 100%);
2776
+ border-radius: 15px;
2777
+ padding: 30px;
2778
+ margin: 30px 0;
2779
+ text-align: center;
2780
+ border-left: 5px solid #4caf50;
2781
+ }
2782
+ .action-section h3 {
2783
+ margin: 0 0 15px 0;
2784
+ color: #2e7d32;
2785
+ font-weight: 600;
2786
+ font-size: 18px;
2787
+ }
2788
+ .action-section p {
2789
+ margin: 0 0 20px 0;
2790
+ color: #555;
2791
+ font-size: 15px;
2792
+ line-height: 1.6;
2793
+ }
2794
+ .action-required-box {
2795
+ background: linear-gradient(135deg, #fff3e0 0%, #ffecb3 100%);
2796
+ border: 2px solid #ff9800;
2797
+ border-radius: 15px;
2798
+ padding: 25px;
2799
+ margin: 25px 0;
2800
+ text-align: center;
2801
+ }
2802
+ .action-required-box h3 {
2803
+ margin: 0 0 15px 0;
2804
+ color: #e65100;
2805
+ font-weight: 700;
2806
+ font-size: 18px;
2807
+ }
2808
+ .action-required-box p {
2809
+ margin: 0 0 12px 0;
2810
+ color: #bf360c;
2811
+ font-size: 15px;
2812
+ line-height: 1.6;
2813
+ }
2814
+ .action-required-box p:last-child {
2815
+ margin-bottom: 0;
2816
+ }
2817
+ .pending-notice {
2818
+ background: #fff8e1;
2819
+ border-radius: 8px;
2820
+ padding: 12px 15px;
2821
+ margin-top: 15px;
2822
+ display: inline-block;
2823
+ }
2824
+ .pending-notice p {
2825
+ margin: 0;
2826
+ color: #f57c00;
2827
+ font-size: 14px;
2828
+ font-weight: 600;
2829
+ }
2830
+ .footer {
2831
+ background: #f8f9fa;
2832
+ padding: 25px 30px;
2833
+ text-align: center;
2834
+ color: #666;
2835
+ font-size: 14px;
2836
+ border-top: 1px solid #eee;
2837
+ }
2838
+ .logo {
2839
+ font-size: 24px;
2840
+ font-weight: 700;
2841
+ color: white;
2842
+ margin-bottom: 5px;
2843
+ }
2844
+ .divider {
2845
+ height: 2px;
2846
+ background: linear-gradient(90deg, #a48a76, #67574A);
2847
+ margin: 25px 0;
2848
+ border-radius: 1px;
2849
+ }
2850
+ .icon {
2851
+ text-align: center;
2852
+ margin: 20px 0;
2853
+ font-size: 48px;
2854
+ }
2855
+ .arrow {
2856
+ text-align: center;
2857
+ font-size: 32px;
2858
+ color: #ff9800;
2859
+ margin: 10px 0;
2860
+ }
2861
+ </style>
2862
+ </head>
2863
+ <body>
2864
+ <div class="email-container">
2865
+ <div class="header">
2866
+ <div class="logo">MetaEstetics</div>
2867
+ <h1>Appointment Reschedule Proposal</h1>
2868
+ <div class="subtitle">Action Required</div>
2869
+ </div>
2870
+
2871
+ <div class="content">
2872
+ <div class="icon">\u{1F4C5}</div>
2873
+
2874
+ <div class="greeting">
2875
+ Dear <strong>{{patientName}}</strong>,
2876
+ </div>
2877
+
2878
+ <p style="color: #555; font-size: 16px; line-height: 1.6; margin-bottom: 25px;">
2879
+ We hope this message finds you well. We need to propose a new time for your upcoming appointment. Please review the details below and confirm if the new time works for you.
2880
+ </p>
2881
+
2882
+ <div class="info-box">
2883
+ <p><strong>\u26A0\uFE0F Important:</strong> Please respond to this reschedule proposal as soon as possible. Your appointment will remain pending until you confirm or reject the new time.</p>
2884
+ </div>
2885
+
2886
+ <div class="appointment-card">
2887
+ <div class="appointment-title">\u{1F4CB} Appointment Details</div>
2888
+ <div class="appointment-details">
2889
+ <div class="detail-row">
2890
+ <div class="detail-label">Procedure:</div>
2891
+ <div class="detail-value procedure-name">{{procedureName}}</div>
2892
+ </div>
2893
+ <div class="detail-row">
2894
+ <div class="detail-label">Practitioner:</div>
2895
+ <div class="detail-value">{{practitionerName}}</div>
2896
+ </div>
2897
+ <div class="detail-row">
2898
+ <div class="detail-label">Location:</div>
2899
+ <div class="detail-value clinic-name">{{clinicName}}</div>
2900
+ </div>
2901
+ </div>
2902
+ </div>
2903
+
2904
+ <div class="time-comparison">
2905
+ <div class="time-card old-time">
2906
+ <div class="time-label old">Previous Time</div>
2907
+ <div style="font-size: 18px; font-weight: 600; color: #424242; margin-bottom: 8px;">{{previousDate}}</div>
2908
+ <div style="font-size: 16px; color: #616161;">{{previousTime}}</div>
2909
+ </div>
2910
+
2911
+ <div class="arrow">\u2193</div>
2912
+
2913
+ <div class="time-card new-time">
2914
+ <div class="time-label new">Proposed New Time</div>
2915
+ <div style="font-size: 18px; font-weight: 600; color: #e65100; margin-bottom: 8px;">{{newDate}}</div>
2916
+ <div style="font-size: 16px; color: #f57c00; font-weight: 500;">{{newTime}}</div>
2917
+ </div>
2918
+ </div>
2919
+
2920
+ <div class="divider"></div>
2921
+
2922
+ <div class="action-required-box">
2923
+ <h3>Your Response is Required</h3>
2924
+ <p>
2925
+ <strong>Missed our notification?</strong> Please open the MetaEstetics app to confirm or reject this reschedule request.
2926
+ </p>
2927
+ <p>
2928
+ Please respond as soon as possible so we can finalize your appointment.
2929
+ </p>
2930
+ <div class="pending-notice">
2931
+ <p>Your appointment will remain pending until you respond.</p>
2932
+ </div>
2933
+ </div>
2934
+
2935
+ <div class="action-section">
2936
+ <h3>How to Respond</h3>
2937
+ <p>
2938
+ Open the MetaEstetics app and navigate to your appointments.
2939
+ If the new time works for you, simply tap "Accept Reschedule".
2940
+ If not, you can reject it and we'll work with you to find an alternative time.
2941
+ </p>
2942
+ </div>
2943
+
2944
+ <p style="color: #555; font-size: 14px; line-height: 1.6; margin-top: 25px;">
2945
+ <strong>Need Help?</strong> If you have any questions or concerns about this reschedule, please contact us directly through the app or reach out to {{clinicName}}.
2946
+ </p>
2947
+ </div>
2948
+
2949
+ <div class="footer">
2950
+ <p style="margin: 0 0 10px 0;">
2951
+ <strong>MetaEstetics</strong> - Premium Aesthetic Services
2952
+ </p>
2953
+ <p style="margin: 0; font-size: 12px; color: #999;">
2954
+ This is an automated message. Please do not reply to this email.
2955
+ </p>
2956
+ </div>
2957
+ </div>
2958
+ </body>
2959
+ </html>
2960
+ `;
2961
+ var AppointmentMailingService = class extends BaseMailingService {
2962
+ constructor(firestore19, mailgunClient) {
2963
+ super(firestore19, mailgunClient);
2964
+ this.DEFAULT_MAILGUN_DOMAIN = "mg.metaesthetics.net";
2965
+ Logger.info("[AppointmentMailingService] Initialized.");
2966
+ }
2967
+ /**
2968
+ * Formats a Firestore Timestamp in the clinic's timezone
2969
+ * @param timestamp - Firestore Timestamp (UTC)
2970
+ * @param clinicTimezone - IANA timezone string (e.g., "Europe/Zurich")
2971
+ * @param format - Format type: 'date', 'time', or 'datetime'
2972
+ * @returns Formatted string in clinic's local timezone
2973
+ */
2974
+ formatTimestampInClinicTimezone(timestamp, clinicTimezone, format = "datetime") {
2975
+ try {
2976
+ const dateTimeInClinicTz = DateTime.fromMillis(timestamp.toMillis(), {
2977
+ zone: clinicTimezone
2978
+ });
2979
+ switch (format) {
2980
+ case "date":
2981
+ return dateTimeInClinicTz.toLocaleString(DateTime.DATE_FULL);
2982
+ case "time":
2983
+ return dateTimeInClinicTz.toLocaleString(DateTime.TIME_SIMPLE);
2984
+ case "datetime":
2985
+ return dateTimeInClinicTz.toLocaleString(DateTime.DATETIME_FULL);
2986
+ default:
2987
+ return dateTimeInClinicTz.toLocaleString(DateTime.DATETIME_FULL);
2988
+ }
2989
+ } catch (error) {
2990
+ Logger.error("[AppointmentMailingService] Error formatting timestamp in clinic timezone:", {
2991
+ error: error instanceof Error ? error.message : String(error),
2992
+ clinicTimezone
2993
+ });
2994
+ return timestamp.toDate().toLocaleString();
2995
+ }
2996
+ }
2997
+ /**
2998
+ * Gets a user-friendly display name for a timezone
2999
+ * @param timezone - IANA timezone string (e.g., "Europe/Zurich")
3000
+ * @returns User-friendly timezone name (e.g., "Clinic Time - Europe/Zurich")
3001
+ */
3002
+ getTimezoneDisplayName(timezone) {
3003
+ try {
3004
+ const timezoneMap = {
3005
+ "Europe/Zurich": "Clinic Time - Central European Time",
3006
+ "Europe/London": "Clinic Time - GMT/BST",
3007
+ "America/New_York": "Clinic Time - Eastern Time",
3008
+ "America/Los_Angeles": "Clinic Time - Pacific Time",
3009
+ "Asia/Dubai": "Clinic Time - Gulf Standard Time",
3010
+ "Asia/Karachi": "Clinic Time - Pakistan Standard Time",
3011
+ "Asia/Kolkata": "Clinic Time - India Standard Time",
3012
+ "Australia/Sydney": "Clinic Time - Australian Eastern Time"
3013
+ };
3014
+ return timezoneMap[timezone] || `Clinic Time - ${timezone}`;
3015
+ } catch (error) {
3016
+ Logger.error("[AppointmentMailingService] Error getting timezone display name:", {
3017
+ error: error instanceof Error ? error.message : String(error),
3018
+ timezone
3019
+ });
3020
+ return `Clinic Time - ${timezone}`;
3021
+ }
3022
+ }
3023
+ async sendAppointmentConfirmedEmail(data) {
3024
+ var _a, _b, _c, _d, _e;
3025
+ Logger.info(
3026
+ `[AppointmentMailingService] Preparing to send appointment confirmation email to ${data.recipientRole}: ${data.recipientProfile.id}`
3027
+ );
3028
+ const recipientEmail = data.recipientProfile.email;
3029
+ if (!recipientEmail) {
3030
+ Logger.error("[AppointmentMailingService] Recipient email not found for confirmation.", {
3031
+ recipientId: data.recipientProfile.id,
3032
+ role: data.recipientRole
3033
+ });
3034
+ throw new Error("Recipient email address is missing.");
3035
+ }
3036
+ const clinicTimezone = data.appointment.clinic_tz || "UTC";
3037
+ Logger.debug("[AppointmentMailingService] Formatting appointment time", {
3038
+ clinicTimezone,
3039
+ utcTime: data.appointment.appointmentStartTime.toDate().toISOString()
3040
+ });
3041
+ const formattedTime = this.formatTimestampInClinicTimezone(
3042
+ data.appointment.appointmentStartTime,
3043
+ clinicTimezone,
3044
+ "time"
3045
+ );
3046
+ const timezoneName = this.getTimezoneDisplayName(clinicTimezone);
3047
+ const templateVariables = {
3048
+ patientName: data.appointment.patientInfo.fullName,
3049
+ procedureName: data.appointment.procedureInfo.name,
3050
+ appointmentDate: this.formatTimestampInClinicTimezone(
3051
+ data.appointment.appointmentStartTime,
3052
+ clinicTimezone,
3053
+ "date"
3054
+ ),
3055
+ appointmentTime: `${formattedTime} (${timezoneName})`,
3056
+ practitionerName: data.appointment.practitionerInfo.name,
3057
+ clinicName: data.appointment.clinicInfo.name
3058
+ };
3059
+ const html = this.renderTemplate(patientAppointmentConfirmedTemplate, templateVariables);
3060
+ const subject = ((_a = data.options) == null ? void 0 : _a.customSubject) || "Your Appointment is Confirmed!";
3061
+ const fromAddress = ((_b = data.options) == null ? void 0 : _b.fromAddress) || `MetaEstetics <no-reply@${((_c = data.options) == null ? void 0 : _c.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN}>`;
3062
+ const domainToSendFrom = ((_d = data.options) == null ? void 0 : _d.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN;
3063
+ const mailgunSendData = {
3064
+ to: recipientEmail,
3065
+ from: fromAddress,
3066
+ subject,
3067
+ html
3068
+ };
3069
+ try {
3070
+ const result = await this.sendEmail(domainToSendFrom, mailgunSendData);
3071
+ await this.logEmailAttempt(
3072
+ { to: recipientEmail, subject, templateName: "appointment_confirmed" },
3073
+ true
3074
+ );
3075
+ return result;
3076
+ } catch (error) {
3077
+ await this.logEmailAttempt(
3078
+ {
3079
+ to: recipientEmail,
3080
+ subject: ((_e = data.options) == null ? void 0 : _e.customSubject) || "Your Appointment is Confirmed!",
3081
+ templateName: "appointment_confirmed"
3082
+ },
3083
+ false,
3084
+ error
3085
+ );
3086
+ throw error;
3087
+ }
3088
+ }
3089
+ async sendAppointmentRequestedEmailToClinic(data) {
3090
+ var _a, _b, _c, _d, _e, _f;
3091
+ Logger.info(
3092
+ `[AppointmentMailingService] Preparing to send appointment requested email to clinic: ${data.clinicProfile.id}`
3093
+ );
3094
+ const clinicEmail = (_a = data.clinicProfile.contactInfo) == null ? void 0 : _a.email;
3095
+ if (!clinicEmail) {
3096
+ Logger.error(
3097
+ "[AppointmentMailingService] Clinic contact email not found for request notification.",
3098
+ { clinicId: data.clinicProfile.id }
3099
+ );
3100
+ throw new Error("Clinic contact email address is missing.");
3101
+ }
3102
+ const clinicTimezone = data.appointment.clinic_tz || "UTC";
3103
+ Logger.debug("[AppointmentMailingService] Formatting appointment time for clinic", {
3104
+ clinicTimezone,
3105
+ utcTime: data.appointment.appointmentStartTime.toDate().toISOString()
3106
+ });
3107
+ const formattedTime = this.formatTimestampInClinicTimezone(
3108
+ data.appointment.appointmentStartTime,
3109
+ clinicTimezone,
3110
+ "time"
3111
+ );
3112
+ const timezoneName = this.getTimezoneDisplayName(clinicTimezone);
3113
+ const templateVariables = {
3114
+ clinicName: data.clinicProfile.name,
3115
+ patientName: data.appointment.patientInfo.fullName,
3116
+ procedureName: data.appointment.procedureInfo.name,
3117
+ appointmentDate: this.formatTimestampInClinicTimezone(
3118
+ data.appointment.appointmentStartTime,
3119
+ clinicTimezone,
3120
+ "date"
3121
+ ),
3122
+ appointmentTime: `${formattedTime} (${timezoneName})`,
3123
+ practitionerName: data.appointment.practitionerInfo.name
3124
+ };
3125
+ const html = this.renderTemplate(clinicAppointmentRequestedTemplate, templateVariables);
3126
+ const subject = ((_b = data.options) == null ? void 0 : _b.customSubject) || "New Appointment Request Received";
3127
+ const fromAddress = ((_c = data.options) == null ? void 0 : _c.fromAddress) || `MetaEstetics <no-reply@${((_d = data.options) == null ? void 0 : _d.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN}>`;
3128
+ const domainToSendFrom = ((_e = data.options) == null ? void 0 : _e.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN;
3129
+ const mailgunSendData = {
3130
+ to: clinicEmail,
3131
+ from: fromAddress,
3132
+ subject,
3133
+ html
3134
+ };
3135
+ try {
3136
+ const result = await this.sendEmail(domainToSendFrom, mailgunSendData);
3137
+ await this.logEmailAttempt(
3138
+ {
3139
+ to: clinicEmail,
3140
+ subject,
3141
+ templateName: "appointment_requested_clinic"
3142
+ },
3143
+ true
3144
+ );
3145
+ return result;
3146
+ } catch (error) {
3147
+ await this.logEmailAttempt(
3148
+ {
3149
+ to: clinicEmail,
3150
+ subject: ((_f = data.options) == null ? void 0 : _f.customSubject) || "New Appointment Request Received",
3151
+ templateName: "appointment_requested_clinic"
3152
+ },
3153
+ false,
3154
+ error
3155
+ );
3156
+ throw error;
3157
+ }
3158
+ }
3159
+ /**
3160
+ * Gets a user-friendly display text for who cancelled the appointment
3161
+ * @param cancelledBy - The entity that cancelled the appointment
3162
+ * @param clinicName - The clinic name for context
3163
+ * @returns User-friendly cancellation source text
3164
+ */
3165
+ getCancelledByDisplayText(cancelledBy, clinicName) {
3166
+ switch (cancelledBy) {
3167
+ case "patient":
3168
+ return "Patient Request";
3169
+ case "clinic":
3170
+ return `${clinicName} (Clinic)`;
3171
+ case "practitioner":
3172
+ return "Your Practitioner";
3173
+ case "system":
3174
+ return "System (Automatic)";
3175
+ default:
3176
+ return "Unknown";
3177
+ }
3178
+ }
3179
+ /**
3180
+ * Sends an appointment cancellation email to the recipient
3181
+ * @param data - Appointment cancellation email data
3182
+ * @returns Promise with the sending result
3183
+ */
3184
+ async sendAppointmentCancelledEmail(data) {
3185
+ var _a, _b, _c, _d;
3186
+ Logger.info(
3187
+ `[AppointmentMailingService] Preparing to send appointment cancellation email to ${data.recipientRole}: ${data.recipientProfile.id}`
3188
+ );
3189
+ const recipientEmail = data.recipientProfile.email;
3190
+ if (!recipientEmail) {
3191
+ Logger.error("[AppointmentMailingService] Recipient email not found for cancellation.", {
3192
+ recipientId: data.recipientProfile.id,
3193
+ role: data.recipientRole
3194
+ });
3195
+ throw new Error("Recipient email address is missing.");
3196
+ }
3197
+ const clinicTimezone = data.appointment.clinic_tz || "UTC";
3198
+ Logger.debug("[AppointmentMailingService] Formatting appointment time for cancellation", {
3199
+ clinicTimezone,
3200
+ utcTime: data.appointment.appointmentStartTime.toDate().toISOString()
3201
+ });
3202
+ const formattedTime = this.formatTimestampInClinicTimezone(
3203
+ data.appointment.appointmentStartTime,
3204
+ clinicTimezone,
3205
+ "time"
3206
+ );
3207
+ const timezoneName = this.getTimezoneDisplayName(clinicTimezone);
3208
+ const cancelledBy = data.appointment.canceledBy || "system";
3209
+ const cancelledByDisplay = this.getCancelledByDisplayText(
3210
+ cancelledBy,
3211
+ data.appointment.clinicInfo.name
3212
+ );
3213
+ const recipientName = data.recipientRole === "patient" ? data.appointment.patientInfo.fullName : data.appointment.practitionerInfo.name;
3214
+ const templateVariables = {
3215
+ recipientName,
3216
+ procedureName: data.appointment.procedureInfo.name,
3217
+ appointmentDate: this.formatTimestampInClinicTimezone(
3218
+ data.appointment.appointmentStartTime,
3219
+ clinicTimezone,
3220
+ "date"
3221
+ ),
3222
+ appointmentTime: `${formattedTime} (${timezoneName})`,
3223
+ practitionerName: data.appointment.practitionerInfo.name,
3224
+ clinicName: data.appointment.clinicInfo.name,
3225
+ cancelledByDisplay
3226
+ };
3227
+ const cancellationReason = data.cancellationReason || data.appointment.cancellationReason;
3228
+ let html = appointmentCancelledTemplate;
3229
+ if (cancellationReason) {
3230
+ templateVariables.cancellationReason = cancellationReason;
3231
+ html = html.replace(
3232
+ /\{\{#if cancellationReason\}\}([\s\S]*?)\{\{\/if\}\}/g,
3233
+ "$1"
3234
+ );
3235
+ } else {
3236
+ html = html.replace(/\{\{#if cancellationReason\}\}[\s\S]*?\{\{\/if\}\}/g, "");
3237
+ }
3238
+ html = this.renderTemplate(html, templateVariables);
3239
+ const subject = ((_a = data.options) == null ? void 0 : _a.customSubject) || `Appointment Cancelled: ${data.appointment.procedureInfo.name}`;
3240
+ const fromAddress = ((_b = data.options) == null ? void 0 : _b.fromAddress) || `MetaEstetics <no-reply@${((_c = data.options) == null ? void 0 : _c.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN}>`;
3241
+ const domainToSendFrom = ((_d = data.options) == null ? void 0 : _d.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN;
3242
+ const mailgunSendData = {
3243
+ to: recipientEmail,
3244
+ from: fromAddress,
3245
+ subject,
3246
+ html
3247
+ };
3248
+ try {
3249
+ const result = await this.sendEmail(domainToSendFrom, mailgunSendData);
3250
+ await this.logEmailAttempt(
3251
+ { to: recipientEmail, subject, templateName: "appointment_cancelled" },
3252
+ true
3253
+ );
3254
+ Logger.info(
3255
+ `[AppointmentMailingService] Successfully sent cancellation email to ${recipientEmail}`
3256
+ );
3257
+ return result;
3258
+ } catch (error) {
3259
+ await this.logEmailAttempt(
3260
+ {
3261
+ to: recipientEmail,
3262
+ subject,
3263
+ templateName: "appointment_cancelled"
3264
+ },
3265
+ false,
3266
+ error
3267
+ );
3268
+ Logger.error(
3269
+ `[AppointmentMailingService] Error sending cancellation email to ${recipientEmail}:`,
3270
+ error
3271
+ );
3272
+ throw error;
3273
+ }
3274
+ }
3275
+ /**
3276
+ * Sends a reschedule proposal email to the patient
3277
+ * @param data - Appointment reschedule proposal email data
3278
+ * @returns Promise with the sending result
3279
+ */
3280
+ async sendAppointmentRescheduledProposalEmail(data) {
3281
+ var _a, _b, _c, _d;
3282
+ Logger.info(
3283
+ `[AppointmentMailingService] Preparing to send reschedule proposal email to patient: ${data.patientProfile.id}`
3284
+ );
3285
+ const recipientEmail = data.patientProfile.email;
3286
+ if (!recipientEmail) {
3287
+ Logger.error("[AppointmentMailingService] Patient email not found for reschedule proposal.", {
3288
+ patientId: data.patientProfile.id
3289
+ });
3290
+ throw new Error("Patient email address is missing.");
3291
+ }
3292
+ const clinicTimezone = data.appointment.clinic_tz || "UTC";
3293
+ Logger.debug("[AppointmentMailingService] Formatting appointment times for reschedule", {
3294
+ clinicTimezone,
3295
+ previousTime: data.previousStartTime.toDate().toISOString(),
3296
+ newTime: data.appointment.appointmentStartTime.toDate().toISOString()
3297
+ });
3298
+ const previousFormattedTime = this.formatTimestampInClinicTimezone(
3299
+ data.previousStartTime,
3300
+ clinicTimezone,
3301
+ "time"
3302
+ );
3303
+ const previousFormattedDate = this.formatTimestampInClinicTimezone(
3304
+ data.previousStartTime,
3305
+ clinicTimezone,
3306
+ "date"
3307
+ );
3308
+ const previousTimezoneName = this.getTimezoneDisplayName(clinicTimezone);
3309
+ const newFormattedTime = this.formatTimestampInClinicTimezone(
3310
+ data.appointment.appointmentStartTime,
3311
+ clinicTimezone,
3312
+ "time"
3313
+ );
3314
+ const newFormattedDate = this.formatTimestampInClinicTimezone(
3315
+ data.appointment.appointmentStartTime,
3316
+ clinicTimezone,
3317
+ "date"
3318
+ );
3319
+ const newTimezoneName = this.getTimezoneDisplayName(clinicTimezone);
3320
+ const templateVariables = {
3321
+ patientName: data.appointment.patientInfo.fullName,
3322
+ procedureName: data.appointment.procedureInfo.name,
3323
+ practitionerName: data.appointment.practitionerInfo.name,
3324
+ clinicName: data.appointment.clinicInfo.name,
3325
+ previousDate: previousFormattedDate,
3326
+ previousTime: `${previousFormattedTime} (${previousTimezoneName})`,
3327
+ newDate: newFormattedDate,
3328
+ newTime: `${newFormattedTime} (${newTimezoneName})`
3329
+ };
3330
+ const html = this.renderTemplate(appointmentRescheduledProposalTemplate, templateVariables);
3331
+ const subject = ((_a = data.options) == null ? void 0 : _a.customSubject) || `Action Required: Reschedule Proposal for Your ${data.appointment.procedureInfo.name} Appointment`;
3332
+ const fromAddress = ((_b = data.options) == null ? void 0 : _b.fromAddress) || `MetaEstetics <no-reply@${((_c = data.options) == null ? void 0 : _c.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN}>`;
3333
+ const domainToSendFrom = ((_d = data.options) == null ? void 0 : _d.mailgunDomain) || this.DEFAULT_MAILGUN_DOMAIN;
3334
+ const mailgunSendData = {
3335
+ to: recipientEmail,
3336
+ from: fromAddress,
3337
+ subject,
3338
+ html
3339
+ };
3340
+ try {
3341
+ const result = await this.sendEmail(domainToSendFrom, mailgunSendData);
3342
+ await this.logEmailAttempt(
3343
+ { to: recipientEmail, subject, templateName: "appointment_rescheduled_proposal" },
3344
+ true
3345
+ );
3346
+ Logger.info(
3347
+ `[AppointmentMailingService] Successfully sent reschedule proposal email to ${recipientEmail}`
3348
+ );
3349
+ return result;
3350
+ } catch (error) {
3351
+ await this.logEmailAttempt(
3352
+ {
3353
+ to: recipientEmail,
3354
+ subject,
3355
+ templateName: "appointment_rescheduled_proposal"
3356
+ },
3357
+ false,
3358
+ error
3359
+ );
3360
+ Logger.error(
3361
+ `[AppointmentMailingService] Error sending reschedule proposal email to ${recipientEmail}:`,
3362
+ error
3363
+ );
3364
+ throw error;
3365
+ }
3366
+ }
3367
+ async sendReviewRequestEmail(data) {
3368
+ Logger.info(
3369
+ `[AppointmentMailingService] Placeholder for sendReviewRequestEmail to patient: ${data.patientProfile.id}`
3370
+ );
3371
+ return Promise.resolve();
3372
+ }
3373
+ async sendReviewAddedEmail(data) {
3374
+ Logger.info(
3375
+ `[AppointmentMailingService] Placeholder for sendReviewAddedEmail to ${data.recipientRole}: ${data.recipientProfile.id}`
3376
+ );
3377
+ return Promise.resolve();
3378
+ }
3379
+ };
3380
+
3381
+ // src/admin/aggregation/appointment/appointment.aggregation.service.ts
3382
+ var AppointmentAggregationService = class {
3383
+ /**
2517
3384
  * Constructor for AppointmentAggregationService.
2518
3385
  * @param mailgunClient - An initialized Mailgun client instance.
2519
3386
  * @param firestore Optional Firestore instance. If not provided, it uses the default admin SDK instance.
@@ -2868,6 +3735,16 @@ var AppointmentAggregationService = class {
2868
3735
  // TODO: Properly import PatientProfileInfo and types
2869
3736
  );
2870
3737
  }
3738
+ if ((patientProfile == null ? void 0 : patientProfile.expoTokens) && patientProfile.expoTokens.length > 0) {
3739
+ Logger.info(
3740
+ `[AggService] Sending reschedule proposal push notification to patient ${after.patientId}`
3741
+ );
3742
+ await this.notificationsAdmin.sendAppointmentRescheduledProposalPush(
3743
+ after,
3744
+ after.patientId,
3745
+ patientProfile.expoTokens
3746
+ );
3747
+ }
2871
3748
  Logger.info(
2872
3749
  `[AggService] TODO: Send reschedule proposal notifications to practitioner as well.`
2873
3750
  );