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).
135 lines
4.5 KiB
TypeScript
135 lines
4.5 KiB
TypeScript
/*
|
|
|--------------------------------------------------------------------------
|
|
| Routes file
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| Toutes les routes sont sous /api/v1/. Les groupes auth et account sont
|
|
| importés depuis le contrôleur généré par Tuyau pour garantir le contrat
|
|
| client typé (cf. docs/tech/backend.md §8).
|
|
|
|
|
*/
|
|
|
|
import { middleware } from '#start/kernel'
|
|
import router from '@adonisjs/core/services/router'
|
|
import { controllers } from '#generated/controllers'
|
|
|
|
router.get('/', () => {
|
|
return { hello: 'world' }
|
|
})
|
|
|
|
router
|
|
.group(() => {
|
|
/**
|
|
* Auth — public.
|
|
*/
|
|
router
|
|
.group(() => {
|
|
router.post('signup', [controllers.NewAccount, 'store']).as('signup')
|
|
router.post('login', [controllers.AccessTokens, 'store']).as('login')
|
|
})
|
|
.prefix('auth')
|
|
.as('auth')
|
|
|
|
/**
|
|
* Compte courant — auth requise.
|
|
*/
|
|
router
|
|
.group(() => {
|
|
router.get('profile', [controllers.Profile, 'show']).as('profile.show')
|
|
router.patch('profile', [controllers.Profile, 'update']).as('profile.update')
|
|
router.post('logout', [controllers.AccessTokens, 'destroy']).as('logout')
|
|
})
|
|
.prefix('account')
|
|
.as('account')
|
|
.use(middleware.auth())
|
|
|
|
/**
|
|
* Organisation rattachée à l'utilisateur courant — auth requise.
|
|
*/
|
|
router
|
|
.group(() => {
|
|
router.get('me', [controllers.Organizations, 'show']).as('show')
|
|
router.patch('me', [controllers.Organizations, 'update']).as('update')
|
|
})
|
|
.prefix('organizations')
|
|
.as('organizations')
|
|
.use(middleware.auth())
|
|
|
|
/**
|
|
* Clients — auth requise, scope par organization de l'utilisateur courant.
|
|
*/
|
|
router
|
|
.group(() => {
|
|
router.get('', [controllers.Clients, 'index']).as('index')
|
|
router.post('', [controllers.Clients, 'store']).as('store')
|
|
router.get(':id', [controllers.Clients, 'show']).as('show').where('id', router.matchers.uuid())
|
|
router.patch(':id', [controllers.Clients, 'update']).as('update').where('id', router.matchers.uuid())
|
|
})
|
|
.prefix('clients')
|
|
.as('clients')
|
|
.use(middleware.auth())
|
|
|
|
/**
|
|
* Plans — auth requise. Lookup par slug (stable et lisible :
|
|
* /parametres/plans/standard-30j). POST plan custom = V2.
|
|
*/
|
|
router
|
|
.group(() => {
|
|
router.get('', [controllers.Plans, 'index']).as('index')
|
|
router.get(':slug', [controllers.Plans, 'show']).as('show')
|
|
router.patch(':slug', [controllers.Plans, 'update']).as('update')
|
|
})
|
|
.prefix('plans')
|
|
.as('plans')
|
|
.use(middleware.auth())
|
|
|
|
/**
|
|
* Invoices — auth requise. Ordre IMPORTANT : les routes statiques
|
|
* (/upload, /counts, /import-batch/...) sont déclarées AVANT /:id
|
|
* sinon `:id` matche les segments littéraux.
|
|
*/
|
|
router
|
|
.group(() => {
|
|
router.get('', [controllers.Invoices, 'index']).as('index')
|
|
router.post('', [controllers.Invoices, 'store']).as('store')
|
|
router.get('counts', [controllers.Invoices, 'counts']).as('counts')
|
|
|
|
// OCR / Import batch (cf. ImportBatchesController)
|
|
router.post('upload', [controllers.ImportBatches, 'upload']).as('upload')
|
|
router
|
|
.get('import-batch/:id', [controllers.ImportBatches, 'show'])
|
|
.as('import-batch.show')
|
|
.where('id', router.matchers.uuid())
|
|
router
|
|
.post('import-batch/:id/drafts/:draftId/validate', [
|
|
controllers.ImportBatches,
|
|
'validateDraft',
|
|
])
|
|
.as('import-batch.draft.validate')
|
|
.where('id', router.matchers.uuid())
|
|
.where('draftId', router.matchers.uuid())
|
|
router
|
|
.post('import-batch/:id/drafts/:draftId/skip', [
|
|
controllers.ImportBatches,
|
|
'skipDraft',
|
|
])
|
|
.as('import-batch.draft.skip')
|
|
.where('id', router.matchers.uuid())
|
|
.where('draftId', router.matchers.uuid())
|
|
router
|
|
.delete('import-batch/:id', [controllers.ImportBatches, 'destroy'])
|
|
.as('import-batch.destroy')
|
|
.where('id', router.matchers.uuid())
|
|
|
|
router.get(':id', [controllers.Invoices, 'show']).as('show').where('id', router.matchers.uuid())
|
|
router
|
|
.post(':id/mark-paid', [controllers.Invoices, 'markPaid'])
|
|
.as('mark-paid')
|
|
.where('id', router.matchers.uuid())
|
|
})
|
|
.prefix('invoices')
|
|
.as('invoices')
|
|
.use(middleware.auth())
|
|
})
|
|
.prefix('/api/v1')
|