Files
story/internal/webevents/history.go
T
Codex Local bf1233d5df
ci / verify (push) Canceled after 0s
feat: expand companion platform, device controls, and memoir workflows
Add customer and operator login, BLE provisioning, device fleet controls, call memory, memoir generation, and refreshed family and maintenance portals. Include API contracts, migrations, firmware sources, tests, assets, and deployment documentation.
2026-10-08 10:06:44 +08:00

36 lines
1.5 KiB
Go

package webevents
import (
"context"
"data-terminal-cloud/internal/identity"
"time"
)
// History only returns retained metadata from households the caller still belongs to.
func (s *Service) History(ctx context.Context, principal identity.Principal, since time.Time, limit int) ([]Event, error) {
if limit < 1 || limit > 200 {
limit = 200
}
rows, err := s.pool.Query(ctx, `SELECT we.id,we.event_type,h.public_id::text,we.resource_type,we.resource_public_id::text,COALESCE(sp.public_id::text,''),we.status,we.occurred_at
FROM web_event we JOIN household h ON h.id=we.household_id AND h.tenant_id=we.tenant_id AND h.status='active'
LEFT JOIN subject_profile sp ON sp.id=we.subject_id AND sp.tenant_id=we.tenant_id
WHERE we.occurred_at >= $3 AND we.expires_at>now() AND EXISTS (
SELECT 1 FROM household_member hm JOIN user_account ua ON ua.id=hm.user_id AND ua.tenant_id=hm.tenant_id AND ua.status='active'
JOIN tenant t ON t.id=ua.tenant_id AND t.status='active'
WHERE hm.tenant_id=we.tenant_id AND hm.household_id=we.household_id AND hm.status='active' AND ua.oidc_issuer=$1 AND ua.oidc_subject=$2)
ORDER BY we.id DESC LIMIT $4`, principal.Issuer, principal.Subject, since, limit)
if err != nil {
return nil, err
}
defer rows.Close()
items := make([]Event, 0)
for rows.Next() {
var item Event
if err := rows.Scan(&item.ID, &item.Type, &item.HouseholdID, &item.ResourceType, &item.ResourceID, &item.SubjectID, &item.Status, &item.OccurredAt); err != nil {
return nil, err
}
items = append(items, item)
}
return items, rows.Err()
}