feature: added transcations table and process confirmed payments

main
Nilo Roberto C Paim 2023-10-21 08:36:57 -03:00
parent f862ac83c9
commit ddac58a6d3
6 changed files with 92 additions and 42 deletions

View File

@ -4,7 +4,6 @@ import (
"api/database" "api/database"
"api/models" "api/models"
"api/utils" "api/utils"
"fmt"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -130,7 +129,6 @@ func AddUser(c *fiber.Ctx) error {
UserType: data["usertype"], UserType: data["usertype"],
Blocked: "N", Blocked: "N",
Cancelled: "N", Cancelled: "N",
CreatedBy: data["createdby"],
} }
database.DB.Create(&user) database.DB.Create(&user)
@ -183,20 +181,22 @@ func GetAllUsers(c *fiber.Ctx) error {
return c.JSON(users) return c.JSON(users)
} }
func WixIntegration(c *fiber.Ctx) error { func GetUserByEmail(c *fiber.Ctx) error {
var data models.Purchase
if err := c.BodyParser(&data); err != nil { var user models.User
return fiber.ErrBadRequest
_, err := utils.ProcessToken(c)
if err != nil {
return fiber.ErrUnauthorized
} }
fmt.Println(data.Data.Contact.Email) email := c.Params("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)
return c.JSON(data) database.DB.Where("email = ?", email).First(&user)
if user.ID == 0 {
return fiber.ErrNotFound
}
return c.JSON(user)
} }

View File

@ -1,9 +1,12 @@
package controllers package controllers
import ( import (
"api/database"
"api/models" "api/models"
"api/utils" "api/utils"
"fmt"
"log" "log"
"strconv"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -43,7 +46,6 @@ func OnUpdate(c *fiber.Ctx) error {
return c.SendString("On_Update: " + string(c.Body())) return c.SendString("On_Update: " + string(c.Body()))
} }
func OnSubStart(c *fiber.Ctx) error { func OnSubStart(c *fiber.Ctx) error {
p := new(models.Update) p := new(models.Update)
if err := c.BodyParser(p); err != nil { if err := c.BodyParser(p); err != nil {
@ -86,3 +88,47 @@ func OnPubStop(c *fiber.Ctx) error {
log.Printf("Stop %s\n", p.Channel) log.Printf("Stop %s\n", p.Channel)
return c.SendString("On_Pub_Stop: " + string(c.Body())) 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)
}

View File

@ -62,6 +62,10 @@ func ConnectDB() error {
return result 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. // Count how many servers we have on the database. If none, creates our initial server.
var servers []models.Server var servers []models.Server
@ -96,7 +100,6 @@ func ConnectDB() error {
UserType: "A", UserType: "A",
Blocked: "N", Blocked: "N",
Cancelled: "N", Cancelled: "N",
CreatedBy: "Auto",
ServerId: 1, ServerId: 1,
} }

15
models/transactions.go Normal file
View File

@ -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"`
}

View File

@ -6,30 +6,15 @@ import (
type User struct { type User struct {
gorm.Model gorm.Model
Name string `gorm:"size:40;not null" json:"name"` Name string `gorm:"size:40" json:"name"`
CompanyName string `gorm:"size:40;not null" json:"companyname"` CompanyName string `gorm:"size:40" json:"companyname"`
Email string `gorm:"size:40;not null;unique" json:"email"` Email string `gorm:"size:40;unique" json:"email"`
Password []byte `gorm:"size:100;not null;" json:"-"` Password []byte `gorm:"size:100;" json:"-"`
Channel string `gorm:"size:40;not null" json:"channel"` Channel string `gorm:"size:40" json:"channel"`
Url string `gorm:"size:40;not null" json:"url"` Url string `gorm:"size:40" json:"url"`
CpfCnpj string `gorm:"size:20;not null" json:"cpfcnpj"` CpfCnpj string `gorm:"size:20" json:"cpfcnpj"`
UserType string `gorm:"size:1;not null;default:U" json:"usertype"` UserType string `gorm:"size:1;default:U" json:"usertype"`
Plan string `gorm:"size:1;not null;default:A" json:"plan"` Blocked string `gorm:"size:1;default:N" json:"blocked"`
Blocked string `gorm:"size:1;not null;default:N" json:"blocked"` Cancelled string `gorm:"size:1;default:N" json:"cancelled"`
Cancelled string `gorm:"size:1;not null;default:N" json:"cancelled"` ServerId uint `gorm:"not null;default:1" json:"serverid"`
CreatedBy string `gorm:"size:15;not null;default:Manual" json:"createdby"`
ServerId uint `gorm:"not null" 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

View File

@ -39,6 +39,7 @@ func Setup(app *fiber.App) {
protected.Get("user", controllers.GetOwnUser) protected.Get("user", controllers.GetOwnUser)
protected.Get("users", controllers.GetAllUsers) protected.Get("users", controllers.GetAllUsers)
protected.Get("users/:email", controllers.GetUserByEmail)
protected.Post("user", controllers.AddUser) protected.Post("user", controllers.AddUser)
protected.Post("event", controllers.AddEvent) protected.Post("event", controllers.AddEvent)