39 lines
1.4 KiB
Go
39 lines
1.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
type SourceAuditItem struct {
|
|
ID string `json:"id"`
|
|
ConnectionID string `json:"connection_id"`
|
|
SourceName string `json:"source_name"`
|
|
Action string `json:"action"`
|
|
Actor string `json:"actor"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
Detail string `json:"detail"`
|
|
}
|
|
type SourceAuditPage struct {
|
|
Items []SourceAuditItem `json:"items"`
|
|
Total int `json:"total"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
|
|
func (s *Store) ListSourceAudit(ctx context.Context, limit, offset int) (SourceAuditPage, error) {
|
|
if limit < 1 || limit > 100 || offset < 0 {
|
|
return SourceAuditPage{}, errors.New("invalid audit pagination")
|
|
}
|
|
var raw []byte
|
|
err := s.db.QueryRow(ctx, `SELECT jsonb_build_object('total',(SELECT count(*) FROM source_configuration_events),'limit',$1::int,'offset',$2::int,'items',COALESCE((SELECT jsonb_agg(to_jsonb(e) ORDER BY e.created_at DESC,e.sort_id DESC) FROM (SELECT id::text AS id,id AS sort_id,connection_id,source_name,action,actor,created_at,detail FROM source_configuration_events ORDER BY created_at DESC,id DESC LIMIT $1 OFFSET $2) e),'[]'::jsonb))`, limit, offset).Scan(&raw)
|
|
if err != nil {
|
|
return SourceAuditPage{}, err
|
|
}
|
|
var page SourceAuditPage
|
|
err = json.Unmarshal(raw, &page)
|
|
return page, err
|
|
}
|