llink share

main
alex 2024-03-16 17:08:02 +01:00
parent f5513a086a
commit 1e6b42579e
7 changed files with 292 additions and 0 deletions

BIN
main

Binary file not shown.

View File

@ -47,4 +47,6 @@ func InitDatabase() {
db.AutoMigrate(&structs.LogManagerServerConnection{})
db.AutoMigrate(&structs.CrmCustomer{})
db.AutoMigrate(&structs.CrmCallProtocol{})
db.AutoMigrate(&structs.CrmLink{})
db.AutoMigrate(&structs.CrmLinkHistory{})
}

View File

@ -108,4 +108,23 @@ type CrmCallProtocolRequest struct {
type CrmGetCustomerResponse struct {
Customer CrmCustomer
CallProtocols []CrmCallProtocol
Links []CrmLink
LinkHistory []CrmLinkHistory
}
// swagger:model CrmLink
type CrmLink struct {
Id string
CustomerId string
Name string
Url string
CreatedBy string
CreatedAt time.Time
}
// swagger:model CrmLinkHistory
type CrmLinkHistory struct {
LinkId string
UserAgent string
UsedAt time.Time
}

View File

@ -104,6 +104,9 @@ const (
SentCmdCrmCustomerCreated = 48
SentCmdCrmCallProtocolCreated = 49
SentCmdCrmCallProtocolDeleted = 50
SentCmdCrmLinkCreated = 51
SentCmdCrmLinkUsed = 52
SentCmdCrmLinkDeleted = 53
)
// commands received from web clients
@ -227,6 +230,9 @@ const (
PermissionCrmCallProtocolCreate = _crm + "call_protocol.create"
PermissionCrmCallProtocolView = _crm + "call_protocol.view"
PermissionCrmCallProtocolDelete = _crm + "call_protocol.delete"
PermissionCrmLinkCreate = _crm + "link.create"
PermissionCrmLinkView = _crm + "link.view"
PermissionCrmLinkDelete = _crm + "link.delete"
)
var SystemPermissions = []string{
@ -266,6 +272,9 @@ var SystemPermissions = []string{
PermissionCrmCallProtocolCreate,
PermissionCrmCallProtocolView,
PermissionCrmCallProtocolDelete,
PermissionCrmLinkCreate,
PermissionCrmLinkView,
PermissionCrmLinkDelete,
}
var DynamicGroupTasksPermissions = []string{

View File

@ -1387,6 +1387,10 @@
"Turnover": {
"type": "string"
},
"UpdatedAt": {
"type": "string",
"format": "date-time"
},
"Website": {
"type": "string"
},
@ -1453,6 +1457,10 @@
},
"Telephone": {
"type": "string"
},
"UpdatedAt": {
"type": "string",
"format": "date-time"
}
},
"x-go-package": "jannex/admin-dashboard-backend/modules/structs"

View File

@ -162,6 +162,20 @@ func GetCrmCustomer(c *fiber.Ctx) error {
database.DB.Find(&customerResponse.CallProtocols, "customer_id = ?", params.Id)
// get links
database.DB.Find(&customerResponse.Links, "customer_id = ?", params.Id)
// get links history by looping through links
for _, link := range customerResponse.Links {
var linkHistory []structs.CrmLinkHistory
database.DB.Find(&linkHistory, "link_id = ?", link.Id)
customerResponse.LinkHistory = append(customerResponse.LinkHistory, linkHistory...)
}
return c.JSON(customerResponse)
}
@ -550,3 +564,239 @@ func DeleteCrmCallProtocol(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
func CreateCrmLink(c *fiber.Ctx) error {
// swagger:operation POST /crm/links/create crm crmCreateCrmLink
// ---
// summary: Create crm link
// produces:
// - application/json
// parameters:
// - name: crmLink
// in: body
// description: Crm link
// schema:
// "$ref": "#/definitions/CrmLink"
// responses:
// '200':
// description: Crm link created
// '400':
// description: Invalid request query
// '401':
// description: No permissions
// '500':
// description: Failed to create crm link
if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionCrmLinkCreate) {
return c.SendStatus(fiber.StatusUnauthorized)
}
var body structs.CrmLink
if err := c.BodyParser(&body); err != nil ||
body.CustomerId == "" ||
body.Name == "" ||
body.Url == "" {
return c.SendStatus(fiber.StatusBadRequest)
}
// check if customer exists
var count int64
database.DB.Model(&structs.CrmCustomer{}).Where("id = ?", body.CustomerId).Count(&count)
if count == 0 {
logger.AddCrmLog(rslogger.LogTypeError, "Failed to create crm link as customer id not found: %v", body.CustomerId)
return c.SendStatus(fiber.StatusNotFound)
}
body.Id = uuid.New().String()
body.CreatedBy = c.Locals("userId").(string)
body.CreatedAt = time.Now()
result := database.DB.Model(&structs.CrmLink{}).Create(&body)
if result.Error != nil {
logger.AddCrmLog(rslogger.LogTypeError, "Failed to create crm link: %v", result.Error.Error())
return c.SendStatus(fiber.StatusInternalServerError)
}
socketclients.BroadcastMessageToTopicStartsWith(utils.SubscribedTopicCrm,
structs.SendSocketMessage{
Cmd: utils.SentCmdCrmLinkCreated,
Body: body,
})
logger.AddCrmLog(rslogger.LogTypeInfo, "Crm link id: %s created: %v", body.Id, body)
return c.SendStatus(fiber.StatusOK)
}
func GetCrmLinks(c *fiber.Ctx) error {
// swagger:operation GET /crm/links/{customerId} crm crmGetCrmLinks
// ---
// summary: Get crm links
// produces:
// - application/json
// parameters:
// - name: customerId
// in: path
// description: Customer id
// responses:
// '200':
// description: Crm links
// schema:
// "$ref": "#/definitions/CrmLink"
// '400':
// description: Invalid request query
// '401':
// description: No permissions
// '500':
// description: Failed to get crm links
if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionCrmLinkView) {
return c.SendStatus(fiber.StatusUnauthorized)
}
var params structs.CrmGetCustomerRequest
if err := c.ParamsParser(&params); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
var crmLinks []structs.CrmLink
database.DB.Find(&crmLinks, "customer_id = ?", params.Id)
return c.JSON(crmLinks)
}
func CrmUseLink(c *fiber.Ctx) error {
// swagger:operation POST /crm/link/{id} crm crmUseLink
// ---
// summary: Use crm link
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: Link id
// responses:
// '200':
// description: Crm link used
// '400':
// description: Invalid request query
// '401':
// description: No permissions
// '404':
// description: Crm link not found
// '500':
// description: Failed to use crm link
var params structs.CrmGetCustomerRequest
if err := c.ParamsParser(&params); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
var link structs.CrmLink
database.DB.First(&link, "id = ?", params.Id)
if link.Id != params.Id {
return c.SendStatus(fiber.StatusNotFound)
}
// create link history
linkHistory := structs.CrmLinkHistory{
LinkId: link.Id,
UserAgent: string(c.Context().UserAgent()),
UsedAt: time.Now(),
}
result := database.DB.Model(&structs.CrmLinkHistory{}).Create(&linkHistory)
if result.Error != nil {
logger.AddCrmLog(rslogger.LogTypeError, "Failed to use crm link: %v", result.Error.Error())
return c.SendStatus(fiber.StatusInternalServerError)
}
socketclients.BroadcastMessageToTopicStartsWith(utils.SubscribedTopicCrm,
structs.SendSocketMessage{
Cmd: utils.SentCmdCrmLinkUsed,
Body: linkHistory,
})
logger.AddCrmLog(rslogger.LogTypeInfo, "Crm link id: %s used: %v", link.Id, link)
return c.Redirect(link.Url)
}
func DeleteCrmLink(c *fiber.Ctx) error {
// swagger:operation DELETE /crm/links/{id} crm crmDeleteCrmLink
// ---
// summary: Delete crm link
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: Link id
// responses:
// '200':
// description: Crm link deleted
// '400':
// description: Invalid request query
// '401':
// description: No permissions
// '404':
// description: Crm link not found
// '500':
// description: Failed to delete crm link
if !socketclients.HasPermission(c.Locals("userId").(string), utils.PermissionCrmLinkDelete) {
return c.SendStatus(fiber.StatusUnauthorized)
}
var params structs.CrmGetCustomerRequest
if err := c.ParamsParser(&params); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
var link structs.CrmLink
database.DB.First(&link, "id = ?", params.Id)
if link.Id != params.Id {
return c.SendStatus(fiber.StatusNotFound)
}
result := database.DB.Delete(&link, "id = ?", params.Id)
if result.Error != nil {
logger.AddCrmLog(rslogger.LogTypeError, "Failed to delete crm link: %v", result.Error.Error())
return c.SendStatus(fiber.StatusInternalServerError)
}
// delete all link history
result = database.DB.Delete(&structs.CrmLinkHistory{}, "link_id = ?", params.Id)
if result.Error != nil {
logger.AddCrmLog(rslogger.LogTypeError, "Failed to delete crm link history: %v", result.Error.Error())
return c.SendStatus(fiber.StatusInternalServerError)
}
socketclients.BroadcastMessageToTopicStartsWith(utils.SubscribedTopicCrm,
structs.SendSocketMessage{
Cmd: utils.SentCmdCrmLinkDeleted,
Body: params.Id,
})
logger.AddCrmLog(rslogger.LogTypeInfo, "Crm link id: %s deleted: %v", link.Id, link)
return c.SendStatus(fiber.StatusOK)
}

View File

@ -76,6 +76,10 @@ func SetupRoutes(app *fiber.App) {
c.Get("/customers", requestAccessValidation, crm.GetAllCustomers)
c.Post("/calls/create", requestAccessValidation, crm.CreateCrmCallProtocol)
c.Delete("/calls/delete/:id", requestAccessValidation, crm.DeleteCrmCallProtocol)
c.Post("/links", requestAccessValidation, crm.CreateCrmLink)
c.Get("/links/:customerId", requestAccessValidation, crm.GetCrmLinks)
c.Get("/link/:id", crm.CrmUseLink)
c.Delete("/links/:id", requestAccessValidation, crm.DeleteCrmLink)
app.Static("/", config.Cfg.FolderPaths.PublicStatic)
}