1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-20 13:49:42 +02:00
documize/core/api/request/event.go

60 lines
1.4 KiB
Go
Raw Normal View History

2017-05-04 12:31:52 +01:00
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package request
import (
"time"
"github.com/documize/community/core/api/entity"
"github.com/documize/community/core/log"
)
// RecordEvent adds event entry for specified user.
func (p *Persister) RecordEvent(t entity.EventType) {
e := entity.AppEvent{}
e.OrgID = p.Context.OrgID
e.UserID = p.Context.UserID
e.Created = time.Now().UTC()
2017-05-05 10:44:52 +01:00
e.IP = p.Context.ClientIP
2017-05-04 12:31:52 +01:00
e.Type = string(t)
if e.OrgID == "" || e.UserID == "" {
log.Info("Missing OrgID/UserID for event record " + e.Type)
return
}
tx, err := Db.Beginx()
if err != nil {
log.Error("Unable to prepare insert RecordEvent", err)
return
}
2017-05-05 10:44:52 +01:00
stmt, err := tx.Preparex("INSERT INTO userevent (orgid, userid, eventtype, ip, created) VALUES (?, ?, ?, ?, ?)")
2017-05-04 12:31:52 +01:00
if err != nil {
tx.Rollback()
log.Error("Unable to prepare insert RecordEvent", err)
return
}
2017-05-05 10:44:52 +01:00
_, err = stmt.Exec(e.OrgID, e.UserID, e.Type, e.IP, e.Created)
2017-05-04 12:31:52 +01:00
if err != nil {
log.Error("Unable to execute insert RecordEvent", err)
tx.Rollback()
return
}
stmt.Close()
tx.Commit()
return
}