@ideal-photography/shared
Version:
Shared MongoDB and utility logic for Ideal Photography PWAs: users, products, services, bookings, orders/cart, galleries, reviews, notifications, campaigns, settings, audit logs, minimart items/orders, and push notification subscriptions.
40 lines (33 loc) • 1.27 kB
JavaScript
import { models } from '../../mongoDB/index.js';
import { emitAlert } from '../index.js';
const SIX_HOURS_MS = 6 * 60 * 60 * 1000;
/**
* Find bookings whose preferred/confirmed date-time is within next 6h and emit reminders.
* Intended to be run every hour by a cron job.
*/
export async function runBookingReminderJob() {
const now = new Date();
const sixHoursLater = new Date(now.getTime() + SIX_HOURS_MS);
// Assuming booking.confirmedDate/time fields compose to Date `scheduledAt` virtual
const bookings = await models.Booking.find({
confirmedDate: { $lte: sixHoursLater, $gte: now },
status: 'confirmed',
reminderSent6h: { $ne: true }
}).populate('userId');
for (const b of bookings) {
const user = b.userId;
emitAlert('booking.reminder.6h', { booking: b, user });
// mark flag
b.reminderSent6h = true;
await b.save();
}
return bookings.length;
}
// If executed directly via node, run once
if (import.meta.url === `file://${process.argv[1]}`) {
(async () => {
const count = await runBookingReminderJob();
console.log(`Emitted ${count} booking.reminder.6h alerts`);
process.exit();
})();
}