diff --git a/controllers/authController.go b/controllers/authController.go index 4b2cf60..bcd3698 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -4,7 +4,6 @@ import ( "api/database" "api/models" "api/utils" - "fmt" "log" "os" "strconv" @@ -130,7 +129,6 @@ func AddUser(c *fiber.Ctx) error { UserType: data["usertype"], Blocked: "N", Cancelled: "N", - CreatedBy: data["createdby"], } database.DB.Create(&user) @@ -183,20 +181,22 @@ func GetAllUsers(c *fiber.Ctx) error { return c.JSON(users) } -func WixIntegration(c *fiber.Ctx) error { - var data models.Purchase +func GetUserByEmail(c *fiber.Ctx) error { - if err := c.BodyParser(&data); err != nil { - return fiber.ErrBadRequest + var user models.User + + _, err := utils.ProcessToken(c) + if err != nil { + return fiber.ErrUnauthorized } - fmt.Println(data.Data.Contact.Email) - fmt.Println(data.Data.Contact.Name.First + " " + data.Data.Contact.Name.Last) - fmt.Printf(data.Data.PlanOrderID) - fmt.Println(data.Data.PlanPrice.Value) - fmt.Println(data.Data.PlanStartDate) - fmt.Println(data.Data.PlanValidUntil) - fmt.Println(data.Data.PlanTitle) + email := c.Params("email") - return c.JSON(data) + database.DB.Where("email = ?", email).First(&user) + + if user.ID == 0 { + return fiber.ErrNotFound + } + + return c.JSON(user) } diff --git a/controllers/webhookController.go b/controllers/webhookController.go index c5c0c37..4df976f 100644 --- a/controllers/webhookController.go +++ b/controllers/webhookController.go @@ -1,9 +1,12 @@ package controllers import ( + "api/database" "api/models" "api/utils" + "fmt" "log" + "strconv" "github.com/gofiber/fiber/v2" ) @@ -43,7 +46,6 @@ func OnUpdate(c *fiber.Ctx) error { return c.SendString("On_Update: " + string(c.Body())) } - func OnSubStart(c *fiber.Ctx) error { p := new(models.Update) if err := c.BodyParser(p); err != nil { @@ -86,3 +88,47 @@ func OnPubStop(c *fiber.Ctx) error { log.Printf("Stop %s\n", p.Channel) return c.SendString("On_Pub_Stop: " + string(c.Body())) } + +func WixIntegration(c *fiber.Ctx) error { + var data models.Purchase + + if err := c.BodyParser(&data); err != nil { + return fiber.ErrBadRequest + } + + fmt.Println(data.Data.Contact.Email) + fmt.Println(data.Data.Contact.Name.First + " " + data.Data.Contact.Name.Last) + fmt.Printf(data.Data.PlanOrderID) + fmt.Println(data.Data.PlanPrice.Value) + fmt.Println(data.Data.PlanStartDate) + fmt.Println(data.Data.PlanValidUntil) + fmt.Println(data.Data.PlanTitle) + + result := database.DB.Where("email = ?", data.Data.Contact.Email).First(&data) + + if result.RowsAffected == 0 { + // User does not exist. Inserts it. + user := models.User{ + Email: data.Data.Contact.Email, + Name: data.Data.Contact.Name.First + " " + data.Data.Contact.Name.Last, + Blocked: "N", + Cancelled: "N", + } + + database.DB.Create(&user) + } + + value, _ := strconv.Atoi(data.Data.PlanPrice.Value) + transaction := models.Transaction{ + Email: data.Data.Contact.Email, + PlanOrderID: data.Data.PlanOrderID, + Value: value, + PlanStartDate: data.Data.PlanStartDate, + PlanValidUntil: data.Data.PlanValidUntil, + PlanTitle: data.Data.PlanTitle, + } + + database.DB.Create(&transaction) + + return c.JSON(data) +} diff --git a/database/database.go b/database/database.go index 3389760..c90d303 100644 --- a/database/database.go +++ b/database/database.go @@ -62,6 +62,10 @@ func ConnectDB() error { return result } + if result := db.AutoMigrate(&models.Transaction{}); result != nil { + return result + } + // Count how many servers we have on the database. If none, creates our initial server. var servers []models.Server @@ -96,7 +100,6 @@ func ConnectDB() error { UserType: "A", Blocked: "N", Cancelled: "N", - CreatedBy: "Auto", ServerId: 1, } diff --git a/models/transactions.go b/models/transactions.go new file mode 100644 index 0000000..32705c6 --- /dev/null +++ b/models/transactions.go @@ -0,0 +1,15 @@ +package models + +import ( + "gorm.io/gorm" +) + +type Transaction struct { + gorm.Model + Email string `gorm:"size:40" json:"email"` + PlanOrderID string `gorm:"size:25" json:"planorderid"` + Value int `gorm:"size:10" json:"value"` + PlanStartDate string `gorm:"size:10" json:"planstartdate"` + PlanValidUntil string `gorm:"size:10" json:"planvaliduntil"` + PlanTitle string `gorm:"size:40" json:"plantitle"` +} diff --git a/models/users.go b/models/users.go index fe05de8..a1879f3 100644 --- a/models/users.go +++ b/models/users.go @@ -6,30 +6,15 @@ import ( type User struct { gorm.Model - Name string `gorm:"size:40;not null" json:"name"` - CompanyName string `gorm:"size:40;not null" json:"companyname"` - Email string `gorm:"size:40;not null;unique" json:"email"` - Password []byte `gorm:"size:100;not null;" json:"-"` - Channel string `gorm:"size:40;not null" json:"channel"` - Url string `gorm:"size:40;not null" json:"url"` - CpfCnpj string `gorm:"size:20;not null" json:"cpfcnpj"` - UserType string `gorm:"size:1;not null;default:U" json:"usertype"` - Plan string `gorm:"size:1;not null;default:A" json:"plan"` - Blocked string `gorm:"size:1;not null;default:N" json:"blocked"` - Cancelled string `gorm:"size:1;not null;default:N" json:"cancelled"` - CreatedBy string `gorm:"size:15;not null;default:Manual" json:"createdby"` - ServerId uint `gorm:"not null" json:"serverid"` + Name string `gorm:"size:40" json:"name"` + CompanyName string `gorm:"size:40" json:"companyname"` + Email string `gorm:"size:40;unique" json:"email"` + Password []byte `gorm:"size:100;" json:"-"` + Channel string `gorm:"size:40" json:"channel"` + Url string `gorm:"size:40" json:"url"` + CpfCnpj string `gorm:"size:20" json:"cpfcnpj"` + UserType string `gorm:"size:1;default:U" json:"usertype"` + Blocked string `gorm:"size:1;default:N" json:"blocked"` + Cancelled string `gorm:"size:1;default:N" json:"cancelled"` + ServerId uint `gorm:"not null;default:1" json:"serverid"` } - -// Plan: - -// A - Admin -// T - Trial -// FM - Fácil Mensal -// FT - Fácil Trimestral -// FS - Fácil Semestral -// FA - Fácil Anual -// FM - Fácil Plus Mensal -// FT - Fácil Plus Trimestral -// FS - Fácil Plus Semestral -// FA - Fácil Plus Anual diff --git a/routes/routes.go b/routes/routes.go index 29da421..c1908c1 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -39,6 +39,7 @@ func Setup(app *fiber.App) { protected.Get("user", controllers.GetOwnUser) protected.Get("users", controllers.GetAllUsers) + protected.Get("users/:email", controllers.GetUserByEmail) protected.Post("user", controllers.AddUser) protected.Post("event", controllers.AddEvent)