V3 S3 : correction des faits (fiche document + correct_facts)

Backend : CorrectFacts (editor+, validation Go -> 400 jamais 500),
op outbox correct_facts (full-replace jeu user), PATCH
/files/:id/facts (renvoie jeu complet). Tests service + sync + handlers.

Mobile (Room v20) : table resource_facts + FactsDao, FactsRepository
(sync remplace-en-bloc, correction optimiste + outbox atomique),
fiche « Ce qu'on a compris » sur le viewer (chips, suggéré grisé,
édition par type, reset auto), bouton viewer si processed, FR/EN.

Contrat docs/api-v1.md, V3.md (S1-S3 livrés, FTS -> v21).
This commit is contained in:
venture
2026-09-26 10:28:33 +02:00
parent 95b4cf79cf
commit ac8bd9acb4
45 changed files with 2554 additions and 15 deletions
+73
View File
@@ -1,7 +1,9 @@
package service
import (
"fmt"
"log"
"time"
"github.com/kazier/backend/extract"
"github.com/kazier/backend/repository"
@@ -106,3 +108,74 @@ func (s *Resources) GetFacts(userID, resourceID string) ([]FactDTO, error) {
}
return out, nil
}
// validFactTypes mirrors the resource_facts CHECK (migration 000019).
// Validated in Go so violations map to INVALID_REQUEST, never 500.
var validFactTypes = map[string]bool{
"date": true, "due_date": true, "period": true, "amount": true,
"issuer": true, "reference": true, "doc_type": true,
}
// ErrInvalidFact marks client fact-validation failures (400 INVALID_REQUEST,
// never 500 — cf. FEEDBACK #8). Wrapped by validateFactParam.
var ErrInvalidFact = fmt.Errorf("invalid fact")
// CorrectFacts replaces the user's correction set on a resource (S3 :
// full-replace, not a diff). Requires editor+ like update_metadata
// (A8-style : viewer- ne peut pas corriger → NOT_FOUND halt, pas d'oracle).
// Absent/trashed/foreign → repository.ErrNotFound.
func (s *Resources) CorrectFacts(userID, resourceID string, facts []FactDTO) error {
access, err := s.Repository.Shares.EffectiveAccess(userID, resourceID)
if err != nil {
return err
}
if access.EffectiveRank < repository.AccessEditor {
return repository.ErrNotFound
}
params := make([]repository.FactParam, 0, len(facts))
for i, f := range facts {
p, err := validateFactParam(i, f)
if err != nil {
return err
}
params = append(params, p)
}
return s.Repository.Resources.CorrectFacts(resourceID, params)
}
func validateFactParam(i int, f FactDTO) (repository.FactParam, error) {
if !validFactTypes[f.FactType] {
return repository.FactParam{}, fmt.Errorf("facts[%d].factType invalide: %w", i, ErrInvalidFact)
}
if f.Confidence < 0 || f.Confidence > 1 {
return repository.FactParam{}, fmt.Errorf("facts[%d].confidence hors bornes [0,1]: %w", i, ErrInvalidFact)
}
var num *float64
if f.ValueNum != nil {
n := *f.ValueNum
num = &n
}
var dat *string
if f.ValueDate != "" {
if _, err := time.Parse("2006-01-02", f.ValueDate); err != nil {
return repository.FactParam{}, fmt.Errorf("facts[%d].valueDate doit être ISO YYYY-MM-DD: %w", i, ErrInvalidFact)
}
d := f.ValueDate
dat = &d
}
if f.ValueText == "" && num == nil && dat == nil {
return repository.FactParam{}, fmt.Errorf("facts[%d] sans valeur (valueText|valueNum|valueDate requis): %w", i, ErrInvalidFact)
}
conf := f.Confidence
if conf == 0 {
// Correction manuelle sans confiance explicite = affirmation.
conf = 1.0
}
return repository.FactParam{
Type: f.FactType,
ValueText: f.ValueText,
ValueNum: num,
ValueDate: dat,
Confidence: conf,
}, nil
}