rubis/apps/api/app/services/checkin_scheduler.ts
ordinarthur 94263c6447 feat(api): check-in flow — email à l'user + endpoints publics paid/pending
Le check-in remplace l'intégration banking V1 (cf. CLAUDE.md → Glossaire) :
avant que la 1re relance ne parte, on demande à l'user "as-tu été payé ?"
via email, et il clique sur l'un des 2 liens publics.

Service checkin_token.ts : génération + hash SHA-256. 32 bytes random base64url, plain dans le mail, hash en DB (CheckinTask.token_hash unique).

Service checkin_scheduler.ts :
- scheduleCheckinForInvoice(invoice) : crée 1 CheckinTask à dueDate (now+1min si dueDate dans le passé). Idempotent par invoice — cancel les scheduled précédents avant.
- cancelCheckinForInvoice(invoiceId) : appelé par mark-paid pour stopper.

Job send_checkin_job.ts : worker queue 'checkins', skip si invoice paid/cancelled (no-op), construit l'URL avec le plain token (passé dans le payload du job, pas relu DB), appelle sendCheckinEmail.

mail_dispatcher.ts : sendCheckinEmail() — texte brut, destinataire = user (pas client !), 2 URLs (paid / pending), TTL 24h annoncé.

Controller CheckinController :
- GET /api/v1/checkin/:token/paid : status=answered + answer=paid + mark invoice paid (mêmes effets que POST /invoices/:id/mark-paid : rubis +1, ActivityEvent invoice_paid avec label "via check-in", cancelFutureRelances). Idempotent : 2e click → redirect "already_answered".
- GET /api/v1/checkin/:token/pending : status=answered + answer=still_pending. Les relances suivent leur cours.
- Validation : lookup hash, expiry (sentAt + 24h), redirects propres pour invalid / expired / already_answered.

Routes : nouveau group public `checkin` (PAS de middleware.auth) à côté du group auth, sous /api/v1.

Triggers branchés :
- InvoicesController.store et ImportBatchesController.validateDraft → scheduleCheckinForInvoice après création
- InvoicesController.markPaid → cancelCheckinForInvoice dans la tx

start/queue.ts : registerWorker('checkins', sendCheckinJob).

env : nouveau WEB_URL (URL du SPA pour redirects), default localhost:5173 en dev.

Bruno : nouveau dossier 08-Checkin avec doc complète du flow + 2 requêtes (paid / pending). var d'env `checkinToken` à remplir manuellement après avoir reçu l'email dans Mailpit.
2026-05-06 15:31:40 +02:00

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()
}
}