Files
Mazyar 241e3b5f2d Implement audit logging functionality across services and middleware
- Introduced a new `auditlog` package to handle audit logging for user actions, including creation, updates, deletions, and authentication events.
- Enhanced existing services (customer, user) to log relevant actions using the new audit logger, capturing details such as actor ID, action type, target type, and success status.
- Added middleware to enrich request context with metadata for audit logging, ensuring comprehensive tracking of user interactions.
- Integrated Elasticsearch for persistent storage of audit logs, with fallback to file-only logging if Elasticsearch is unavailable.
- Updated API documentation to include new audit log endpoints for administrative access.

This update significantly improves the system's ability to track and audit user actions, enhancing security and accountability within the application.
2026-06-29 21:11:33 +03:30

53 lines
1.3 KiB
Go

package audit
import "strconv"
// MergeMetadata combines multiple metadata maps into one.
func MergeMetadata(parts ...map[string]string) map[string]string {
merged := make(map[string]string)
for _, part := range parts {
for key, value := range part {
if value != "" {
merged[key] = value
}
}
}
if len(merged) == 0 {
return nil
}
return merged
}
// FlagMetadata returns a single boolean flag metadata entry when condition is true.
func FlagMetadata(key string, condition bool) map[string]string {
if !condition {
return nil
}
return map[string]string{key: "true"}
}
// StringMetadata returns metadata for a non-empty string value.
func StringMetadata(key, value string) map[string]string {
if value == "" {
return nil
}
return map[string]string{key: value}
}
// OptionalStringMetadata returns metadata when a pointer string is set and non-empty.
func OptionalStringMetadata(key string, value *string) map[string]string {
if value == nil || *value == "" {
return nil
}
return map[string]string{key: *value}
}
// PaginationMetadata returns safe pagination metadata for list audit events.
func PaginationMetadata(limit, offset int, totalCount int64) map[string]string {
return map[string]string{
"limit": strconv.Itoa(limit),
"offset": strconv.Itoa(offset),
"result_count": strconv.FormatInt(totalCount, 10),
}
}