Files
modstagram-back/services/BookingLifecycleCron.js
payacom 3f416a7a60 full
2026-08-31 20:45:41 +03:30

54 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const cron = require('node-cron')
const BookingModel = require('../models/BookingModel')
const { notifyUser } = require('../utils/notifyUser')
const PAYMENT_TIMEOUT_MINUTES = 60
let ioInstance = null
/** Called once from index.js right after the socket.io server is created — cron jobs have no `req`. */
function setIo (io) {
ioInstance = io
}
/** Cancel unpaid bookings that missed the deposit-payment window, freeing their time slot back up. */
const autoCancelUnpaidBookings = async () => {
const cutoff = new Date(Date.now() - PAYMENT_TIMEOUT_MINUTES * 60 * 1000)
try {
const bookings = await BookingModel.find({
status: 'pending_payment',
createdAt: { $lte: cutoff }
})
for (const booking of bookings) {
try {
booking.status = 'cancelled'
await booking.save()
await notifyUser(ioInstance, booking.buyer, {
project_post_id: booking._id,
type: 'booking',
title: 'لغو رزرو',
description: `رزرو خدمت «${booking.serviceTitle}» به دلیل عدم پرداخت بیعانه در بازه زمانی مجاز لغو شد.`
})
} catch (err) {
console.error(`[BookingLifecycleCron] failed to cancel booking ${booking._id}:`, err)
}
}
if (bookings.length) {
console.log(`[BookingLifecycleCron] auto-cancelled ${bookings.length} unpaid booking(s)`)
}
} catch (error) {
console.error('[BookingLifecycleCron] auto-cancel failed:', error)
}
}
// هر ۱۰ دقیقه: لغو رزروهای بدون پرداخت بیعانه خارج از مهلت ۶۰ دقیقه‌ای
cron.schedule('*/10 * * * *', autoCancelUnpaidBookings)
module.exports = {
autoCancelUnpaidBookings,
setIo
}