2024-03-29 09:55:51 +01:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"basic-sensor-receiver/integrations"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ContactPointsService struct {
|
|
|
|
|
ctx *Context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ContactPointItem struct {
|
|
|
|
|
Id int64 `json:"id"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
TypeConfig string `json:"typeConfig"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ContactPointsService) GetList() ([]ContactPointItem, error) {
|
|
|
|
|
contactPoints := make([]ContactPointItem, 0)
|
|
|
|
|
|
|
|
|
|
rows, err := s.ctx.DB.Query("SELECT id, name, type, type_config FROM contact_points")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return contactPoints, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
item := ContactPointItem{}
|
|
|
|
|
|
|
|
|
|
err := rows.Scan(&item.Id, &item.Name, &item.Type, &item.TypeConfig)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contactPoints = append(contactPoints, item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return contactPoints, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ContactPointsService) Create(name string, contactType string, contactTypeConfig string) (*ContactPointItem, error) {
|
|
|
|
|
switch contactType {
|
|
|
|
|
case "telegram":
|
|
|
|
|
{
|
|
|
|
|
err := s.ctx.Integrations.Telegram.ValidateConfig(contactTypeConfig)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to validate telegram config - %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item := ContactPointItem{
|
|
|
|
|
Name: name,
|
|
|
|
|
Type: contactType,
|
|
|
|
|
TypeConfig: contactTypeConfig,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := s.ctx.DB.Exec("INSERT INTO contact_points (name, type, type_config) VALUES (?, ?, ?)", item.Name, item.Type, item.TypeConfig)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id, err := res.LastInsertId()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.Id = id
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ContactPointsService) GetById(id int64) (*ContactPointItem, error) {
|
|
|
|
|
item := ContactPointItem{}
|
|
|
|
|
|
|
|
|
|
row := s.ctx.DB.QueryRow("SELECT id, name, type, type_config FROM contact_points WHERE id = ?", id)
|
|
|
|
|
err := row.Scan(&item.Id, &item.Name, &item.Type, &item.TypeConfig)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ContactPointsService) Update(id int64, name string, contactType string, contactTypeConfig string) (*ContactPointItem, error) {
|
|
|
|
|
item := ContactPointItem{
|
|
|
|
|
Id: id,
|
|
|
|
|
Name: name,
|
|
|
|
|
Type: contactType,
|
|
|
|
|
TypeConfig: contactTypeConfig,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := s.ctx.DB.Exec("UPDATE contact_points SET name = ?, type = ?, type_config = ? WHERE id = ?", item.Name, item.Type, item.TypeConfig, item.Id)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &item, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *ContactPointsService) Delete(id int64) error {
|
|
|
|
|
_, err := s.ctx.DB.Exec("DELETE FROM contact_points WHERE id = ?", id)
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-31 09:50:09 +02:00
|
|
|
func (s *ContactPointsService) Test(contactPointType string, typeConfig string) error {
|
|
|
|
|
item := ContactPointItem{
|
|
|
|
|
Type: contactPointType,
|
|
|
|
|
TypeConfig: typeConfig,
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 09:55:51 +01:00
|
|
|
service, err := item.getTestService(s.ctx)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return service.ProcessEvent(&integrations.ContactPointEvent{
|
|
|
|
|
Type: integrations.ContactPointEventTest,
|
|
|
|
|
}, item.TypeConfig)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (channel *ContactPointItem) getService(ctx *Context) (integrations.ContactPointIntegration, error) {
|
|
|
|
|
switch channel.Type {
|
|
|
|
|
case "telegram":
|
|
|
|
|
return ctx.Integrations.Telegram, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, errors.New("unknown channel type")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (channel *ContactPointItem) getTestService(ctx *Context) (integrations.ContactPointIntegration, error) {
|
|
|
|
|
switch channel.Type {
|
|
|
|
|
case "telegram":
|
|
|
|
|
return ctx.Integrations.Telegram, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, errors.New("unknown channel type")
|
|
|
|
|
}
|