MistralOcrProvider (app/services/ocr/mistral_ocr_provider.ts) :
- Pipeline 2 étapes : POST /v1/ocr (mistral-ocr-latest) → markdown structuré, puis POST /v1/chat/completions (mistral-large-latest) avec response_format json_schema strict pour extraire les champs typés (clientName/Email, numero, amountTtcCents, issueDate, dueDate) + un objet `_conf` pour la confiance par champ.
- Télécharge le PDF depuis Drive (MinIO en dev) via getArrayBuffer, encode en base64 pour le data URI.
- Throw clair si storageKey null (incompatible avec le mode JSON {filenames}).
- Throw au constructor si MISTRAL_API_KEY manquante.
getOcrProvider() retourne maintenant vraiment Mistral quand OCR_PROVIDER=mistral (plus de fallback silencieux sur mock).
Multipart upload sur POST /invoices/upload :
- Détecte Content-Type. Si multipart/form-data : itère sur `files[]`, valide ext (pdf/png/jpg/jpeg) + size (10mb), upload chaque fichier vers `import-drafts/<orgId>/<draftId>.<ext>` via @adonisjs/drive, puis appelle createImportBatch avec sources [{filename, storageKey}].
- Si JSON : route compat conservée pour le mode démo.
Refactor service import_batch :
- Nouvelle fonction createImportBatch(orgId, sources) générique
- createImportBatchFromFilenames() devient un wrapper compat (storageKey null)
- OCR exécuté HORS transaction (calls réseau Mistral lents — 3-8s par PDF — pas de raison de tenir un lock PG)
Bruno :
- 06-Imports/02 Upload (multipart Mistral).bru — nouveau, body multipart-form avec @file() à sélectionner. Doc : setup .env, where to find files in MinIO console, latence Mistral.
- Renumérote 03/04/05/06 (Get batch / Validate / Skip / Cancel).
- Met à jour 01 Upload (mock) doc pour pointer vers 02 pour le vrai OCR.
Pour tester :
1. .env → OCR_PROVIDER=mistral + MISTRAL_API_KEY=...
2. Restart pnpm dev:api
3. Bruno → Imports → 02 Upload (multipart Mistral) → sélectionne un PDF
4. Bruno → Imports → 03 Get batch (drafts ont pdfStorageKey + extracted depuis l'OCR)
60 lines
1.5 KiB
Plaintext
60 lines
1.5 KiB
Plaintext
meta {
|
|
name: 04 Validate draft
|
|
type: http
|
|
seq: 4
|
|
}
|
|
|
|
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)
|
|
}
|