Skip to content

Commit 68ec6b7

Browse files
committed
feat: enhance hold invoice expiration check to include disputes
- Updated the `checkHoldInvoiceExpired` job to handle orders in 'DISPUTE' status. - Modified the query to include 'DISPUTE' in the list of order statuses with expired hold invoices. - Added logic to update the corresponding dispute status to 'SELLER_REFUNDED' when an order in dispute is processed.
1 parent cf4bc73 commit 68ec6b7

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

jobs/check_hold_invoice_expired.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { HasTelegram } from '../bot/start';
2-
import { Order, User } from '../models';
2+
import { Order, User, Dispute } from '../models';
33
import { holdInvoiceExpirationInSecs, getUserI18nContext } from '../util';
44
import { logger } from '../logger';
55
import { cancelHoldInvoice } from '../ln';
@@ -15,9 +15,10 @@ const checkHoldInvoiceExpired = async (bot: HasTelegram) => {
1515
const now = new Date();
1616
const holdExpiredBefore = new Date(now.getTime() - expirationTimeInMs);
1717

18-
// Orders in ACTIVE or FIAT_SENT whose hold invoice has exceeded the hold time
18+
// Orders in ACTIVE, DISPUTE or FIAT_SENT whose hold invoice has exceeded the hold time
19+
const orderStatuses = ['ACTIVE', 'DISPUTE', 'FIAT_SENT'];
1920
const ordersWithHoldInvoice = await Order.find({
20-
$or: [{ status: 'ACTIVE' }, { status: 'FIAT_SENT' }],
21+
$or: orderStatuses.map(status => ({ status })),
2122
hash: { $ne: null },
2223
invoice_held_at: { $ne: null, $lte: holdExpiredBefore },
2324
});
@@ -31,17 +32,25 @@ const checkHoldInvoiceExpired = async (bot: HasTelegram) => {
3132
async () => {
3233
const updatedOrder = await Order.findById(order._id);
3334
if (!updatedOrder) return;
34-
if (
35-
updatedOrder.status !== 'ACTIVE' &&
36-
updatedOrder.status !== 'FIAT_SENT'
37-
) {
35+
if (!orderStatuses.includes(updatedOrder.status)) {
3836
return;
3937
}
4038
if (!updatedOrder.hash || !updatedOrder.invoice_held_at) return;
4139

4240
// Cancel the hold invoice ourselves so our LND state is clean (idempotent if node already canceled via CLTV).
4341
try {
4442
await cancelHoldInvoice({ hash: updatedOrder.hash });
43+
44+
const isDispute = updatedOrder.status === 'DISPUTE';
45+
if (isDispute) {
46+
const dispute = await Dispute.findOne({
47+
order_id: String(updatedOrder._id),
48+
});
49+
if (dispute) {
50+
dispute.status = 'SELLER_REFUNDED';
51+
await dispute.save();
52+
}
53+
}
4554
} catch (err) {
4655
logger.error(
4756
`checkHoldInvoiceExpired: cancelHoldInvoice for order ${updatedOrder._id}: ${err}`,

0 commit comments

Comments
 (0)