42 lines
916 B
Go
42 lines
916 B
Go
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"database/sql"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
type AuditLogRepo struct {
|
||
|
|
db *sql.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewAuditLogRepo(db *sql.DB) *AuditLogRepo {
|
||
|
|
return &AuditLogRepo{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (r *AuditLogRepo) Create(ctx context.Context, userID, companyID, action, resourceType, resourceID, ip string, newValue any) error {
|
||
|
|
id := uuid.New().String()
|
||
|
|
|
||
|
|
var valueJSON []byte
|
||
|
|
if newValue != nil {
|
||
|
|
var err error
|
||
|
|
valueJSON, err = json.Marshal(newValue)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("marshal audit value: %w", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
_, err := r.db.ExecContext(ctx,
|
||
|
|
`INSERT INTO ilm_audit_log (id, user_id, company_id, action, resource_type, resource_id, new_value, ip)
|
||
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||
|
|
id, userID, companyID, action, resourceType, resourceID, valueJSON, ip,
|
||
|
|
)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("create audit log: %w", err)
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|