2025-01-03 10:33:07 -06:00
|
|
|
import { PAID_ACTION_TERMINAL_STATES, USER_ID } from '@/lib/constants'
|
2024-11-27 07:39:05 -06:00
|
|
|
import { datePivot } from '@/lib/time'
|
2024-11-16 01:38:14 +01:00
|
|
|
|
|
|
|
const MAX_PENDING_PAID_ACTIONS_PER_USER = 100
|
2024-11-27 07:39:05 -06:00
|
|
|
const MAX_PENDING_DIRECT_INVOICES_PER_USER_MINUTES = 10
|
|
|
|
const MAX_PENDING_DIRECT_INVOICES_PER_USER = 100
|
2024-11-16 01:38:14 +01:00
|
|
|
|
|
|
|
export async function assertBelowMaxPendingInvoices (context) {
|
|
|
|
const { models, me } = context
|
|
|
|
const pendingInvoices = await models.invoice.count({
|
|
|
|
where: {
|
|
|
|
userId: me?.id ?? USER_ID.anon,
|
|
|
|
actionState: {
|
|
|
|
notIn: PAID_ACTION_TERMINAL_STATES
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (pendingInvoices >= MAX_PENDING_PAID_ACTIONS_PER_USER) {
|
|
|
|
throw new Error('You have too many pending paid actions, cancel some or wait for them to expire')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-27 07:39:05 -06:00
|
|
|
export async function assertBelowMaxPendingDirectPayments (userId, context) {
|
|
|
|
const { models, me } = context
|
|
|
|
|
|
|
|
if (me?.id !== userId) {
|
|
|
|
const pendingSenderInvoices = await models.directPayment.count({
|
|
|
|
where: {
|
|
|
|
senderId: me?.id ?? USER_ID.anon,
|
|
|
|
createdAt: {
|
|
|
|
gt: datePivot(new Date(), { minutes: -MAX_PENDING_DIRECT_INVOICES_PER_USER_MINUTES })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (pendingSenderInvoices >= MAX_PENDING_DIRECT_INVOICES_PER_USER) {
|
|
|
|
throw new Error('You\'ve sent too many direct payments')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userId) return
|
|
|
|
|
|
|
|
const pendingReceiverInvoices = await models.directPayment.count({
|
|
|
|
where: {
|
|
|
|
receiverId: userId,
|
|
|
|
createdAt: {
|
|
|
|
gt: datePivot(new Date(), { minutes: -MAX_PENDING_DIRECT_INVOICES_PER_USER_MINUTES })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (pendingReceiverInvoices >= MAX_PENDING_DIRECT_INVOICES_PER_USER) {
|
|
|
|
throw new Error('Receiver has too many direct payments')
|
|
|
|
}
|
|
|
|
}
|