320 lines
8.5 KiB
Go
320 lines
8.5 KiB
Go
package crm
|
|
|
|
import (
|
|
"jannex/admin-dashboard-backend/modules/crm"
|
|
"jannex/admin-dashboard-backend/modules/database"
|
|
"jannex/admin-dashboard-backend/modules/logger"
|
|
"jannex/admin-dashboard-backend/modules/structs"
|
|
"jannex/admin-dashboard-backend/modules/utils"
|
|
"jannex/admin-dashboard-backend/socketclients"
|
|
"time"
|
|
|
|
"git.ex.umbach.dev/Alex/roese-utils/rslogger"
|
|
"git.ex.umbach.dev/Alex/roese-utils/rspagination"
|
|
"github.com/go-sql-driver/mysql"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// testing
|
|
func GetAllCustomers(c *fiber.Ctx) error {
|
|
var customers []structs.CrmTableCustomer
|
|
|
|
database.DB.Find(&customers)
|
|
|
|
return c.JSON(customers)
|
|
}
|
|
|
|
func GetCrmTypeCustomers(c *fiber.Ctx) error {
|
|
// swagger:operation GET /crm/pipeline/{type}/{dealPhase} crm crmGetCrmCustomers
|
|
// ---
|
|
// summary: Get crm customers
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: page
|
|
// in: query
|
|
// description: Page number
|
|
// responses:
|
|
// '200':
|
|
// description: Crm customers
|
|
// schema:
|
|
// "$ref": "#/definitions/CrmTableCustomerResponse"
|
|
// '400':
|
|
// description: Invalid request query
|
|
// '401':
|
|
// description: No permissions
|
|
// '404':
|
|
// description: Crm type not found
|
|
// '422':
|
|
// description: Deal phase not set
|
|
// '500':
|
|
// description: Failed to get crm customers
|
|
|
|
var params structs.CrmTypeCustomerRequest
|
|
|
|
if err := c.ParamsParser(¶ms); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionCrmCustomersView) {
|
|
return c.SendStatus(fiber.StatusUnauthorized)
|
|
}
|
|
|
|
var query rspagination.PageQuery
|
|
|
|
if err := c.QueryParser(&query); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
if params.Type == utils.CrmPipelineCustomers {
|
|
return c.JSON(structs.CrmTableCustomerResponse{
|
|
Customers: crm.GetTableCustomers(params, query),
|
|
TotalPages: rspagination.GetTotalPages(database.DB,
|
|
utils.CrmCustomersPaginationLimit,
|
|
[]structs.CrmCustomer{},
|
|
nil),
|
|
})
|
|
}
|
|
|
|
// Check if deal phase is set
|
|
if params.DealPhase == 0 {
|
|
return c.SendStatus(fiber.StatusUnprocessableEntity)
|
|
}
|
|
|
|
if params.Type == utils.CrmPipelineDmcPipeline {
|
|
return c.JSON(structs.CrmTableCustomerResponse{
|
|
Customers: crm.GetTableCustomers(params, query),
|
|
TotalPages: rspagination.GetTotalPages(database.DB,
|
|
utils.CrmCustomersPaginationLimit,
|
|
[]structs.CrmCustomer{},
|
|
structs.CrmCustomer{
|
|
Pipeline: utils.CrmPipelineDmcPipelineInt,
|
|
DealPhase: params.DealPhase}),
|
|
})
|
|
}
|
|
|
|
if params.Type == utils.CrmPipelineSetterCloser {
|
|
return c.JSON(structs.CrmTableCustomerResponse{
|
|
Customers: crm.GetTableCustomers(params, query),
|
|
TotalPages: rspagination.GetTotalPages(database.DB,
|
|
utils.CrmCustomersPaginationLimit,
|
|
[]structs.CrmCustomer{},
|
|
structs.CrmCustomer{
|
|
Pipeline: utils.CrmPipelineSetterCloserInt,
|
|
DealPhase: params.DealPhase}),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
func GetCrmCustomer(c *fiber.Ctx) error {
|
|
// swagger:operation GET /crm/customer/view/{id} crm crmGetCrmCustomer
|
|
// ---
|
|
// summary: Get crm customer
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: id
|
|
// in: path
|
|
// description: Customer id
|
|
// responses:
|
|
// '200':
|
|
// description: Crm customer
|
|
// schema:
|
|
// "$ref": "#/definitions/CrmCustomer"
|
|
// '400':
|
|
// description: Invalid request query
|
|
// '401':
|
|
// description: No permissions
|
|
// '404':
|
|
// description: Crm customer not found
|
|
// '500':
|
|
// description: Failed to get crm customer
|
|
|
|
var params structs.CrmGetCustomerRequest
|
|
|
|
if err := c.ParamsParser(¶ms); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionCrmCustomersView) {
|
|
return c.SendStatus(fiber.StatusUnauthorized)
|
|
}
|
|
|
|
var customer structs.CrmCustomer
|
|
|
|
database.DB.First(&customer, "id = ?", params.Id)
|
|
|
|
if customer.Id != params.Id {
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
return c.JSON(customer)
|
|
}
|
|
|
|
func UpdateCrmCustomer(c *fiber.Ctx) error {
|
|
// swagger:operation POST /crm/customer/update/{id} crm crmUpdateCrmCustomer
|
|
// ---
|
|
// summary: Update crm customer
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: id
|
|
// in: path
|
|
// description: Customer id
|
|
// - name: crmCustomer
|
|
// in: body
|
|
// description: Crm customer
|
|
// schema:
|
|
// "$ref": "#/definitions/CrmCustomer"
|
|
// responses:
|
|
// '200':
|
|
// description: Crm customer
|
|
// schema:
|
|
// "$ref": "#/definitions/CrmCustomer"
|
|
// '400':
|
|
// description: Invalid request query
|
|
// '401':
|
|
// description: No permissions
|
|
// '404':
|
|
// description: Crm customer not found
|
|
// '409':
|
|
// description: Crm customer with the company name already exists
|
|
// '500':
|
|
// description: Failed to update crm customer
|
|
|
|
var params structs.CrmGetCustomerRequest
|
|
|
|
if err := c.ParamsParser(¶ms); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionCrmCustomersEdit) {
|
|
return c.SendStatus(fiber.StatusUnauthorized)
|
|
}
|
|
|
|
var customer structs.CrmCustomer
|
|
|
|
database.DB.First(&customer, "id = ?", params.Id)
|
|
|
|
if customer.Id != params.Id {
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
var crmCustomer map[string]interface{}
|
|
|
|
if err := c.BodyParser(&crmCustomer); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
result := database.DB.Model(&structs.CrmCustomer{}).
|
|
Where("id = ?", params.Id).
|
|
Select("*"). // update all fields (even if they are empty)
|
|
Updates(crmCustomer)
|
|
|
|
if err := handleError(result, c); err != nil {
|
|
return err
|
|
}
|
|
|
|
crmCustomer["Id"] = params.Id
|
|
|
|
socketclients.BroadcastMessageToTopicStartsWith(utils.SubscribedTopicCrm, structs.SendSocketMessage{
|
|
Cmd: utils.SentCmdCrmCustomerUpdated,
|
|
Body: crmCustomer,
|
|
})
|
|
|
|
logger.AddCrmLog(rslogger.LogTypeInfo, "Crm customer id: %s updated with following changes: %v", params.Id, customer)
|
|
|
|
return c.JSON(crmCustomer)
|
|
}
|
|
|
|
func CreateCrmCustomer(c *fiber.Ctx) error {
|
|
// swagger:operation POST /crm/customer/create crm crmCreateCrmCustomer
|
|
// ---
|
|
// summary: Create crm customer
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: crmCustomer
|
|
// in: body
|
|
// description: Crm customer
|
|
// schema:
|
|
// "$ref": "#/definitions/CrmCustomer"
|
|
// responses:
|
|
// '200':
|
|
// description: Crm customer
|
|
// schema:
|
|
// "$ref": "#/definitions/CrmCustomer"
|
|
// '400':
|
|
// description: Invalid request query
|
|
// '401':
|
|
// description: No permissions
|
|
// '409':
|
|
// description: Crm customer with the company name already exists
|
|
// '500':
|
|
// description: Failed to create crm customer
|
|
|
|
if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionCrmCustomersCreate) {
|
|
return c.SendStatus(fiber.StatusUnauthorized)
|
|
}
|
|
|
|
var crmCustomer map[string]interface{}
|
|
|
|
if err := c.BodyParser(&crmCustomer); err != nil {
|
|
return c.SendStatus(fiber.StatusBadRequest)
|
|
}
|
|
|
|
crmCustomer["Id"] = uuid.New().String()
|
|
|
|
if crmCustomer["Pipeline"] == nil {
|
|
crmCustomer["Pipeline"] = 1
|
|
}
|
|
|
|
if crmCustomer["DealPhase"] == nil {
|
|
crmCustomer["DealPhase"] = 1
|
|
}
|
|
|
|
crmCustomer["CreatedBy"] = c.Locals("userId").(string)
|
|
crmCustomer["CreatedAt"] = time.Now()
|
|
|
|
result := database.DB.Model(&structs.CrmCustomer{}).Create(&crmCustomer)
|
|
|
|
if err := handleError(result, c); err != nil {
|
|
return err
|
|
}
|
|
|
|
socketclients.BroadcastMessageToTopicStartsWith(utils.SubscribedTopicCrm,
|
|
structs.SendSocketMessage{
|
|
Cmd: utils.SentCmdCrmCustomerCreated,
|
|
Body: crmCustomer,
|
|
})
|
|
|
|
logger.AddCrmLog(rslogger.LogTypeInfo, "Crm customer id: %s created with following changes: %v", crmCustomer["Id"], crmCustomer)
|
|
|
|
return c.JSON(crmCustomer)
|
|
}
|
|
|
|
func handleError(result *gorm.DB, c *fiber.Ctx) error {
|
|
if result.Error != nil {
|
|
if mysqlErr, ok := result.Error.(*mysql.MySQLError); ok {
|
|
switch mysqlErr.Number {
|
|
case 1062:
|
|
logger.AddCrmLog(rslogger.LogTypeError, "Failed to create crm customer as company name already exists: %v", result.Error.Error())
|
|
return c.SendStatus(fiber.StatusConflict)
|
|
|
|
default:
|
|
logger.AddCrmLog(rslogger.LogTypeError, "Failed to create crm customer: %v", result.Error.Error())
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
logger.AddCrmLog(rslogger.LogTypeError, "Failed to create crm customer: %v", result.Error.Error())
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
}
|
|
|
|
return nil
|
|
}
|