fixing notification status endpoints

This commit is contained in:
Roland Osborne 2022-11-14 13:29:27 -08:00
parent c8f8ef7a97
commit 9959541094
3 changed files with 58 additions and 15 deletions

View File

@ -15,13 +15,6 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
channelID := params["channelID"]
// get enabled state
var flag bool
if err := ParseRequest(r, w, &flag); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
tokenType := ParamTokenType(r)
if tokenType == APPTokenAgent {
account, code, err := ParamAgentToken(r, false)
@ -31,8 +24,8 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
}
// get channel entry
channel := store.Channel{}
if store.DB.Model(&channel).Where("channel_id = ? AND account_id = ?", channelID, account.ID).First(&channel).Error != nil {
slot := store.ChannelSlot{}
if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, account.ID).First(&slot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
@ -40,9 +33,13 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
}
return
}
if (slot.Channel == nil) {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel"));
return;
}
// retrun notification status
WriteResponse(w, channel.HostPush)
// return notification status
WriteResponse(w, slot.Channel.HostPush)
} else if tokenType == APPTokenContact {
card, code, err := ParamContactToken(r, true)
if err != nil {
@ -50,9 +47,24 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
return
}
// get channel entry
slot := store.ChannelSlot{}
if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, card.Account.ID).First(&slot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if (slot.Channel == nil) {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel"));
return;
}
// get member entry
member := store.Member{}
if store.DB.Model(&member).Where("channel_id ? AND card_id = ?", channelID, card.ID).First(&member).Error != nil {
if err := store.DB.Model(&member).Where("channel_id = ? AND card_id = ?", slot.Channel.ID, card.ID).First(&member).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {

View File

@ -24,7 +24,7 @@ func SetChannelCard(w http.ResponseWriter, r *http.Request) {
// load referenced channel
var channelSlot store.ChannelSlot
if err := store.DB.Preload("Channel.Cards.CardSlot").Preload("Channel.Groups.GroupSlot").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_slot_id = ?", account.ID, channelID).First(&channelSlot).Error; err != nil {
if err := store.DB.Preload("Channel.Members.Card.CardSlot").Preload("Channel.Groups.GroupSlot").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_slot_id = ?", account.ID, channelID).First(&channelSlot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {

View File

@ -3,6 +3,7 @@ package databag
import (
"databag/internal/store"
"errors"
"gorm.io/gorm"
"github.com/gorilla/mux"
"net/http"
)
@ -29,8 +30,23 @@ func SetChannelNotification(w http.ResponseWriter, r *http.Request) {
return
}
// get channel entry
slot := store.ChannelSlot{}
if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, account.ID).First(&slot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if (slot.Channel == nil) {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel"));
return;
}
// update host notification status
if err = store.DB.Model(&store.Channel{}).Where("account_id = ? AND id = ?", account.ID, channelID).Update("host_push", flag).Error; err != nil {
if err = store.DB.Model(&store.Channel{}).Where("account_id = ? AND id = ?", account.ID, slot.Channel.ID).Update("host_push", flag).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
@ -41,8 +57,23 @@ func SetChannelNotification(w http.ResponseWriter, r *http.Request) {
return
}
// get channel entry
slot := store.ChannelSlot{}
if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, card.Account.ID).First(&slot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if (slot.Channel == nil) {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel"));
return;
}
// update member notification status
if err := store.DB.Model(&store.Member{}).Where("channel_id = ? AND card_id = ?", channelID, card.ID).Update("push_enabled", flag).Error; err != nil {
if err := store.DB.Model(&store.Member{}).Where("channel_id = ? AND card_id = ?", slot.Channel.ID, card.ID).Update("push_enabled", flag).Error; err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
}
} else {