Files
gochat/backend/internal/handler/api/v1/notification_setting_handler.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

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(),
}
}