2022-02-11 23:32:53 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
2022-07-22 19:28:14 +00:00
|
|
|
"databag/internal/store"
|
|
|
|
"errors"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
2022-02-11 23:32:53 +00:00
|
|
|
)
|
|
|
|
|
2022-07-29 21:39:39 +00:00
|
|
|
//RemoveCard removes card from account
|
2022-02-11 23:32:53 +00:00
|
|
|
func RemoveCard(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-11 23:32:53 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// scan parameters
|
|
|
|
params := mux.Vars(r)
|
|
|
|
cardID := params["cardID"]
|
2022-02-11 23:32:53 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// load referenced card
|
|
|
|
var slot store.CardSlot
|
2022-11-15 23:27:46 +00:00
|
|
|
if err := store.DB.Preload("Card.Groups").Preload("Card.Members.Channel.Members.Card").Preload("Card.Members.Channel.ChannelSlot").Where("account_id = ? AND card_slot_id = ?", account.ID, cardID).First(&slot).Error; err != nil {
|
2022-07-22 19:28:14 +00:00
|
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
} else {
|
|
|
|
ErrResponse(w, http.StatusNotFound, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if slot.Card == nil {
|
|
|
|
ErrResponse(w, http.StatusNotFound, errors.New("card has been deleted"))
|
|
|
|
return
|
|
|
|
}
|
2022-02-11 23:32:53 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// cards to update
|
|
|
|
cards := make(map[string]*store.Card)
|
2022-02-12 16:28:36 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// save and update contact revision
|
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-11-15 23:27:46 +00:00
|
|
|
for _, member := range slot.Card.Members {
|
|
|
|
if res := tx.Model(&store.Member{}).Where("card_id = ? AND channel_id = ?", slot.Card.ID, member.Channel.ID).Delete(&store.Member{}).Error; res != nil {
|
2022-07-22 19:28:14 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-11-15 23:27:46 +00:00
|
|
|
if res := tx.Model(&store.Channel{}).Where("id = ?", member.Channel.ID).Update("detail_revision", account.ChannelRevision+1).Error; res != nil {
|
2022-10-11 20:56:27 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-11-15 23:27:46 +00:00
|
|
|
if res := tx.Model(&store.ChannelSlot{}).Where("id = ?", member.Channel.ChannelSlot.ID).Update("revision", account.ChannelRevision+1).Error; res != nil {
|
2022-07-22 19:28:14 +00:00
|
|
|
return res
|
|
|
|
}
|
2022-11-15 23:27:46 +00:00
|
|
|
for _, member := range member.Channel.Members {
|
2022-11-11 23:26:43 +00:00
|
|
|
cards[member.Card.GUID] = &member.Card
|
2022-07-22 19:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if res := tx.Model(&slot.Card).Association("Groups").Clear(); res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Delete(&slot.Card).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
slot.Card = nil
|
|
|
|
if res := tx.Model(&slot).Update("card_id", 0).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&slot).Update("revision", account.CardRevision+1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&account).Update("card_revision", account.CardRevision+1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&account).Update("channel_revision", account.ChannelRevision+1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2022-02-11 23:32:53 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
for _, card := range cards {
|
|
|
|
SetContactChannelNotification(account, card)
|
|
|
|
}
|
|
|
|
SetStatus(account)
|
|
|
|
WriteResponse(w, nil)
|
2022-02-11 23:32:53 +00:00
|
|
|
}
|