So the receiving service can look up a handoff it already made before it repeats one (tenant-registry#119). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aRnCGqELxoix5TfpX8Ejf
172 lines
5.3 KiB
Go
172 lines
5.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
func validCapture(c IntakeCapture) bool {
|
|
return c.Channel == IntakeChannelPortal && c.ReporterSub != "" && len(c.ReporterSub) <= 200 &&
|
|
slices.Contains([]string{IntakeAboutProduct, IntakeAboutITOT, IntakeAboutUnknown}, c.About) &&
|
|
slices.Contains([]string{"yes", "no", "unknown"}, c.Danger) && isJSONObject(c.Original)
|
|
}
|
|
|
|
func validReceipt(r IntakeReceipt) bool {
|
|
return slices.Contains([]string{IntakeWaiting, IntakeDelivered, IntakeProcessed, IntakeRefused}, r.Status) &&
|
|
len(r.Reason) <= 80 && len(r.HandoffID) <= 80 && len(r.RunID) <= 80 && r.By != "" && len(r.By) <= 200
|
|
}
|
|
|
|
func isJSONObject(b json.RawMessage) bool {
|
|
var v map[string]any
|
|
return json.Unmarshal(b, &v) == nil && v != nil
|
|
}
|
|
|
|
func (m *Memory) CreateIntakeReport(_ context.Context, c IntakeCapture) (*IntakeReport, *IntakeEntry, error) {
|
|
if !validCapture(c) {
|
|
return nil, nil, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[c.TenantID]; !ok {
|
|
return nil, nil, ErrNotFound
|
|
}
|
|
m.intakeCounters[c.TenantID]++
|
|
at := c.At.UTC().Truncate(time.Microsecond)
|
|
r := IntakeReport{TenantID: c.TenantID, Reference: IntakeReference(at, m.intakeCounters[c.TenantID]), ReceivedAt: at,
|
|
Channel: c.Channel, ReporterSub: c.ReporterSub, About: c.About, Danger: c.Danger}
|
|
m.intakeReports = append(m.intakeReports, r)
|
|
e := m.addEntry(c.TenantID, r.Reference, IntakeEntryCapture, c.ReporterSub, c.Original, at)
|
|
return &r, &e, nil
|
|
}
|
|
|
|
func (m *Memory) addEntry(tenantID, ref, kind, by string, content json.RawMessage, at time.Time) IntakeEntry {
|
|
m.intakeEntryID++
|
|
e := IntakeEntry{ID: m.intakeEntryID, TenantID: tenantID, Reference: ref, Kind: kind,
|
|
Content: append(json.RawMessage(nil), content...), By: by, At: at, Receipts: []IntakeReceipt{}}
|
|
m.intakeEntries = append(m.intakeEntries, e)
|
|
return e
|
|
}
|
|
|
|
func (m *Memory) report(tenantID, ref string) (IntakeReport, bool) {
|
|
for _, r := range m.intakeReports {
|
|
if r.TenantID == tenantID && r.Reference == ref {
|
|
return r, true
|
|
}
|
|
}
|
|
return IntakeReport{}, false
|
|
}
|
|
|
|
func (m *Memory) GetIntakeReport(_ context.Context, tenantID, ref string) (*IntakeReport, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
r, ok := m.report(tenantID, ref)
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
func (m *Memory) ListIntakeReports(_ context.Context, tenantID, reporterSub string) ([]IntakeReport, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []IntakeReport{}
|
|
for i := len(m.intakeReports) - 1; i >= 0; i-- {
|
|
if r := m.intakeReports[i]; r.TenantID == tenantID && r.ReporterSub == reporterSub {
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) IntakeEntries(_ context.Context, tenantID, ref string) ([]IntakeEntry, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
if _, ok := m.report(tenantID, ref); !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
out := []IntakeEntry{}
|
|
for _, e := range m.intakeEntries {
|
|
if e.TenantID == tenantID && e.Reference == ref {
|
|
e.Receipts = slices.Clone(e.Receipts)
|
|
out = append(out, e)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) AddIntakeSupplement(_ context.Context, tenantID, ref, by string, content json.RawMessage, at time.Time) (*IntakeEntry, error) {
|
|
if by == "" || len(by) > 200 || !isJSONObject(content) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.report(tenantID, ref); !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
e := m.addEntry(tenantID, ref, IntakeEntrySupplement, by, content, at.UTC().Truncate(time.Microsecond))
|
|
return &e, nil
|
|
}
|
|
|
|
func (m *Memory) PendingIntake(_ context.Context, limit int) ([]PendingIntake, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []PendingIntake{}
|
|
for _, e := range m.intakeEntries {
|
|
if len(out) >= limit {
|
|
break
|
|
}
|
|
if n := len(e.Receipts); n > 0 && e.Receipts[n-1].Settled() {
|
|
continue
|
|
}
|
|
r, _ := m.report(e.TenantID, e.Reference)
|
|
p := PendingIntake{EntryID: e.ID, TenantID: e.TenantID, Reference: e.Reference, Kind: e.Kind, At: e.At,
|
|
ReceivedAt: r.ReceivedAt, Channel: r.Channel, About: r.About, Danger: r.Danger}
|
|
if n := len(e.Receipts); n > 0 {
|
|
last := e.Receipts[n-1]
|
|
p.Status, p.Reason, p.HandoffID = last.Status, last.Reason, last.HandoffID
|
|
}
|
|
out = append(out, p)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m *Memory) AddIntakeReceipt(_ context.Context, r IntakeReceipt) (bool, error) {
|
|
if !validReceipt(r) {
|
|
return false, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for i := range m.intakeEntries {
|
|
e := &m.intakeEntries[i]
|
|
if e.ID != r.EntryID {
|
|
continue
|
|
}
|
|
if n := len(e.Receipts); n > 0 {
|
|
last := e.Receipts[n-1]
|
|
if last.Settled() {
|
|
return false, ErrConflict
|
|
}
|
|
if sameReceipt(last, r) {
|
|
return false, nil
|
|
}
|
|
}
|
|
r.At = r.At.UTC().Truncate(time.Microsecond)
|
|
e.Receipts = append(e.Receipts, r)
|
|
return true, nil
|
|
}
|
|
return false, ErrNotFound
|
|
}
|
|
|
|
// sameReceipt: a receipt that repeats the newest one adds nothing.
|
|
func sameReceipt(a, b IntakeReceipt) bool {
|
|
return a.Status == b.Status && a.Reason == b.Reason && a.HandoffID == b.HandoffID && a.RunID == b.RunID
|
|
}
|
|
|
|
func (m *Memory) deleteIntake(tenantID string) {
|
|
delete(m.intakeCounters, tenantID)
|
|
m.intakeReports = slices.DeleteFunc(m.intakeReports, func(r IntakeReport) bool { return r.TenantID == tenantID })
|
|
m.intakeEntries = slices.DeleteFunc(m.intakeEntries, func(e IntakeEntry) bool { return e.TenantID == tenantID })
|
|
}
|