106 lines
3.8 KiB
Go
106 lines
3.8 KiB
Go
package v1
|
|
|
|
// NotificationSettingHandler handles notification setting API endpoints.
|
|
// Reference: Chatwoot NotificationSettingsController — show + update
|
|
//
|
|
// Routes:
|
|
// GET /api/v1/accounts/:account_id/notification_settings
|
|
// PATCH /api/v1/accounts/:account_id/notification_settings
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/service"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// NotificationSettingHandler handles HTTP requests for notification settings.
|
|
type NotificationSettingHandler struct {
|
|
svc *service.NotificationSettingService
|
|
}
|
|
|
|
// NewNotificationSettingHandler creates a new handler instance.
|
|
func NewNotificationSettingHandler(svc *service.NotificationSettingService) *NotificationSettingHandler {
|
|
return &NotificationSettingHandler{svc: svc}
|
|
}
|
|
|
|
// NotificationSettingUpdateWrapper matches Chatwoot params.require(:notification_settings) —
|
|
// request body must be nested under "notification_settings" key.
|
|
type NotificationSettingUpdateWrapper struct {
|
|
NotificationSettings service.UpdateNotificationSettingRequest `json:"notification_settings"`
|
|
}
|
|
|
|
// Show returns the notification setting for the current user in the account.
|
|
// GET /api/v1/accounts/:account_id/notification_settings
|
|
// Reference: Chatwoot show — @user.notification_settings.find_by(account_id: Current.account.id)
|
|
// Jbuilder: id, user_id, account_id, all_email_flags, selected_email_flags, all_push_flags, selected_push_flags
|
|
func (h *NotificationSettingHandler) Show(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
userID := getUserID(c)
|
|
if userID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "user not authenticated")
|
|
return
|
|
}
|
|
|
|
ns, svcErr := h.svc.Get(c.Request.Context(), accountID, userID)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, serializeNotificationSetting(ns))
|
|
}
|
|
|
|
// Update modifies notification settings for the current user in the account.
|
|
// PATCH /api/v1/accounts/:account_id/notification_settings
|
|
// Reference: Chatwoot update — params.require(:notification_settings).permit(selected_email_flags: [], selected_push_flags: [])
|
|
// Then update_flags → save → render show
|
|
func (h *NotificationSettingHandler) Update(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
|
return
|
|
}
|
|
|
|
userID := getUserID(c)
|
|
if userID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "user not authenticated")
|
|
return
|
|
}
|
|
|
|
var wrapper NotificationSettingUpdateWrapper
|
|
if err := c.ShouldBindJSON(&wrapper); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
ns, svcErr := h.svc.Update(c.Request.Context(), accountID, userID, wrapper.NotificationSettings)
|
|
if svcErr != nil {
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, serializeNotificationSetting(ns))
|
|
}
|
|
|
|
// serializeNotificationSetting builds the response object matching Chatwoot Jbuilder show.json.jbuilder:
|
|
// id, user_id, account_id, all_email_flags, selected_email_flags, all_push_flags, selected_push_flags
|
|
func serializeNotificationSetting(ns *model.NotificationSetting) gin.H {
|
|
return gin.H{
|
|
"id": ns.ID,
|
|
"user_id": ns.UserID,
|
|
"account_id": ns.AccountID,
|
|
"all_email_flags": model.AllEmailFlagNames(),
|
|
"selected_email_flags": ns.SelectedEmailFlagNames(),
|
|
"all_push_flags": model.AllPushFlagNames(),
|
|
"selected_push_flags": ns.SelectedPushFlagNames(),
|
|
}
|
|
}
|