2026-03-01 03:04:24 +08:00
|
|
|
import { prisma } from '@/lib/db';
|
2026-03-06 17:34:42 +08:00
|
|
|
import { ORDER_STATUS } from '@/lib/constants';
|
2026-03-01 18:44:49 +08:00
|
|
|
import { cancelOrderCore } from './service';
|
2026-03-01 03:04:24 +08:00
|
|
|
|
|
|
|
|
const INTERVAL_MS = 30_000; // 30 seconds
|
|
|
|
|
let timer: ReturnType<typeof setInterval> | null = null;
|
|
|
|
|
|
|
|
|
|
export async function expireOrders(): Promise<number> {
|
2026-03-01 17:58:08 +08:00
|
|
|
const orders = await prisma.order.findMany({
|
2026-03-01 03:04:24 +08:00
|
|
|
where: {
|
2026-03-06 17:34:42 +08:00
|
|
|
status: ORDER_STATUS.PENDING,
|
2026-03-01 03:04:24 +08:00
|
|
|
expiresAt: { lt: new Date() },
|
|
|
|
|
},
|
2026-03-01 17:58:08 +08:00
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
paymentTradeNo: true,
|
|
|
|
|
paymentType: true,
|
|
|
|
|
},
|
2026-03-01 03:04:24 +08:00
|
|
|
});
|
|
|
|
|
|
2026-03-01 17:58:08 +08:00
|
|
|
if (orders.length === 0) return 0;
|
|
|
|
|
|
|
|
|
|
let expiredCount = 0;
|
|
|
|
|
|
|
|
|
|
for (const order of orders) {
|
|
|
|
|
try {
|
2026-03-01 18:44:49 +08:00
|
|
|
const outcome = await cancelOrderCore({
|
|
|
|
|
orderId: order.id,
|
|
|
|
|
paymentTradeNo: order.paymentTradeNo,
|
|
|
|
|
paymentType: order.paymentType,
|
2026-03-06 17:34:42 +08:00
|
|
|
finalStatus: ORDER_STATUS.EXPIRED,
|
2026-03-01 18:44:49 +08:00
|
|
|
operator: 'timeout',
|
|
|
|
|
auditDetail: 'Order expired',
|
2026-03-01 17:58:08 +08:00
|
|
|
});
|
|
|
|
|
|
2026-03-01 18:44:49 +08:00
|
|
|
if (outcome === 'cancelled') expiredCount++;
|
2026-03-01 17:58:08 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`Error expiring order ${order.id}:`, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (expiredCount > 0) {
|
|
|
|
|
console.log(`Expired ${expiredCount} orders`);
|
2026-03-01 03:04:24 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 17:58:08 +08:00
|
|
|
return expiredCount;
|
2026-03-01 03:04:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function startTimeoutScheduler(): void {
|
|
|
|
|
if (timer) return;
|
|
|
|
|
|
|
|
|
|
// Run immediately on startup
|
|
|
|
|
expireOrders().catch(console.error);
|
|
|
|
|
|
|
|
|
|
// Then run every 30 seconds
|
|
|
|
|
timer = setInterval(() => {
|
|
|
|
|
expireOrders().catch(console.error);
|
|
|
|
|
}, INTERVAL_MS);
|
|
|
|
|
|
|
|
|
|
console.log('Order timeout scheduler started');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function stopTimeoutScheduler(): void {
|
|
|
|
|
if (timer) {
|
|
|
|
|
clearInterval(timer);
|
|
|
|
|
timer = null;
|
|
|
|
|
console.log('Order timeout scheduler stopped');
|
|
|
|
|
}
|
|
|
|
|
}
|