feat: transaction activity audit and account activity audit

This commit is contained in:
juancwu 2026-05-03 23:50:39 +00:00
commit c96595d41e
19 changed files with 1259 additions and 20 deletions

View file

@ -9,6 +9,8 @@ type SpaceAuditLogRepository interface {
Create(log *model.SpaceAuditLog) error
ListBySpace(spaceID string, limit, offset int) ([]*model.SpaceAuditLogWithActor, error)
CountBySpace(spaceID string) (int, error)
ListAccountEvents(accountID string, limit, offset int) ([]*model.SpaceAuditLogWithActor, error)
CountAccountEvents(accountID string) (int, error)
}
type spaceAuditLogRepository struct {
@ -58,3 +60,31 @@ func (r *spaceAuditLogRepository) CountBySpace(spaceID string) (int, error) {
err := r.db.Get(&count, `SELECT COUNT(*) FROM space_audit_logs WHERE space_id = $1;`, spaceID)
return count, err
}
func (r *spaceAuditLogRepository) ListAccountEvents(accountID string, limit, offset int) ([]*model.SpaceAuditLogWithActor, error) {
query := `
SELECT
a.id, a.space_id, a.actor_id, a.action, a.target_user_id, a.target_email,
a.metadata, a.created_at,
actor.name AS actor_name, actor.email AS actor_email,
target.name AS target_user_name, target.email AS target_user_email
FROM space_audit_logs a
LEFT JOIN users actor ON actor.id = a.actor_id
LEFT JOIN users target ON target.id = a.target_user_id
WHERE a.action LIKE 'account.%'
AND a.metadata->>'account_id' = $1
ORDER BY a.created_at DESC
LIMIT $2 OFFSET $3;`
var logs []*model.SpaceAuditLogWithActor
err := r.db.Select(&logs, query, accountID, limit, offset)
return logs, err
}
func (r *spaceAuditLogRepository) CountAccountEvents(accountID string) (int, error) {
var count int
err := r.db.Get(&count,
`SELECT COUNT(*) FROM space_audit_logs
WHERE action LIKE 'account.%' AND metadata->>'account_id' = $1;`,
accountID)
return count, err
}