Migrations :
- relance_tasks (uuid id, organization_id FK CASCADE [scope direct sans join], invoice_id FK CASCADE, plan_step_id FK RESTRICT, send_at, status ENUM scheduled/sent/cancelled/failed, sent_at, queue_job_id pour cancel via BullMQ.remove). Indexes (org,status), (invoice_id), (send_at).
- checkin_tasks (uuid id, org_id, invoice_id, send_at, token_hash unique [SHA-256 du HMAC, TTL 24h], status ENUM scheduled/sent/answered/expired, answer 'paid'|'still_pending'). Pas encore branché — flow check-in arrivera dans un commit séparé (cf. backend.md §13.3).
Schema rules : status enums + answer typés.
Models RelanceTask + CheckinTask avec belongsTo Invoice / PlanStep.
Service relance_scheduler.ts :
- scheduleRelancesForInvoice(invoice) : pour chaque step du plan, calcule sendAt = dueDate + offsetDays. Si sendAt < now (facture importée en retard), on programme à `now + 1min` plutôt que skip — l'utilisateur "rattrape" une dette de relance, l'envoi immédiat est cohérent. Crée la RelanceTask + enqueue BullMQ avec delay, retry 5x exponential, jobId = `relance:<taskId>` pour idempotency. Cancelle les tasks scheduled existantes avant de re-programmer (gestion changement de plan).
- cancelFutureRelances(invoiceId, trx) : appelé par mark-paid pour stopper la chaîne.
Service queue.ts :
- getQueue(name) singleton lazy par queue
- registerWorker(name, handler) avec concurrency 5, log failed/completed
- shutdownQueue() pour le terminating hook Adonis
start/queue.ts (preload) : registerWorker('relances', sendRelanceJob) seulement quand `app.getEnvironment() === 'web'` (pas en tests/REPL — pas de connexion Redis pendant Japa).
Job send_relance_job.ts :
- Idempotent : si task.status !== 'scheduled', no-op
- Hook critique : si invoice paid/cancelled entre-temps, task.status = cancelled
- Mise en demeure (step.requiresManualValidation) : on n'envoie PAS, on log un activity_event 'warning_drafted' (cf. CLAUDE.md → Principes : validation manuelle obligatoire)
- Sinon : sendRelanceEmail + task.status=sent + invoice.rubisEarned+1 + organizations.rubis_count+1 + activity_event 'relance_sent'. Si invoice.status='pending', passe en 'in_relance' (sortie de l'état silencieux).
Service mail_dispatcher.ts : sendRelanceEmail interpole step.subject/body via mini moteur Mustache-like (renderTemplate, services/template.ts) avec {{client.name}}/{{numero}}/{{amount}}/{{dueDate}}/{{signature}}, puis @adonisjs/mail.use(MAIL_DRIVER) → Mailpit en dev, Resend en prod. Texte brut V1.
Triggers branchés :
- InvoicesController.store : si planId, scheduleRelancesForInvoice après création
- ImportBatchesController.validateDraft : pareil
- InvoicesController.markPaid : cancelFutureRelances dans la même tx que le paiement
#jobs/* ajouté aux imports package.json. Adonisrc preload start/queue.ts.
Bruno : doc 05-Invoices/04 Create maj avec instructions pour tester l'envoi immédiat (dueDate dans le passé → relance à now+1min → email visible dans Mailpit http://localhost:8025).
367 lines
11 KiB
TypeScript
367 lines
11 KiB
TypeScript
import Invoice from '#models/invoice'
|
|
import Plan from '#models/plan'
|
|
import InvoiceTransformer from '#transformers/invoice_transformer'
|
|
import {
|
|
createInvoiceValidator,
|
|
listInvoicesValidator,
|
|
} from '#validators/invoice'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
import { Exception } from '@adonisjs/core/exceptions'
|
|
import db from '@adonisjs/lucid/services/db'
|
|
import { DateTime } from 'luxon'
|
|
import { resolveClient } from '#services/resolve_client'
|
|
import { recordActivity } from '#services/activity_recorder'
|
|
import {
|
|
scheduleRelancesForInvoice,
|
|
cancelFutureRelances,
|
|
} from '#services/relance_scheduler'
|
|
|
|
const PAGE_SIZE = 50
|
|
|
|
// Priorité d'affichage côté liste : ce qui est actionnable d'abord.
|
|
const STATUS_PRIORITY: Record<string, number> = {
|
|
awaiting_user_confirmation: 0,
|
|
in_relance: 1,
|
|
pending: 2,
|
|
litigation: 3,
|
|
paid: 4,
|
|
cancelled: 5,
|
|
}
|
|
|
|
function requireOrgId(auth: HttpContext['auth']): string {
|
|
const user = auth.getUserOrFail()
|
|
if (!user.organizationId) {
|
|
throw new Exception('Aucune organisation rattachée', { status: 404, code: 'not_found' })
|
|
}
|
|
return user.organizationId
|
|
}
|
|
|
|
function serializeInvoice(i: Invoice) {
|
|
return new InvoiceTransformer(i).toObject()
|
|
}
|
|
|
|
/**
|
|
* Construit la timeline d'une facture en composant les étapes du plan
|
|
* avec l'état courant (V1 simplifié — les RelanceTask viendront plus tard).
|
|
*
|
|
* - étapes dont sendDay <= aujourd'hui : 'past' (envoyées)
|
|
* - étape actuelle (la prochaine future) : 'current'
|
|
* - étapes futures : 'future'
|
|
*/
|
|
function buildTimeline(invoice: Invoice): Array<{
|
|
id: string
|
|
state: 'past' | 'current' | 'future'
|
|
when: string
|
|
what: string
|
|
}> {
|
|
const events: Array<{
|
|
id: string
|
|
state: 'past' | 'current' | 'future'
|
|
when: string
|
|
what: string
|
|
}> = [
|
|
{
|
|
id: `${invoice.id}__issued`,
|
|
state: 'past',
|
|
when: `${formatShortDate(invoice.issueDate)} · facture émise`,
|
|
what: 'Importée',
|
|
},
|
|
]
|
|
|
|
if (
|
|
invoice.plan?.steps?.length &&
|
|
invoice.status !== 'paid' &&
|
|
invoice.status !== 'cancelled'
|
|
) {
|
|
const dueMs = invoice.dueDate.toMillis()
|
|
const nowMs = DateTime.now().toMillis()
|
|
let currentSet = false
|
|
|
|
for (const step of invoice.plan.steps.slice().sort((a, b) => a.order - b.order)) {
|
|
const sendMs = dueMs + step.offsetDays * 24 * 60 * 60 * 1000
|
|
const stepDate = DateTime.fromMillis(sendMs)
|
|
const labelStep = `J${step.offsetDays >= 0 ? '+' : ''}${step.offsetDays} — Étape ${step.order + 1}`
|
|
|
|
let state: 'past' | 'current' | 'future'
|
|
if (sendMs < nowMs) state = 'past'
|
|
else if (!currentSet) {
|
|
state = 'current'
|
|
currentSet = true
|
|
} else state = 'future'
|
|
|
|
events.push({
|
|
id: `${invoice.id}__step_${step.order}`,
|
|
state,
|
|
when: `${formatShortDate(stepDate)} · ${labelStep}`,
|
|
what:
|
|
state === 'past'
|
|
? `Email envoyé · "${step.subject.replace('{{numero}}', invoice.numero)}"`
|
|
: `Email programmé · "${step.subject.replace('{{numero}}', invoice.numero)}"`,
|
|
})
|
|
}
|
|
}
|
|
|
|
if (invoice.status === 'paid' && invoice.paidAt) {
|
|
events.push({
|
|
id: `${invoice.id}__paid`,
|
|
state: 'past',
|
|
when: `${formatShortDate(invoice.paidAt)} · facture encaissée`,
|
|
what: 'Marquée encaissée — relances stoppées',
|
|
})
|
|
}
|
|
|
|
return events
|
|
}
|
|
|
|
function formatShortDate(d: DateTime): string {
|
|
return d.toFormat('dd/LL/yyyy')
|
|
}
|
|
|
|
export default class InvoicesController {
|
|
/**
|
|
* GET /invoices?status=&q=&clientId=&page=
|
|
*/
|
|
async index({ auth, request, response }: HttpContext) {
|
|
const organizationId = requireOrgId(auth)
|
|
const filters = await request.validateUsing(listInvoicesValidator)
|
|
|
|
const query = Invoice.query()
|
|
.where('organization_id', organizationId)
|
|
.preload('client')
|
|
.preload('plan')
|
|
|
|
if (filters.status && filters.status !== 'all') {
|
|
query.where('status', filters.status)
|
|
}
|
|
if (filters.clientId) {
|
|
query.where('client_id', filters.clientId)
|
|
}
|
|
if (filters.q) {
|
|
const q = filters.q.toLowerCase()
|
|
query.where((b) => {
|
|
b.whereILike('numero', `%${q}%`).orWhereExists((sub) => {
|
|
sub
|
|
.from('clients')
|
|
.whereColumn('clients.id', 'invoices.client_id')
|
|
.whereILike('clients.name', `%${q}%`)
|
|
})
|
|
})
|
|
}
|
|
|
|
const invoices = await query.exec()
|
|
|
|
// Tri : actionnable d'abord (status priority), puis échéance croissante.
|
|
invoices.sort((a, b) => {
|
|
const dp = (STATUS_PRIORITY[a.status] ?? 99) - (STATUS_PRIORITY[b.status] ?? 99)
|
|
if (dp !== 0) return dp
|
|
return a.dueDate.toMillis() - b.dueDate.toMillis()
|
|
})
|
|
|
|
// Pagination simple en V1 (cf. backend.md §6 — cursor-based plus tard).
|
|
const page = filters.page ?? 1
|
|
const total = invoices.length
|
|
const sliced = invoices.slice((page - 1) * PAGE_SIZE, page * PAGE_SIZE)
|
|
|
|
return response.json({
|
|
data: sliced.map(serializeInvoice),
|
|
meta: { total, page },
|
|
})
|
|
}
|
|
|
|
/**
|
|
* GET /invoices/counts — compteurs par statut pour les chips dashboard.
|
|
*/
|
|
async counts({ auth, response }: HttpContext) {
|
|
const organizationId = requireOrgId(auth)
|
|
|
|
const rows = await db
|
|
.from('invoices')
|
|
.where('organization_id', organizationId)
|
|
.select('status')
|
|
.count('* as count')
|
|
.groupBy('status')
|
|
|
|
const counts = {
|
|
all: 0,
|
|
pending: 0,
|
|
in_relance: 0,
|
|
awaiting_user_confirmation: 0,
|
|
paid: 0,
|
|
litigation: 0,
|
|
cancelled: 0,
|
|
}
|
|
for (const r of rows) {
|
|
const c = Number(r.count)
|
|
counts.all += c
|
|
const s = r.status as keyof typeof counts
|
|
if (s in counts) counts[s] = c
|
|
}
|
|
|
|
return response.json({ data: counts })
|
|
}
|
|
|
|
/**
|
|
* GET /invoices/:id — détail enrichi (client + plan + timeline).
|
|
*/
|
|
async show({ auth, params, response }: HttpContext) {
|
|
const organizationId = requireOrgId(auth)
|
|
|
|
const invoice = await Invoice.query()
|
|
.where('organization_id', organizationId)
|
|
.where('id', params.id)
|
|
.preload('client')
|
|
.preload('plan', (q) => q.preload('steps'))
|
|
.first()
|
|
|
|
if (!invoice) {
|
|
throw new Exception('Facture introuvable', { status: 404, code: 'not_found' })
|
|
}
|
|
|
|
const data = serializeInvoice(invoice)
|
|
return response.json({
|
|
data: {
|
|
...data,
|
|
client: invoice.client && {
|
|
id: invoice.client.id,
|
|
name: invoice.client.name,
|
|
email: invoice.client.email,
|
|
phone: invoice.client.phone,
|
|
address: invoice.client.address,
|
|
siret: invoice.client.siret,
|
|
},
|
|
plan: invoice.plan && {
|
|
id: invoice.plan.id,
|
|
slug: invoice.plan.slug,
|
|
name: invoice.plan.name,
|
|
steps: (invoice.plan.steps ?? [])
|
|
.slice()
|
|
.sort((a, b) => a.order - b.order)
|
|
.map((s) => ({
|
|
id: s.id,
|
|
order: s.order,
|
|
offsetDays: s.offsetDays,
|
|
tone: s.tone,
|
|
subject: s.subject,
|
|
body: s.body,
|
|
requiresManualValidation: s.requiresManualValidation,
|
|
})),
|
|
},
|
|
timeline: buildTimeline(invoice),
|
|
},
|
|
})
|
|
}
|
|
|
|
/**
|
|
* POST /invoices — saisie manuelle.
|
|
*/
|
|
async store({ auth, request, response }: HttpContext) {
|
|
const organizationId = requireOrgId(auth)
|
|
const fields = await request.validateUsing(createInvoiceValidator)
|
|
|
|
const invoice = await db.transaction(async (trx) => {
|
|
const result = await resolveClient(organizationId, fields, trx)
|
|
if ('errorCode' in result) {
|
|
throw new Exception(
|
|
'Email du client requis — Rubis en a besoin pour envoyer les relances.',
|
|
{ status: 422, code: result.errorCode }
|
|
)
|
|
}
|
|
const client = result.client
|
|
|
|
// Vérification plan (s'il est fourni, doit appartenir à l'org).
|
|
let planId: string | null = null
|
|
if (fields.planId) {
|
|
const plan = await Plan.query({ client: trx })
|
|
.where('organization_id', organizationId)
|
|
.where('id', fields.planId)
|
|
.first()
|
|
if (plan) planId = plan.id
|
|
}
|
|
|
|
return Invoice.create(
|
|
{
|
|
organizationId,
|
|
clientId: client.id,
|
|
planId,
|
|
numero: fields.numero,
|
|
amountTtcCents: fields.amountTtcCents,
|
|
issueDate: DateTime.fromISO(fields.issueDate),
|
|
dueDate: DateTime.fromISO(fields.dueDate),
|
|
status: 'pending',
|
|
rubisEarned: 1, // bonus saisie initiale (cf. CLAUDE.md → glossaire)
|
|
pdfStorageKey: null,
|
|
notes: null,
|
|
paidAt: null,
|
|
},
|
|
{ client: trx }
|
|
)
|
|
})
|
|
|
|
await invoice.load('client')
|
|
await invoice.load('plan')
|
|
|
|
// Programme les relances BullMQ si la facture a un plan. Hors tx :
|
|
// les jobs sont posés dans Redis, on n'a pas besoin de cohérence DB
|
|
// (et BullMQ.add() retourne avant d'écrire à Redis sur certains modes).
|
|
if (invoice.planId) {
|
|
await scheduleRelancesForInvoice(invoice)
|
|
}
|
|
|
|
return response.status(201).json({ data: serializeInvoice(invoice) })
|
|
}
|
|
|
|
/**
|
|
* POST /invoices/:id/mark-paid
|
|
* Marque encaissée + bonus +1 rubis (à la fois sur invoice.rubisEarned
|
|
* et sur organization.rubisCount). Annule toutes les relances futures.
|
|
*/
|
|
async markPaid({ auth, params, response }: HttpContext) {
|
|
const organizationId = requireOrgId(auth)
|
|
|
|
const invoice = await Invoice.query()
|
|
.where('organization_id', organizationId)
|
|
.where('id', params.id)
|
|
.preload('client')
|
|
.preload('plan')
|
|
.first()
|
|
|
|
if (!invoice) {
|
|
throw new Exception('Facture introuvable', { status: 404, code: 'not_found' })
|
|
}
|
|
if (invoice.status === 'paid') {
|
|
// Idempotent : déjà payée, on renvoie l'état courant sans bumper.
|
|
return response.json({ data: serializeInvoice(invoice) })
|
|
}
|
|
|
|
await db.transaction(async (trx) => {
|
|
invoice.useTransaction(trx)
|
|
invoice.status = 'paid'
|
|
invoice.paidAt = DateTime.now()
|
|
invoice.rubisEarned = invoice.rubisEarned + 1
|
|
await invoice.save()
|
|
|
|
// Bump du compteur agrégé sur l'organisation
|
|
await trx
|
|
.from('organizations')
|
|
.where('id', organizationId)
|
|
.increment('rubis_count', 1)
|
|
|
|
// Journal d'activité (cf. dashboard activity feed).
|
|
await recordActivity({
|
|
organizationId,
|
|
kind: 'invoice_paid',
|
|
label: `Facture <b>${invoice.numero}</b> marquée encaissée`,
|
|
meta: { invoiceId: invoice.id, clientId: invoice.clientId },
|
|
trx,
|
|
})
|
|
|
|
// Annule toutes les relances futures programmées pour cette facture
|
|
// (idempotent, BullMQ.remove peut échouer silencieusement si le
|
|
// job a déjà été consommé).
|
|
await cancelFutureRelances(invoice.id, trx)
|
|
})
|
|
|
|
return response.json({ data: serializeInvoice(invoice) })
|
|
}
|
|
}
|