feat(audit): record enterprise mutations

This commit is contained in:
2026-06-05 10:33:33 +08:00
parent 3f8f04d65a
commit 4597d404fd
9 changed files with 341 additions and 26 deletions
+29 -1
View File
@@ -8,13 +8,15 @@ import (
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/automation"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
)
// MacroHandler handles macro API endpoints.
// Reference: Chatwoot MacrosController — CRUD + execute
type MacroHandler struct {
svc *automation.MacroService
svc *automation.MacroService
auditSvc *service.AuditService
}
// NewMacroHandler creates a new MacroHandler.
@@ -22,6 +24,11 @@ func NewMacroHandler(svc *automation.MacroService) *MacroHandler {
return &MacroHandler{svc: svc}
}
func (h *MacroHandler) WithAuditService(auditSvc *service.AuditService) *MacroHandler {
h.auditSvc = auditSvc
return h
}
// List retrieves all macros for an account, respecting visibility.
// GET /api/v1/accounts/:account_id/macros
func (h *MacroHandler) List(c *gin.Context) {
@@ -101,6 +108,13 @@ func (h *MacroHandler) Create(c *gin.Context) {
handleServiceError(c, svcErr)
return
}
recordAuditMutation(c, h.auditSvc, auditMutation{
AccountID: accountID,
AuditableType: "Macro",
AuditableID: macro.ID,
Action: "create",
AuditedChanges: serializeMacro(macro),
})
c.JSON(http.StatusOK, gin.H{"payload": serializeMacro(macro)})
}
@@ -143,6 +157,13 @@ func (h *MacroHandler) Update(c *gin.Context) {
handleServiceError(c, svcErr)
return
}
recordAuditMutation(c, h.auditSvc, auditMutation{
AccountID: accountID,
AuditableType: "Macro",
AuditableID: updated.ID,
Action: "update",
AuditedChanges: serializeMacro(updated),
})
c.JSON(http.StatusOK, gin.H{"payload": serializeMacro(updated)})
}
@@ -175,6 +196,13 @@ func (h *MacroHandler) Delete(c *gin.Context) {
handleServiceError(c, svcErr)
return
}
recordAuditMutation(c, h.auditSvc, auditMutation{
AccountID: accountID,
AuditableType: "Macro",
AuditableID: macroID,
Action: "destroy",
AuditedChanges: gin.H{"id": macroID},
})
c.Status(http.StatusOK)
}