2023-11-07 22:34:57 -05:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2023-11-21 20:32:49 -05:00
|
|
|
"errors"
|
2023-11-18 21:15:15 -05:00
|
|
|
"fmt"
|
2023-11-07 22:34:57 -05:00
|
|
|
"strings"
|
|
|
|
|
2023-11-18 21:15:15 -05:00
|
|
|
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config"
|
2023-11-07 22:34:57 -05:00
|
|
|
"git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models"
|
2023-11-18 21:15:15 -05:00
|
|
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco"
|
|
|
|
"git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/webhooks"
|
2023-11-07 22:34:57 -05:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-11-18 21:15:15 -05:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type actionFunc func(user *models.User) error
|
|
|
|
|
|
|
|
var (
|
2023-11-18 21:16:44 -05:00
|
|
|
actionFuncs map[string]actionFunc = map[string]actionFunc{"pco.plan": setupPcoSubscriptions}
|
2023-11-18 21:15:15 -05:00
|
|
|
webhooksTemplate map[string]webhooks.Subscription = map[string]webhooks.Subscription{
|
|
|
|
"services.v2.events.plan.created": {
|
|
|
|
Active: true,
|
|
|
|
Name: "services.v2.events.plan.created",
|
|
|
|
Url: "https://%s/pco/%s",
|
|
|
|
},
|
|
|
|
"services.v2.events.plan.updated": {
|
|
|
|
Active: true,
|
|
|
|
Name: "services.v2.events.plan.updated",
|
|
|
|
Url: "https://%s/pco/%s",
|
|
|
|
},
|
|
|
|
"services.v2.events.plan.deleted": {
|
|
|
|
Active: true,
|
|
|
|
Name: "services.v2.events.plan.deleted",
|
|
|
|
Url: "https://%s/pco/%s",
|
|
|
|
},
|
|
|
|
}
|
2023-11-07 22:34:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func AddActionFromForm(c *gin.Context) {
|
|
|
|
user := getUserFromContext(c)
|
|
|
|
if user == nil {
|
|
|
|
log.Warnf("Could not find user in context. Trying to redner Action form")
|
|
|
|
badRequest(c, "No user available in context")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//parse the form
|
|
|
|
c.Request.ParseForm()
|
|
|
|
var source []string
|
|
|
|
var action []string
|
|
|
|
|
|
|
|
//validate source
|
|
|
|
if str := c.Request.FormValue("source"); str != "" {
|
|
|
|
source = strings.Split(str, ".")
|
|
|
|
} else {
|
|
|
|
log.Warnf("Form request was partially or fully blank")
|
|
|
|
badRequest(c, "Form request was partially or fully blank")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//validate action
|
|
|
|
if str := c.Request.FormValue("action"); str != "" {
|
|
|
|
action = strings.Split(str, ".")
|
|
|
|
} else {
|
|
|
|
log.Warnf("Form request was partially or fully blank")
|
|
|
|
badRequest(c, "Form request was partially or fully blank")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-18 21:15:15 -05:00
|
|
|
//setup action listener
|
|
|
|
if afunc, ok := actionFuncs[strings.Join(source, ".")]; ok {
|
|
|
|
err := afunc(user)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to setup actions")
|
|
|
|
serverError(c, "Failed to setup actions")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-11-07 22:34:57 -05:00
|
|
|
|
2023-11-18 21:15:15 -05:00
|
|
|
//Build mappings
|
2023-11-07 22:34:57 -05:00
|
|
|
am := &models.ActionMapping{
|
2023-11-16 20:45:48 -05:00
|
|
|
UserId: user.Id,
|
|
|
|
SourceEvent: &models.Event{
|
2023-11-07 22:34:57 -05:00
|
|
|
VendorName: source[0],
|
|
|
|
Key: source[1],
|
|
|
|
Fields: map[string]string{},
|
|
|
|
},
|
2023-11-16 20:45:48 -05:00
|
|
|
Action: &models.Action{
|
2023-11-07 22:34:57 -05:00
|
|
|
VendorName: action[0],
|
|
|
|
Type: action[1],
|
|
|
|
Fields: map[string]string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-21 20:32:49 -05:00
|
|
|
err := mongo.SaveModel(am)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to setup actions")
|
|
|
|
serverError(c, "Failed to setup actions")
|
|
|
|
return
|
|
|
|
}
|
2023-11-07 22:34:57 -05:00
|
|
|
|
|
|
|
c.Redirect(302, "/dashboard")
|
|
|
|
}
|
2023-11-18 21:15:15 -05:00
|
|
|
|
|
|
|
func setupPcoSubscriptions(user *models.User) error {
|
|
|
|
// Get PCO vendor account
|
|
|
|
conf := config.Config()
|
|
|
|
pcoAccount, err := mongo.FindVendorAccountByUser(user.Id, models.PCO_VENDOR_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//build pco api
|
|
|
|
tokenSource := oauth2.ReuseTokenSource(pcoAccount.Token(), mongo.NewVendorTokenSource(pcoAccount))
|
|
|
|
pcoApi := pco.NewClientWithOauthConfig(conf.Vendors[models.PCO_VENDOR_NAME].OauthConfig(), tokenSource)
|
|
|
|
|
|
|
|
//Check if subscriptions already exist
|
|
|
|
webhookMap := make(map[string]webhooks.Subscription)
|
|
|
|
subscriptions, err := pcoApi.GetSubscriptions()
|
2023-11-21 20:32:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return errors.Join(fmt.Errorf("Failed to find subscriptions for user: %s", user.Id), err)
|
|
|
|
}
|
2023-11-18 21:15:15 -05:00
|
|
|
//Loop through found subscriptions
|
2023-11-18 21:16:44 -05:00
|
|
|
for _, sub := range subscriptions {
|
2023-11-18 21:15:15 -05:00
|
|
|
//if subsciption is in the templates look to add it to our map
|
|
|
|
if templ, ok := webhooksTemplate[sub.Name]; ok {
|
|
|
|
//if the subscription is for our url add it to our map
|
|
|
|
url := fmt.Sprintf(templ.Url, conf.AppSettings.WebhookServiceUrl, user.Id.Hex())
|
|
|
|
if url == sub.Url {
|
|
|
|
webhookMap[sub.Name] = sub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
builtHooks := make([]webhooks.Subscription, 0, len(webhooksTemplate))
|
|
|
|
//Build subscriptions
|
|
|
|
for _, templ := range webhooksTemplate {
|
|
|
|
if _, ok := webhookMap[templ.Name]; !ok {
|
|
|
|
builtHooks = append(builtHooks, webhooks.Subscription{
|
2023-11-18 21:16:44 -05:00
|
|
|
Active: false,
|
|
|
|
Name: templ.Name,
|
|
|
|
Url: fmt.Sprintf(templ.Url, conf.AppSettings.WebhookServiceUrl, user.Id.Hex()),
|
2023-11-18 21:15:15 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Post Subscriptions
|
|
|
|
subscriptions, err = pcoApi.CreateSubscriptions(builtHooks)
|
|
|
|
if err != nil {
|
2023-11-21 20:32:49 -05:00
|
|
|
return errors.Join(fmt.Errorf("Failed to create subscriptions for user: %s", user.Id), err)
|
2023-11-18 21:15:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//Save Subscriptions
|
|
|
|
err = mongo.SaveSubscriptionsForUser(user.Id, subscriptions...)
|
|
|
|
if err != nil {
|
2023-11-21 20:32:49 -05:00
|
|
|
return errors.Join(fmt.Errorf("Failed to save subscriptions for user: %s", user.Id), err)
|
2023-11-18 21:15:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|