B: Formatting
This commit is contained in:
parent
920e203b9d
commit
46f9460a37
|
@ -28,7 +28,7 @@ var (
|
||||||
|
|
||||||
type actionFunc func(*gin.Context, *webhooks.EventDelivery) error
|
type actionFunc func(*gin.Context, *webhooks.EventDelivery) error
|
||||||
|
|
||||||
func userIdFromContext(c *gin.Context) (*primitive.ObjectID) {
|
func userIdFromContext(c *gin.Context) *primitive.ObjectID {
|
||||||
if id, ok := c.Get("user_bson_id"); !ok {
|
if id, ok := c.Get("user_bson_id"); !ok {
|
||||||
userId := c.Param("userid")
|
userId := c.Param("userid")
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/google/jsonapi"
|
"github.com/google/jsonapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
//gets all current subscriptions
|
// gets all current subscriptions
|
||||||
func (api *PcoApiClient) GetSubscriptions() ([]webhooks.Subscription, error) {
|
func (api *PcoApiClient) GetSubscriptions() ([]webhooks.Subscription, error) {
|
||||||
api.Url().Path = "/webhook/v2/subscriptions"
|
api.Url().Path = "/webhook/v2/subscriptions"
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ func (api *PcoApiClient) GetSubscriptions() ([]webhooks.Subscription, error) {
|
||||||
return subscriptions, nil
|
return subscriptions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//Posts subscriptions to PCO api and returns a new list of subscriptions
|
// Posts subscriptions to PCO api and returns a new list of subscriptions
|
||||||
func (api *PcoApiClient) CreateSubscriptions(subscriptions []webhooks.Subscription) ([]webhooks.Subscription, error) {
|
func (api *PcoApiClient) CreateSubscriptions(subscriptions []webhooks.Subscription) ([]webhooks.Subscription, error) {
|
||||||
api.Url().Path = "/webhook/v2/subscriptions"
|
api.Url().Path = "/webhook/v2/subscriptions"
|
||||||
|
|
||||||
|
@ -67,8 +67,8 @@ func (api *PcoApiClient) CreateSubscriptions(subscriptions []webhooks.Subscripti
|
||||||
return new_subscriptions, nil
|
return new_subscriptions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//Posts subcription to PCO api and updates the subscription at the pointer that was passed to the fuinction with the server response
|
// Posts subcription to PCO api and updates the subscription at the pointer that was passed to the fuinction with the server response
|
||||||
func (api *PcoApiClient) CreateSubscription(subscription *webhooks.Subscription) (error) {
|
func (api *PcoApiClient) CreateSubscription(subscription *webhooks.Subscription) error {
|
||||||
api.Url().Path = "/webhook/v2/subscriptions"
|
api.Url().Path = "/webhook/v2/subscriptions"
|
||||||
|
|
||||||
body := bytes.NewBuffer([]byte{})
|
body := bytes.NewBuffer([]byte{})
|
||||||
|
@ -91,7 +91,6 @@ func (api *PcoApiClient) CreateSubscription(subscription *webhooks.Subscription)
|
||||||
return fmt.Errorf("Failed to retrieve plan with status code: %d", resp.StatusCode)
|
return fmt.Errorf("Failed to retrieve plan with status code: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
err = jsonapi.UnmarshalPayload(resp.Body, subscription)
|
err = jsonapi.UnmarshalPayload(resp.Body, subscription)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -3,11 +3,11 @@ package webhooks
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type Subscription struct {
|
type Subscription struct {
|
||||||
Id string `jsonapi:"primary,Subscription" bson:"id"`
|
Id string `jsonapi:"primary,Subscription" bson:"id"`
|
||||||
//attrs
|
//attrs
|
||||||
Active bool `jsonapi:"attr,active,omitempty" bson:"active"`
|
Active bool `jsonapi:"attr,active,omitempty" bson:"active"`
|
||||||
ApplicationId string `jsonapi:"attr,application_id,omitempty" bson:"application_id"`
|
ApplicationId string `jsonapi:"attr,application_id,omitempty" bson:"application_id"`
|
||||||
AuthenticitySecret string `jsonapi:"attr,authenticity_secret,omitempty" bson:"authenticity_secret"`
|
AuthenticitySecret string `jsonapi:"attr,authenticity_secret,omitempty" bson:"authenticity_secret"`
|
||||||
CreatedAt time.Time `jsonapi:"attr,created_at,omitempty" bson:"created_at"`
|
CreatedAt time.Time `jsonapi:"attr,created_at,omitempty" bson:"created_at"`
|
||||||
UpdatedAt time.Time `jsonapi:"attr,updated_at,omitempty" bson:"updated_at"`
|
UpdatedAt time.Time `jsonapi:"attr,updated_at,omitempty" bson:"updated_at"`
|
||||||
Name string `jsonapi:"attr,name,omitempty" bson:"name"`
|
Name string `jsonapi:"attr,name,omitempty" bson:"name"`
|
||||||
|
@ -15,11 +15,11 @@ type Subscription struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebhookSubscription struct {
|
type WebhookSubscription struct {
|
||||||
Id string `jsonapi:"primary,WebhookSubscription" bson:"id"`
|
Id string `jsonapi:"primary,WebhookSubscription" bson:"id"`
|
||||||
//attrs
|
//attrs
|
||||||
Active bool `jsonapi:"attr,active,omitempty" bson:"active"`
|
Active bool `jsonapi:"attr,active,omitempty" bson:"active"`
|
||||||
ApplicationId string `jsonapi:"attr,application_id,omitempty" bson:"application_id"`
|
ApplicationId string `jsonapi:"attr,application_id,omitempty" bson:"application_id"`
|
||||||
AuthenticitySecret string `jsonapi:"attr,authenticity_secret,omitempty" bson:"authenticity_secret"`
|
AuthenticitySecret string `jsonapi:"attr,authenticity_secret,omitempty" bson:"authenticity_secret"`
|
||||||
CreatedAt time.Time `jsonapi:"attr,created_at,omitempty" bson:"created_at"`
|
CreatedAt time.Time `jsonapi:"attr,created_at,omitempty" bson:"created_at"`
|
||||||
UpdatedAt time.Time `jsonapi:"attr,updated_at,omitempty" bson:"updated_at"`
|
UpdatedAt time.Time `jsonapi:"attr,updated_at,omitempty" bson:"updated_at"`
|
||||||
Name string `jsonapi:"attr,name,omitempty" bson:"name"`
|
Name string `jsonapi:"attr,name,omitempty" bson:"name"`
|
||||||
|
|
|
@ -15,7 +15,7 @@ import (
|
||||||
type actionFunc func(user *models.User) error
|
type actionFunc func(user *models.User) error
|
||||||
|
|
||||||
var (
|
var (
|
||||||
actionFuncs map[string]actionFunc = map[string]actionFunc{"pco.plan": setupPcoSubscriptions}
|
actionFuncs map[string]actionFunc = map[string]actionFunc{"pco.plan": setupPcoSubscriptions}
|
||||||
webhooksTemplate map[string]webhooks.Subscription = map[string]webhooks.Subscription{
|
webhooksTemplate map[string]webhooks.Subscription = map[string]webhooks.Subscription{
|
||||||
"services.v2.events.plan.created": {
|
"services.v2.events.plan.created": {
|
||||||
Active: true,
|
Active: true,
|
||||||
|
@ -111,7 +111,7 @@ func setupPcoSubscriptions(user *models.User) error {
|
||||||
webhookMap := make(map[string]webhooks.Subscription)
|
webhookMap := make(map[string]webhooks.Subscription)
|
||||||
subscriptions, err := pcoApi.GetSubscriptions()
|
subscriptions, err := pcoApi.GetSubscriptions()
|
||||||
//Loop through found subscriptions
|
//Loop through found subscriptions
|
||||||
for _, sub := range subscriptions{
|
for _, sub := range subscriptions {
|
||||||
//if subsciption is in the templates look to add it to our map
|
//if subsciption is in the templates look to add it to our map
|
||||||
if templ, ok := webhooksTemplate[sub.Name]; ok {
|
if templ, ok := webhooksTemplate[sub.Name]; ok {
|
||||||
//if the subscription is for our url add it to our map
|
//if the subscription is for our url add it to our map
|
||||||
|
@ -127,9 +127,9 @@ func setupPcoSubscriptions(user *models.User) error {
|
||||||
for _, templ := range webhooksTemplate {
|
for _, templ := range webhooksTemplate {
|
||||||
if _, ok := webhookMap[templ.Name]; !ok {
|
if _, ok := webhookMap[templ.Name]; !ok {
|
||||||
builtHooks = append(builtHooks, webhooks.Subscription{
|
builtHooks = append(builtHooks, webhooks.Subscription{
|
||||||
Active: false,
|
Active: false,
|
||||||
Name: templ.Name,
|
Name: templ.Name,
|
||||||
Url: fmt.Sprintf(templ.Url, conf.AppSettings.WebhookServiceUrl, user.Id.Hex()),
|
Url: fmt.Sprintf(templ.Url, conf.AppSettings.WebhookServiceUrl, user.Id.Hex()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
//return audit trail for user
|
// return audit trail for user
|
||||||
func (db *DB) FindAuditTrailForUser(userId primitive.ObjectID) ([]models.EventRecieved, []models.ActionTaken, error) {
|
func (db *DB) FindAuditTrailForUser(userId primitive.ObjectID) ([]models.EventRecieved, []models.ActionTaken, error) {
|
||||||
conf := config.Config()
|
conf := config.Config()
|
||||||
|
|
||||||
|
@ -22,7 +22,6 @@ func (db *DB) FindAuditTrailForUser(userId primitive.ObjectID) ([]models.EventRe
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
errs := make([]error, 2)
|
errs := make([]error, 2)
|
||||||
|
|
||||||
|
|
||||||
events := []models.EventRecieved{}
|
events := []models.EventRecieved{}
|
||||||
actions := []models.ActionTaken{}
|
actions := []models.ActionTaken{}
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,7 @@ func (db *DB) SaveModels(m ...Model) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//For allowing more varidaic like things
|
// For allowing more varidaic like things
|
||||||
func saveModels[T Model](db *DB, m ...T) error {
|
func saveModels[T Model](db *DB, m ...T) error {
|
||||||
conf := config.Config()
|
conf := config.Config()
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,8 @@ package models
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
||||||
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
)
|
)
|
||||||
|
|
||||||
const PCO_SUBSCRIPTION_TYPE = "pco_subscription"
|
const PCO_SUBSCRIPTION_TYPE = "pco_subscription"
|
||||||
|
|
12
ui/db/pco.go
12
ui/db/pco.go
|
@ -12,7 +12,7 @@ import (
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
//using userId and event string return PCO Subscriptions saved to the DB
|
// using userId and event string return PCO Subscriptions saved to the DB
|
||||||
func (db *DB) FindPcoSubscriptionForUser(userId primitive.ObjectID, eventName string) (*models.PcoSubscription, error) {
|
func (db *DB) FindPcoSubscriptionForUser(userId primitive.ObjectID, eventName string) (*models.PcoSubscription, error) {
|
||||||
conf := config.Config()
|
conf := config.Config()
|
||||||
|
|
||||||
|
@ -35,14 +35,14 @@ func (db *DB) FindPcoSubscriptionForUser(userId primitive.ObjectID, eventName st
|
||||||
return subscription, nil
|
return subscription, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//Okay so learned something here. Interfaces are determined implemented for the type a method is related to.
|
// Okay so learned something here. Interfaces are determined implemented for the type a method is related to.
|
||||||
//This function is not implemented for DB it is implemented for *DB and that is important
|
// This function is not implemented for DB it is implemented for *DB and that is important
|
||||||
func (db *DB) SaveSubscriptionsForUser(userId primitive.ObjectID, subscriptions ...webhooks.Subscription) (error) {
|
func (db *DB) SaveSubscriptionsForUser(userId primitive.ObjectID, subscriptions ...webhooks.Subscription) error {
|
||||||
mods := make([]*models.PcoSubscription, 0, len(subscriptions))
|
mods := make([]*models.PcoSubscription, 0, len(subscriptions))
|
||||||
for _, sub := range subscriptions {
|
for _, sub := range subscriptions {
|
||||||
mods = append(mods, &models.PcoSubscription{
|
mods = append(mods, &models.PcoSubscription{
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
Details: &sub,
|
Details: &sub,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue