This commit is contained in:
payacom
2026-09-13 21:32:36 +03:30
parent 90c5f94b57
commit c302aea177
49 changed files with 4063 additions and 920 deletions

View File

@@ -1,13 +1,14 @@
const OfferModel = require('../models/OfferModel')
const WalletTransactionModel = require('../models/WalletTransactionModel')
const { notifyUser } = require('./notifyUser')
const OFFER_REPLY_WINDOW_MS = 24 * 60 * 60 * 1000
const OFFER_REPLY_WINDOW_MS = 12 * 60 * 60 * 1000
/**
* Called whenever a chat message is sent. If there's a pending paid Offer
* between these two users, tracks the requester's first direct message and
* the receiver's reply — the receiver's 30% commission is only credited if
* they reply within 24 hours of that first message.
* they reply within 12 hours of that first message.
*/
async function trackOfferMessageForCommission (io, senderId, receiverId) {
try {
@@ -33,7 +34,7 @@ async function trackOfferMessageForCommission (io, senderId, receiverId) {
return
}
// پیام از طرف گیرنده — کسی که باید ظرف ۲۴ ساعت پاسخ بدهد
// پیام از طرف گیرنده — کسی که باید ظرف ۱۲ ساعت پاسخ بدهد
if (!offer.firstMessageAt || offer.repliedAt) return
if (new Date() > offer.replyDeadline) return
@@ -65,4 +66,54 @@ async function trackOfferMessageForCommission (io, senderId, receiverId) {
}
}
module.exports = { trackOfferMessageForCommission, OFFER_REPLY_WINDOW_MS }
/**
* Sweep for paid Offers whose 12-hour reply deadline has passed without the
* receiver replying — refunds the pending 30% commission to the sender's
* wallet instead (both web and Android go through the same payment callback,
* so a single server-side sweep covers both). Meant to be called
* periodically by a cron job (see services/OfferReplyCron.js).
*/
async function refundExpiredOfferCommissions (io) {
const now = new Date()
const expiredOffers = await OfferModel.find({
commissionCredited: false,
commissionRefunded: false,
pendingCommissionAmount: { $gt: 0 },
firstMessageAt: { $ne: null },
repliedAt: null,
replyDeadline: { $lte: now }
})
for (const offer of expiredOffers) {
try {
offer.commissionRefunded = true
await offer.save()
await WalletTransactionModel.create({
user: offer.sender,
bucket: 'gift',
type: 'credit',
amount: offer.pendingCommissionAmount,
referenceType: 'payment',
referenceId: offer._id,
description: 'بازگشت وجه درخواست همکاری (طرف مقابل ظرف ۱۲ ساعت مقرر پاسخ نداد)'
})
await notifyUser(io, offer.sender, {
type: 'offer_commission_refunded',
title: 'بازگشت وجه به کیف پول',
description: 'طرف مقابل ظرف مهلت ۱۲ ساعته به درخواست همکاری شما پاسخ نداد؛ مبلغ به کیف پول شما بازگشت.'
})
} catch (error) {
console.error('refundExpiredOfferCommissions error for offer', offer._id, error)
}
}
return expiredOffers.length
}
module.exports = {
trackOfferMessageForCommission,
refundExpiredOfferCommissions,
OFFER_REPLY_WINDOW_MS
}