2021-08-09 04:33:23 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-10 07:58:41 +00:00
|
|
|
"fmt"
|
2021-08-09 04:33:23 +00:00
|
|
|
|
2022-10-10 03:42:39 +00:00
|
|
|
"git.selly.red/Selly-Modules/mongodb"
|
2021-08-09 04:33:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreatePayload ...
|
|
|
|
type CreatePayload struct {
|
|
|
|
Target string
|
|
|
|
TargetID string
|
|
|
|
Action string
|
|
|
|
Data string
|
2022-06-29 04:34:17 +00:00
|
|
|
Message string
|
2021-08-09 04:33:23 +00:00
|
|
|
Author CreatePayloadAuthor
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePayloadAuthor ...
|
|
|
|
type CreatePayloadAuthor struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
2022-06-29 09:57:33 +00:00
|
|
|
Type string
|
2021-08-09 04:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create ...
|
|
|
|
func (s Service) Create(payload CreatePayload) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Get document
|
|
|
|
doc := Audit{
|
|
|
|
ID: mongodb.NewObjectID(),
|
|
|
|
Target: payload.Target,
|
|
|
|
TargetID: payload.TargetID,
|
|
|
|
Action: payload.Action,
|
|
|
|
Data: payload.Data,
|
|
|
|
Author: Author{
|
|
|
|
ID: payload.Author.ID,
|
|
|
|
Name: payload.Author.Name,
|
2022-06-29 09:57:33 +00:00
|
|
|
Type: payload.Author.Type,
|
2021-08-09 04:33:23 +00:00
|
|
|
},
|
2021-08-09 04:44:33 +00:00
|
|
|
CreatedAt: now(),
|
2022-06-29 04:34:17 +00:00
|
|
|
Message: payload.Message,
|
2021-08-09 04:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert to db
|
|
|
|
colName := getColName(payload.Target)
|
|
|
|
if _, err := s.DB.Collection(colName).InsertOne(ctx, doc); err != nil {
|
2022-10-10 07:58:41 +00:00
|
|
|
fmt.Println("audit - Create", err.Error())
|
2021-08-09 04:33:23 +00:00
|
|
|
}
|
|
|
|
}
|