feat: aggregate account and transaction activity logs on space activity page
This commit is contained in:
parent
c96595d41e
commit
9826068208
7 changed files with 135 additions and 38 deletions
|
|
@ -11,6 +11,8 @@ type TransactionAuditLogRepository interface {
|
|||
CountByTransaction(transactionID string) (int, error)
|
||||
ListByAccount(accountID string, limit, offset int) ([]*model.TransactionAuditLogWithActor, error)
|
||||
CountByAccount(accountID string) (int, error)
|
||||
ListBySpace(spaceID string, limit, offset int) ([]*model.TransactionAuditLogWithActor, error)
|
||||
CountBySpace(spaceID string) (int, error)
|
||||
}
|
||||
|
||||
type transactionAuditLogRepository struct {
|
||||
|
|
@ -88,3 +90,38 @@ func (r *transactionAuditLogRepository) CountByAccount(accountID string) (int, e
|
|||
accountID)
|
||||
return count, err
|
||||
}
|
||||
|
||||
// ListBySpace returns transaction audit entries for any transaction belonging to an
|
||||
// account in this space. Resolves the account via the live transactions row, falling
|
||||
// back to the metadata account_id (set on creation) so entries for deleted transactions
|
||||
// still surface as long as the account itself exists.
|
||||
func (r *transactionAuditLogRepository) ListBySpace(spaceID string, limit, offset int) ([]*model.TransactionAuditLogWithActor, error) {
|
||||
query := `
|
||||
SELECT
|
||||
a.id, a.transaction_id, a.actor_id, a.action, a.metadata, a.created_at,
|
||||
actor.name AS actor_name, actor.email AS actor_email
|
||||
FROM transaction_audit_logs a
|
||||
LEFT JOIN users actor ON actor.id = a.actor_id
|
||||
LEFT JOIN transactions t ON t.id = a.transaction_id
|
||||
LEFT JOIN accounts acc
|
||||
ON acc.id = COALESCE(t.account_id, a.metadata->>'account_id')
|
||||
WHERE acc.space_id = $1
|
||||
ORDER BY a.created_at DESC
|
||||
LIMIT $2 OFFSET $3;`
|
||||
var logs []*model.TransactionAuditLogWithActor
|
||||
err := r.db.Select(&logs, query, spaceID, limit, offset)
|
||||
return logs, err
|
||||
}
|
||||
|
||||
func (r *transactionAuditLogRepository) CountBySpace(spaceID string) (int, error) {
|
||||
var count int
|
||||
err := r.db.Get(&count,
|
||||
`SELECT COUNT(*)
|
||||
FROM transaction_audit_logs a
|
||||
LEFT JOIN transactions t ON t.id = a.transaction_id
|
||||
LEFT JOIN accounts acc
|
||||
ON acc.id = COALESCE(t.account_id, a.metadata->>'account_id')
|
||||
WHERE acc.space_id = $1;`,
|
||||
spaceID)
|
||||
return count, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue