Backend : resource_links + scan_session_id (000021), réconciliation déterministe (same_scan outbox, doublons SHA-256 à l'upload + backfill, same_issuer/référence via faits effectifs, verdicts préservés, élagage auto), GET /files/:id/relations + verdict (owner-only), GET /duplicates (groupes + gain récupérable). Tests service + handlers + migrations. Mobile (Room v22) : links + collections, scanSessionId dans les creates de scan, LinksRepository (sync + verdicts REST), « Voir aussi » dans la fiche (verdict infirmer/re-confirmer), écran Doublons (garder le plus récent), collections locales (sauver/rejouer/supprimer). FR/EN. Contrat docs/api-v1.md (/relations, pas /links — pris par le partage), V3.md (S5 livré, version_of non matérialisé).
187 lines
4.9 KiB
Go
187 lines
4.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"database/sql"
|
|
)
|
|
|
|
// FactParam is one generic fact to persist (V3 S2, migration 000019).
|
|
// ValueNum/ValueDate nil when the type carries no numeric/date value.
|
|
type FactParam struct {
|
|
Type string
|
|
ValueText string
|
|
ValueNum *float64
|
|
ValueDate *string // ISO YYYY-MM-DD
|
|
Confidence float64
|
|
}
|
|
|
|
// FactRow is one persisted fact (auto-extracted or user-corrected).
|
|
type FactRow struct {
|
|
ID string
|
|
Type string
|
|
ValueText string
|
|
ValueNum *float64
|
|
ValueDate *string
|
|
Confidence float64
|
|
Origin string
|
|
}
|
|
|
|
// UpsertAutoFacts replaces the origin='auto' facts of a resource with a fresh
|
|
// extraction. Rows with origin='user' (S3 corrections) are never touched.
|
|
// An empty params clears stale auto facts (e.g. OCR re-run yielding nothing).
|
|
func (r *Resources) UpsertAutoFacts(resourceID string, params []FactParam) error {
|
|
tx, err := r.DB.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback() //nolint:errcheck — no-op after Commit.
|
|
if _, err := tx.Exec(
|
|
`DELETE FROM resource_facts WHERE resource_id = $1 AND origin = 'auto'`,
|
|
resourceID,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
for _, p := range params {
|
|
id, err := NewIDErr()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(
|
|
`INSERT INTO resource_facts
|
|
(id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin)
|
|
VALUES ($1, $2, $3, NULLIF($4, ''), $5, NULLIF($6, '')::date, $7, 'auto')`,
|
|
id, resourceID, p.Type, p.ValueText, nullableFloat(p.ValueNum), derefStr(p.ValueDate), clamp01(p.Confidence),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ListFacts returns the facts of a resource, user-corrected first then auto
|
|
// (stable order by type). No visibility check — the caller enforces viewer+
|
|
// (GetFileVisible) like GetFileOcr.
|
|
func (r *Resources) ListFacts(resourceID string) ([]FactRow, error) {
|
|
rows, err := r.DB.Query(
|
|
`SELECT id, fact_type, COALESCE(value_text, ''), value_num,
|
|
COALESCE(value_date::text, ''), confidence, origin
|
|
FROM resource_facts WHERE resource_id = $1
|
|
ORDER BY CASE origin WHEN 'user' THEN 0 ELSE 1 END, fact_type, value_text`,
|
|
resourceID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := make([]FactRow, 0)
|
|
for rows.Next() {
|
|
var row FactRow
|
|
var num sql.NullFloat64
|
|
var dat string
|
|
if err := rows.Scan(&row.ID, &row.Type, &row.ValueText, &num, &dat, &row.Confidence, &row.Origin); err != nil {
|
|
return nil, err
|
|
}
|
|
if num.Valid {
|
|
v := num.Float64
|
|
row.ValueNum = &v
|
|
}
|
|
if dat != "" {
|
|
d := dat
|
|
row.ValueDate = &d
|
|
}
|
|
out = append(out, row)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// CorrectFacts fully replaces the origin='user' set of a resource (S3 :
|
|
// correction en un tap — le client envoie le jeu corrigé complet, pas un
|
|
// diff). Auto facts are never touched. Empty params clears user corrections
|
|
// (back to pure auto).
|
|
func (r *Resources) CorrectFacts(resourceID string, params []FactParam) error {
|
|
tx, err := r.DB.Begin()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback() //nolint:errcheck — no-op after Commit.
|
|
if _, err := tx.Exec(
|
|
`DELETE FROM resource_facts WHERE resource_id = $1 AND origin = 'user'`,
|
|
resourceID,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
for _, p := range params {
|
|
id, err := NewIDErr()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.Exec(
|
|
`INSERT INTO resource_facts
|
|
(id, resource_id, fact_type, value_text, value_num, value_date, confidence, origin)
|
|
VALUES ($1, $2, $3, NULLIF($4, ''), $5, NULLIF($6, '')::date, $7, 'user')`,
|
|
id, resourceID, p.Type, p.ValueText, nullableFloat(p.ValueNum), derefStr(p.ValueDate), clamp01(p.Confidence),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
type factNeedingExtract struct {
|
|
UserID string
|
|
ResourceID string
|
|
Text string
|
|
}
|
|
|
|
// ListNeedingExtract returns up to limit files with non-empty ocr_text and no
|
|
// origin='auto' facts (boot backfill, S2). Live rows only.
|
|
func (r *Resources) ListNeedingExtract(limit int) ([]factNeedingExtract, error) {
|
|
rows, err := r.DB.Query(
|
|
`SELECT user_id, resource_id, ocr_text FROM resources
|
|
WHERE type = 'file' AND deleted_at IS NULL AND user_id IS NOT NULL
|
|
AND ocr_text IS NOT NULL AND ocr_text <> ''
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM resource_facts
|
|
WHERE resource_facts.resource_id = resources.resource_id
|
|
AND origin = 'auto')
|
|
ORDER BY updated_at ASC LIMIT $1`,
|
|
limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []factNeedingExtract
|
|
for rows.Next() {
|
|
var row factNeedingExtract
|
|
if err := rows.Scan(&row.UserID, &row.ResourceID, &row.Text); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, row)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func nullableFloat(v *float64) any {
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return *v
|
|
}
|
|
|
|
func derefStr(v *string) string {
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
return *v
|
|
}
|
|
|
|
func clamp01(v float64) float64 {
|
|
if v < 0 {
|
|
return 0
|
|
}
|
|
if v > 1 {
|
|
return 1
|
|
}
|
|
return v
|
|
}
|