2022-02-15 22:30:04 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-07-22 19:28:14 +00:00
|
|
|
"databag/internal/store"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
2022-02-15 22:30:04 +00:00
|
|
|
)
|
|
|
|
|
2022-07-25 18:44:33 +00:00
|
|
|
//AddChannel adds a channel to account specified by qgent query param
|
2022-02-15 22:30:04 +00:00
|
|
|
func AddChannel(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
account, code, err := ParamAgentToken(r, false)
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
|
|
|
return
|
|
|
|
}
|
2022-02-15 22:30:04 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
var params ChannelParams
|
|
|
|
if err := ParseRequest(r, w, ¶ms); err != nil {
|
|
|
|
ErrResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
2022-02-15 22:30:04 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
cards := []*store.Card{}
|
|
|
|
slot := &store.ChannelSlot{}
|
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-02-15 22:30:04 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
channel := &store.Channel{}
|
|
|
|
channel.AccountID = account.ID
|
|
|
|
channel.Data = params.Data
|
|
|
|
channel.DataType = params.DataType
|
|
|
|
channel.DetailRevision = account.ChannelRevision + 1
|
|
|
|
channel.TopicRevision = account.ChannelRevision + 1
|
|
|
|
if res := tx.Save(channel).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
2022-02-15 22:30:04 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
slot.ChannelSlotID = uuid.New().String()
|
|
|
|
slot.AccountID = account.ID
|
|
|
|
slot.ChannelID = channel.ID
|
|
|
|
slot.Revision = account.ChannelRevision + 1
|
|
|
|
slot.Channel = channel
|
|
|
|
if res := tx.Save(slot).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
for _, cardID := range params.Cards {
|
|
|
|
cardSlot := store.CardSlot{}
|
|
|
|
if res := tx.Preload("Card").Where("account_id = ? AND card_slot_id = ?", account.ID, cardID).First(&cardSlot).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&slot.Channel).Association("Cards").Append(cardSlot.Card); res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
cards = append(cards, cardSlot.Card)
|
|
|
|
}
|
2022-04-06 21:02:18 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
for _, groupID := range params.Groups {
|
|
|
|
groupSlot := store.GroupSlot{}
|
|
|
|
if res := tx.Preload("Group").Where("account_id = ? AND group_slot_id = ?", account.ID, groupID).First(&groupSlot).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&slot.Channel).Association("Groups").Append(groupSlot.Group); res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 21:02:18 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
if res := tx.Model(&account).Update("channel_revision", account.ChannelRevision+1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
2022-04-06 21:02:18 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2022-02-15 22:30:04 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
SetStatus(account)
|
|
|
|
for _, card := range cards {
|
|
|
|
SetContactChannelNotification(account, card)
|
|
|
|
}
|
|
|
|
WriteResponse(w, getChannelModel(slot, true, true))
|
2022-02-15 22:30:04 +00:00
|
|
|
}
|