B: implement audit trail saving
This commit is contained in:
parent
1ecc00a86e
commit
f703f2d1ab
|
@ -12,18 +12,18 @@ import (
|
||||||
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco"
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco"
|
||||||
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/services"
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/services"
|
||||||
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
|
||||||
|
yt_helpers "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/youtube"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/jsonapi"
|
"github.com/google/jsonapi"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"google.golang.org/api/option"
|
"google.golang.org/api/option"
|
||||||
"google.golang.org/api/youtube/v3"
|
"google.golang.org/api/youtube/v3"
|
||||||
yt_helpers "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/youtube"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
eventRegexKeys = map[string]string{"plan": `^services\.v2\.events\.plan\..*`}
|
eventRegexKeys = map[string]string{"plan": `^services\.v2\.events\.plan\..*`}
|
||||||
actionFuncMap = map[string]actionFunc{"youtube.livestream": ScheduleLiveStreamFromWebhook}
|
actionFuncMap = map[string]actionFunc{"youtube.livestream": ScheduleBroadcastFromWebhook}
|
||||||
)
|
)
|
||||||
|
|
||||||
type actionFunc func(*gin.Context, *webhooks.EventDelivery) error
|
type actionFunc func(*gin.Context, *webhooks.EventDelivery) error
|
||||||
|
@ -72,7 +72,6 @@ func ConsumePcoWebhook(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//perform actions
|
//perform actions
|
||||||
//loop through all actions a user has
|
//loop through all actions a user has
|
||||||
for _, mapping := range actionMappings {
|
for _, mapping := range actionMappings {
|
||||||
|
@ -186,29 +185,55 @@ func ScheduleBroadcastFromWebhook(c *gin.Context, body *webhooks.EventDelivery)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Save audit point
|
||||||
|
eventRecievedAudit := &models.EventRecieved{
|
||||||
|
UserId: uid,
|
||||||
|
VendorName: models.PCO_VENDOR_NAME,
|
||||||
|
Type: body.Name,
|
||||||
|
}
|
||||||
|
if err := mongo.SaveModel(eventRecievedAudit); err != nil {
|
||||||
|
log.WithError(err).WithField("EventRecieved", eventRecievedAudit).Error("Failed to save audit event. Logging here and resuming")
|
||||||
|
}
|
||||||
|
|
||||||
//create the broadcast
|
//create the broadcast
|
||||||
//TODO: handle update
|
//TODO: handle update
|
||||||
//TODO: handle delete
|
//TODO: handle delete
|
||||||
|
var broadcast *youtube.LiveBroadcast
|
||||||
switch body.Name {
|
switch body.Name {
|
||||||
case "services.v2.events.plan.created":
|
case "services.v2.events.plan.created":
|
||||||
return scheduleNewBroadcastFromWebhook(c, payload, ytClient, pcoClient)
|
broadcast, err = scheduleNewBroadcastFromWebhook(c, payload, ytClient, pcoClient)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unkown event error: %s", body.Name)
|
return fmt.Errorf("Unkown event error: %s", body.Name)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func scheduleNewBroadcastFromWebhook(c *gin.Context, plan *services.Plan, ytClient *youtube.Service, pcoClient *pco.PcoApiClient) error {
|
//build audit trail after action was taken
|
||||||
times, err := pcoClient.GetPlanTimes(plan.ServiceType.Id, plan.Id)
|
broadcastModel := &models.YoutubeBroadcast{
|
||||||
if err != nil {
|
UserId: uid,
|
||||||
log.WithError(err).Error("Failed to get plan times")
|
Details: broadcast,
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
broadcast, err := yt_helpers.InsertBroadcast(ytClient, plan.Title, times.StartsAt, yt_helpers.STATUS_PRIVATE)
|
actionTaken := &models.ActionTaken{
|
||||||
|
UserId: uid,
|
||||||
|
TriggeringEvent: eventRecievedAudit.MongoId(),
|
||||||
|
Result: []primitive.ObjectID{broadcastModel.MongoId()},
|
||||||
|
VendorName: models.YOUTUBE_VENDOR_NAME,
|
||||||
|
}
|
||||||
|
|
||||||
|
//save audit trail
|
||||||
|
err = mongo.SaveModels(broadcastModel, actionTaken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error("Failed to schedule broadcast")
|
log.WithError(err).Error("Failed to unmarshall body")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func scheduleNewBroadcastFromWebhook(c *gin.Context, plan *services.Plan, ytClient *youtube.Service, pcoClient *pco.PcoApiClient) (*youtube.LiveBroadcast, error) {
|
||||||
|
times, err := pcoClient.GetPlanTimes(plan.ServiceType.Id, plan.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return yt_helpers.InsertBroadcast(ytClient, plan.Title, times.StartsAt, yt_helpers.STATUS_PRIVATE)
|
||||||
|
}
|
||||||
|
|
30
ui/db/db.go
30
ui/db/db.go
|
@ -47,7 +47,35 @@ func (db *DB) SaveModel(m Model) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
|
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
|
||||||
return errors.New("Failed to update vendor account properly")
|
return errors.New("Failed to save model properly")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) SaveModels(m ...Model) error {
|
||||||
|
conf := config.Config()
|
||||||
|
|
||||||
|
writeEntry := make([]mongo.WriteModel, len(m))
|
||||||
|
|
||||||
|
for index, model := range m {
|
||||||
|
entry := mongo.NewUpdateOneModel()
|
||||||
|
entry.SetFilter(bson.M{"_id": model.MongoId})
|
||||||
|
entry.SetUpsert(true)
|
||||||
|
entry.SetUpdate(bson.M{"$set": model})
|
||||||
|
model.UpdateObjectInfo()
|
||||||
|
|
||||||
|
writeEntry[index] = entry
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := options.BulkWrite()
|
||||||
|
res, err := db.client.Database(conf.Mongo.EntDb).Collection(conf.Mongo.EntCol).BulkWrite(context.Background(), writeEntry, opts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.MatchedCount == 0 && res.ModifiedCount == 0 && res.UpsertedCount == 0 {
|
||||||
|
return errors.New("Failed to save models properly")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -17,6 +17,7 @@ type EventRecieved struct {
|
||||||
Id primitive.ObjectID `bson:"_id,omitempty"`
|
Id primitive.ObjectID `bson:"_id,omitempty"`
|
||||||
UserId primitive.ObjectID `bson:"user_id,omitempty"` //what user is this associated too
|
UserId primitive.ObjectID `bson:"user_id,omitempty"` //what user is this associated too
|
||||||
VendorName string `bson:"vendor_name,omitempty"` //Vendor name of who sent us the event
|
VendorName string `bson:"vendor_name,omitempty"` //Vendor name of who sent us the event
|
||||||
|
Type string `bson:"type,omitempty"` //type of event
|
||||||
}
|
}
|
||||||
|
|
||||||
func (obj *EventRecieved) MongoId() primitive.ObjectID {
|
func (obj *EventRecieved) MongoId() primitive.ObjectID {
|
||||||
|
|
Loading…
Reference in New Issue