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:
@@ -81,3 +81,88 @@ func TestFactsEndpoint(t *testing.T) {
|
||||
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+emptyID+"/facts", tokenB, nil, "")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-foreign")
|
||||
}
|
||||
|
||||
// TestCorrectFactsEndpoint : PATCH /files/:id/facts — remplace le jeu user
|
||||
// (editor+ = owner ici), renvoie le jeu complet ; 400 sur corps invalide ;
|
||||
// 404 si trashé/étranger.
|
||||
func TestCorrectFactsEndpoint(t *testing.T) {
|
||||
r, store, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "fc"), "facts-test-password", device)
|
||||
|
||||
fileID := repository.NewID()
|
||||
if err := repo.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert: %v", err)
|
||||
}
|
||||
extractSvc := service.NewExtract(repo)
|
||||
if err := extractSvc.ProcessText(fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
||||
t.Fatalf("extract: %v", err)
|
||||
}
|
||||
_ = store
|
||||
|
||||
patch := func(body string) (int, envelope) {
|
||||
t.Helper()
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
||||
var env envelope
|
||||
if rec.Code == http.StatusOK {
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
|
||||
t.Fatalf("patch unmarshal: %v body=%s", err, rec.Body.String())
|
||||
}
|
||||
}
|
||||
return rec.Code, env
|
||||
}
|
||||
|
||||
// Correction : émetteur fixé par l'utilisateur.
|
||||
code, env := patch(`{"facts":[{"factType":"issuer","valueText":"Vrai Fournisseur","confidence":1}]}`)
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("patch: attendu 200, got %d", code)
|
||||
}
|
||||
var facts []struct {
|
||||
FactType string `json:"factType"`
|
||||
ValueText string `json:"valueText"`
|
||||
Origin string `json:"origin"`
|
||||
}
|
||||
if err := json.Unmarshal(env.Data, &facts); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
foundUser, foundAuto := false, false
|
||||
for _, f := range facts {
|
||||
if f.FactType == "issuer" && f.Origin == "user" && f.ValueText == "Vrai Fournisseur" {
|
||||
foundUser = true
|
||||
}
|
||||
if f.Origin == "auto" {
|
||||
foundAuto = true
|
||||
}
|
||||
}
|
||||
if !foundUser || !foundAuto {
|
||||
t.Fatalf("jeu après correction inattendu: %+v", facts)
|
||||
}
|
||||
|
||||
// Corps invalides → 400.
|
||||
for _, body := range []string{
|
||||
`{}`,
|
||||
`{"facts":[{"factType":"couleur","valueText":"bleu"}]}`,
|
||||
`{"facts":[{"factType":"date","valueDate":"12/03/2026"}]}`,
|
||||
`{"facts":[{"factType":"issuer"}]}`,
|
||||
`not-json`,
|
||||
} {
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "facts-patch-400:"+body)
|
||||
}
|
||||
|
||||
// Étranger (viewer simulé : partage + snapshot hors scope ici, un autre
|
||||
// user sans grant suffit — CorrectFacts exige editor+) → 404.
|
||||
deviceB := repository.NewID()
|
||||
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "fd"), "facts-test-password", deviceB)
|
||||
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", tokenB,
|
||||
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-foreign")
|
||||
|
||||
// Trashé → 404.
|
||||
if _, err := store.Trash(userID, fileID); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
rec, _ = doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token,
|
||||
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
||||
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-trashed")
|
||||
}
|
||||
|
||||
@@ -133,6 +133,54 @@ func FilesGetFacts(c *gin.Context) {
|
||||
api.OK(c, facts)
|
||||
}
|
||||
|
||||
// FilesCorrectFacts remplace le jeu `user` des faits (V3 S3) :
|
||||
// PATCH /files/:id/facts `{ facts: [...] }` (full-replace, jamais de diff).
|
||||
// Nécessite editor+ (comme update_metadata) ; viewer- → 404 NOT_FOUND.
|
||||
// Corps invalide (type inconnu, date non ISO, fait sans valeur) → 400.
|
||||
func FilesCorrectFacts(c *gin.Context) {
|
||||
if Store == nil {
|
||||
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
||||
return
|
||||
}
|
||||
userID := c.GetString(UserIDKey)
|
||||
id := c.Param("id")
|
||||
if !deviceIDPattern.MatchString(id) {
|
||||
api.Error(c, http.StatusNotFound, "NOT_FOUND", "file not found")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Facts []service.FactDTO `json:"facts"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body (expected { facts: [...] })")
|
||||
return
|
||||
}
|
||||
if req.Facts == nil {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "missing required field `facts`")
|
||||
return
|
||||
}
|
||||
if len(req.Facts) > 100 {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "`facts` too long (max 100)")
|
||||
return
|
||||
}
|
||||
if err := Store.CorrectFacts(userID, id, req.Facts); err != nil {
|
||||
// Erreurs de validation (type/date/valeur) → 400 explicite, pas 500 :
|
||||
// ce sont des fautes client, jamais des pannes (cf. FEEDBACK #8).
|
||||
if errors.Is(err, service.ErrInvalidFact) {
|
||||
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", err.Error())
|
||||
return
|
||||
}
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
facts, err := Store.GetFacts(userID, id)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
api.OK(c, facts)
|
||||
}
|
||||
|
||||
// own ou shared) : GET /files/:id/content. Réponse binaire (Content-Type du
|
||||
// fichier, Content-Disposition inline) — jamais d'enveloppe {data}. Supporte
|
||||
// Range via http.ServeContent (Gin c.File). Sans bytes serveur (ressource
|
||||
|
||||
@@ -76,6 +76,7 @@ func setup(t *testing.T) (*gin.Engine, *service.Resources, *repository.Repositor
|
||||
handlers.Auth = manager
|
||||
handlers.Store = store
|
||||
handlers.ResetDevicesRateLimit()
|
||||
handlers.ResetLoginRateLimit()
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
|
||||
@@ -34,6 +34,7 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
protected.GET("/files/:id", FilesGet)
|
||||
protected.GET("/files/:id/ocr", FilesGetOcr)
|
||||
protected.GET("/files/:id/facts", FilesGetFacts)
|
||||
protected.PATCH("/files/:id/facts", FilesCorrectFacts)
|
||||
protected.GET("/files/:id/content", FilesContent)
|
||||
protected.GET("/files/:id/shares", SharesList)
|
||||
protected.GET("/files/:id/links", LinksList)
|
||||
|
||||
Reference in New Issue
Block a user