rubis/bruno/06-Imports/03 Validate draft.bru
ordinarthur c7714e3e8a feat(api): import OCR (batch + drafts) avec MockOcrProvider
Migrations :
- import_batches (uuid id, organization_id FK CASCADE)
- import_drafts (uuid id, batch_id FK CASCADE, filename, pdf_storage_key nullable, extracted/edited/confidence en jsonb, status ENUM PG natif pending/validated/skipped, invoice_id FK SET NULL)

Schema rules : tape précisément extracted/edited/confidence (sinon `any`) + status enum.

Services :
- OcrProvider : interface (storageKey + filename → champs avec confiance par champ)
- MockOcrProvider : génère des champs plausibles depuis le filename (numero parsed via regex, montants random multiples de 50cts, dates ISO décalées) + 30 % de cas avec emails à confiance basse pour simuler la review UX
- getOcrProvider() : sélectionne via OCR_PROVIDER env var (default mock, mistral en attente d'ADR-020)
- createImportBatchFromFilenames : compose extracted/edited/confidence par draft, tente un match client immédiat (case-insensitive sur le nom) pour pré-remplir clientId
- resolveClient extrait dans un service partagé (3 priorités : clientId → match nom → création + email requis), réutilisé par invoices_controller et import_batches_controller

Endpoints (auth + scope par organization) :
- POST /invoices/upload : V1 mock body { filenames[] }, 201 → ImportBatch avec ses drafts. Multipart upload réel quand Mistral arrivera, contrat de réponse identique.
- GET /invoices/import-batch/:id : poll pendant la review
- POST /invoices/import-batch/:id/drafts/:draftId/validate : crée Invoice (résolution client) + draft.status=validated + draft.invoiceId
- POST .../drafts/:draftId/skip : draft.status=skipped (idempotent)
- DELETE /invoices/import-batch/:id : CASCADE drop drafts, les invoices validées restent

Routes : ordre soigné — /upload, /counts, /import-batch/* AVANT /:id pour éviter le shadowing.

Bruno : nouveau dossier 06-Imports avec 5 requêtes documentées + capture batchId/draftId dans l'env local. README mis à jour avec le parcours étendu (étapes 11-13).
2026-05-06 14:51:37 +02:00

60 lines
1.5 KiB
Plaintext

meta {
name: 03 Validate draft
type: http
seq: 3
}
post {
url: {{baseUrl}}/api/v1/invoices/import-batch/{{batchId}}/drafts/{{draftId}}/validate
body: json
auth: inherit
}
body:json {
{
"clientId": null,
"clientName": "Atelier Durand",
"clientEmail": "compta@atelier-durand.fr",
"numero": "F-2026-0039",
"amountTtcCents": 360000,
"issueDate": "2026-04-01T09:00:00.000Z",
"dueDate": "2026-05-01T09:00:00.000Z",
"planId": null
}
}
script:post-response {
if (res.getStatus() === 201) {
bru.setEnvVar("invoiceId", res.getBody().data.id);
}
}
tests {
test("201 Created", function () {
expect(res.getStatus()).to.equal(201);
});
test("invoice rubisEarned = 1 (bonus import)", function () {
expect(res.getBody().data.rubisEarned).to.equal(1);
});
}
docs {
POST /api/v1/invoices/import-batch/:id/drafts/:draftId/validate
L'utilisateur a (potentiellement édité puis) validé un draft → on crée
l'Invoice avec les champs envoyés.
Résolution client identique à POST /invoices (cf. service
`resolveClient`) :
1. clientId fourni → utilise tel quel
2. match par nom (case-insensitive) sur les clients existants
3. création à la volée → clientEmail REQUIS sinon 422 `client_email_required`
Le draft passe `pending` → `validated` et capture l'`invoiceId`.
Erreurs :
- 404 not_found (batch ou draft inexistant)
- 409 draft_already_processed (déjà validated/skipped)
- 422 client_email_required (création de client sans email)
}