BullMQ 5+ refuse les ':' dans les Custom Ids (validateOptions throws "Custom Id cannot contain :"). On utilisait `relance:<taskId>` et `checkin:<taskId>` pour assurer l'idempotence — passe en `relance-<taskId>` / `checkin-<taskId>`.
97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import { DateTime } from 'luxon'
|
|
import CheckinTask from '#models/checkin_task'
|
|
import Invoice from '#models/invoice'
|
|
import { getQueue } from '#services/queue'
|
|
import { generateCheckinToken } from '#services/checkin_token'
|
|
import type { TransactionClientContract } from '@adonisjs/lucid/types/database'
|
|
|
|
const CHECKIN_QUEUE = 'checkins'
|
|
|
|
/**
|
|
* Programme un check-in pour une facture.
|
|
*
|
|
* V1 : 1 check-in par facture, envoyé à `dueDate` (pile à l'échéance).
|
|
* Si dueDate est dans le passé → envoie immédiat (à `now + 1min`),
|
|
* pour que les factures importées en retard reçoivent quand même un
|
|
* check-in.
|
|
*
|
|
* Le token est généré ici (plain) — on retourne le plain pour permettre
|
|
* au caller de le passer dans des emails de test si besoin, mais en
|
|
* pratique seul le hash est stocké et lu via SendCheckinJob.
|
|
*
|
|
* Idempotent par invoice : si une CheckinTask `scheduled` existe déjà,
|
|
* on la cancelle d'abord puis on en crée une nouvelle (cas re-scheduling
|
|
* après changement de dueDate).
|
|
*/
|
|
export async function scheduleCheckinForInvoice(
|
|
invoice: Invoice,
|
|
trx?: TransactionClientContract
|
|
): Promise<{ task: CheckinTask; plain: string } | null> {
|
|
// Cancel l'éventuelle CheckinTask scheduled précédente.
|
|
const existing = await CheckinTask.query(trx ? { client: trx } : undefined)
|
|
.where('invoice_id', invoice.id)
|
|
.where('status', 'scheduled')
|
|
const queue = getQueue(CHECKIN_QUEUE)
|
|
for (const t of existing) {
|
|
await queue.remove(`checkin-${t.id}`).catch(() => {})
|
|
t.useTransaction(trx ?? (null as never))
|
|
t.status = 'expired'
|
|
await t.save()
|
|
}
|
|
|
|
const now = DateTime.now()
|
|
const sendAtRaw = invoice.dueDate
|
|
const sendAt = sendAtRaw < now ? now.plus({ minutes: 1 }) : sendAtRaw
|
|
|
|
const { plain, hashed } = generateCheckinToken()
|
|
|
|
const task = await CheckinTask.create(
|
|
{
|
|
organizationId: invoice.organizationId,
|
|
invoiceId: invoice.id,
|
|
sendAt,
|
|
tokenHash: hashed,
|
|
status: 'scheduled',
|
|
sentAt: null,
|
|
answeredAt: null,
|
|
answer: null,
|
|
},
|
|
trx ? { client: trx } : undefined
|
|
)
|
|
|
|
const delay = Math.max(0, sendAt.toMillis() - now.toMillis())
|
|
await queue.add(
|
|
'send-checkin',
|
|
{ taskId: task.id, plain },
|
|
{
|
|
delay,
|
|
jobId: `checkin-${task.id}`,
|
|
attempts: 3,
|
|
backoff: { type: 'exponential', delay: 30_000 },
|
|
}
|
|
)
|
|
|
|
return { task, plain }
|
|
}
|
|
|
|
/**
|
|
* Annule le check-in scheduled d'une facture (appelé par mark-paid).
|
|
*/
|
|
export async function cancelCheckinForInvoice(
|
|
invoiceId: string,
|
|
trx?: TransactionClientContract
|
|
): Promise<void> {
|
|
const tasks = await CheckinTask.query(trx ? { client: trx } : undefined)
|
|
.where('invoice_id', invoiceId)
|
|
.where('status', 'scheduled')
|
|
if (tasks.length === 0) return
|
|
|
|
const queue = getQueue(CHECKIN_QUEUE)
|
|
for (const t of tasks) {
|
|
await queue.remove(`checkin-${t.id}`).catch(() => {})
|
|
t.useTransaction(trx ?? (null as never))
|
|
t.status = 'expired'
|
|
await t.save()
|
|
}
|
|
}
|