rubis/bruno/06-Imports/01 Upload (mock).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

69 lines
1.7 KiB
Plaintext

meta {
name: 01 Upload (mock)
type: http
seq: 1
}
post {
url: {{baseUrl}}/api/v1/invoices/upload
body: json
auth: inherit
}
body:json {
{
"filenames": [
"facture-martin-042.pdf",
"atelier-durand-2026-039.pdf",
"studio-lefevre-12.pdf"
]
}
}
script:post-response {
if (res.getStatus() === 201) {
const batch = res.getBody().data;
bru.setEnvVar("batchId", batch.id);
if (batch.drafts && batch.drafts.length > 0) {
// Premier draft pending pour les requêtes "validate" / "skip"
const firstPending = batch.drafts.find(d => d.status === "pending") || batch.drafts[0];
bru.setEnvVar("draftId", firstPending.id);
}
}
}
tests {
test("201 Created", function () {
expect(res.getStatus()).to.equal(201);
});
test("3 drafts créés", function () {
expect(res.getBody().data.drafts).to.have.lengthOf(3);
});
test("Chaque draft a extracted + edited + confidence", function () {
const d = res.getBody().data.drafts[0];
expect(d).to.have.property("extracted");
expect(d).to.have.property("edited");
expect(d).to.have.property("confidence");
expect(d.status).to.equal("pending");
});
}
docs {
POST /api/v1/invoices/upload
V1 mock : on envoie un body JSON `{ filenames: [...] }` (pas de fichier
réel). Le service crée un ImportBatch + 1 ImportDraft par filename, en
appelant le `MockOcrProvider` qui invente des champs plausibles depuis
le nom du fichier.
Quand Mistral sera branché : on basculera sur multipart `files[]` avec
upload effectif vers MinIO. Le contrat de réponse reste identique.
Capture `batchId` et `draftId` (le 1er pending) pour les requêtes
suivantes.
Validation :
- 1 à 20 filenames
- Chaque filename ≤ 500 chars
}