44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package processing
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
// loadEnabledStandardFields returns enabled standard_fields for fill-missing.
|
||
|
|
func (p *Pipeline) loadEnabledStandardFields(ctx context.Context, companyID uuid.UUID) ([]StandardFieldDef, error) {
|
||
|
|
if p == nil || p.Pool == nil {
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
rows, err := p.Pool.Query(ctx, `
|
||
|
|
SELECT key, COALESCE(default_value, ''), COALESCE(unit, ''), COALESCE(mapping_hints, '[]'::jsonb)
|
||
|
|
FROM standard_fields
|
||
|
|
WHERE company_id = $1 AND enabled = true
|
||
|
|
ORDER BY sort_order ASC, key ASC`, companyID)
|
||
|
|
if err != nil {
|
||
|
|
// Table / columns may be absent in older local DBs — treat as no defs.
|
||
|
|
return nil, nil
|
||
|
|
}
|
||
|
|
defer rows.Close()
|
||
|
|
|
||
|
|
out := make([]StandardFieldDef, 0)
|
||
|
|
for rows.Next() {
|
||
|
|
var key, defVal, unit string
|
||
|
|
var hintsRaw []byte
|
||
|
|
if err := rows.Scan(&key, &defVal, &unit, &hintsRaw); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
var hints any
|
||
|
|
_ = json.Unmarshal(hintsRaw, &hints)
|
||
|
|
out = append(out, StandardFieldDef{
|
||
|
|
Key: key,
|
||
|
|
DefaultValue: defVal,
|
||
|
|
Unit: unit,
|
||
|
|
MappingHints: parseHints(hints),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
return out, rows.Err()
|
||
|
|
}
|