Files
2025-11-25 15:12:31 +08:00

63 lines
1.5 KiB
Go

package helper
import (
"authentication/models"
"authentication/services"
"encoding/json"
"time"
)
func LogEvent(id string, user *string, ipAddress string, actType int, fieldUpdate interface{}) error {
fieldUpdated := new(json.RawMessage)
if fieldUpdate != nil {
data, err := json.Marshal(fieldUpdate)
if err != nil {
LogError(err, "Error marshalling field update")
return err
}
fieldUpdated = (*json.RawMessage)(&data)
}
params := models.LogEventParams{
ActivityType: actType,
IPAddress: ipAddress,
FieldUpdated: fieldUpdated,
ErrorMessage: ErrorFailedtoLogLoginEvent,
}
return LogLoginEventParams(params, ipAddress)
}
func LogLoginEventV2(id string, ipAddress string) error {
params := models.LogEventParams{
ActivityType: 17,
IPAddress: ipAddress,
FieldUpdated: new(json.RawMessage),
ErrorMessage: ErrorFailedtoLogLoginEvent,
}
return LogLoginEventParams(params, ipAddress)
}
func LogLoginEventParams(params models.LogEventParams, ipAddress string) error {
location, err := LoadAsiaManilaLocation()
if err != nil {
LogError(err, "Failed to load Asia/Manila timezone, using UTC+8 offset")
}
currentTime := time.Now().In(location)
accessLog := models.UserAccessLog{
UserID: params.UserID,
ParticipantID: params.ParticipantID,
ActivityType: params.ActivityType,
IPAddress: ipAddress,
FieldUpdated: params.FieldUpdated,
Time: currentTime,
}
err = services.InsertAccessLogLogin(accessLog)
if err != nil {
LogError(err, params.ErrorMessage)
return err
}
return nil
}