create crm

main
alex 2023-12-07 22:58:44 +01:00
parent e789bfbb4e
commit 359736f693
4 changed files with 63 additions and 5 deletions

View File

@ -54,7 +54,6 @@ type CrmTableCustomer struct {
Email string
LastContact time.Time
CreatedBy string
Notes string
}
func (CrmTableCustomer) TableName() string {

View File

@ -101,6 +101,7 @@ const (
SentCmdAdminAreaManageLogManagerServerConnectionAdded = 45
SentCmdAdminAreaManageLogManagerServerConnectionRemoved = 46
SentCmdCrmCustomerUpdated = 47
SentCmdCrmCustomerCreated = 48
)
// commands received from web clients
@ -218,6 +219,7 @@ const (
_crm = "crm."
PermissionCrmCustomersView = _crm + "customers.view"
PermissionCrmCustomersEdit = _crm + "customers.edit"
PermissionCrmCustomersCreate = _crm + "customers.create"
PermissionCrmDmcPipelineView = _crm + "dmc_pipeline.view"
PermissionCrmSetterCloserView = _crm + "setter_closer.view"
)
@ -253,6 +255,7 @@ var SystemPermissions = []string{
PermissionsRoboticsRobotsViewSwaggerDocumentation,
PermissionCrmCustomersView,
PermissionCrmCustomersEdit,
PermissionCrmCustomersCreate,
PermissionCrmDmcPipelineView,
PermissionCrmSetterCloserView,
}

View File

@ -9,7 +9,7 @@ import (
"git.ex.umbach.dev/Alex/roese-utils/rspagination"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
"github.com/google/uuid"
)
func GetCrmTypeCustomers(c *fiber.Ctx) error {
@ -188,19 +188,19 @@ func UpdateCrmCustomer(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusNotFound)
}
var crmCustomer structs.CrmCustomer
var crmCustomer map[string]interface{}
if err := c.BodyParser(&crmCustomer); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
log.Info().Msgf("%+v %v", crmCustomer, crmCustomer.Telephone)
database.DB.Model(&structs.CrmCustomer{}).
Where("id = ?", params.Id).
Select("*"). // update all fields (even if they are empty)
Updates(crmCustomer)
crmCustomer["Id"] = params.Id
socketclients.BroadcastMessageToTopicStartsWith(utils.SubscribedTopicCrm, structs.SendSocketMessage{
Cmd: utils.SentCmdCrmCustomerUpdated,
Body: crmCustomer,
@ -208,3 +208,58 @@ func UpdateCrmCustomer(c *fiber.Ctx) error {
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
// '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
}
database.DB.Model(&structs.CrmCustomer{}).Create(&crmCustomer)
socketclients.BroadcastMessageToTopicStartsWith(utils.SubscribedTopicCrm,
structs.SendSocketMessage{
Cmd: utils.SentCmdCrmCustomerCreated,
Body: crmCustomer,
})
return c.JSON(crmCustomer)
}

View File

@ -71,6 +71,7 @@ func SetupRoutes(app *fiber.App) {
c.Get("/pipeline/:type/:dealPhase", requestAccessValidation, crm.GetCrmTypeCustomers)
c.Get("/customer/view/:id", requestAccessValidation, crm.GetCrmCustomer)
c.Post("/customer/update/:id", requestAccessValidation, crm.UpdateCrmCustomer)
c.Post("/customer/create", requestAccessValidation, crm.CreateCrmCustomer)
app.Static("/", config.Cfg.FolderPaths.PublicStatic)
}