fix(routes): align dashboard app ids

This commit is contained in:
2026-06-07 12:39:23 +08:00
parent 27f927327d
commit 5062acec61
6 changed files with 58 additions and 47 deletions
@@ -70,9 +70,9 @@ func (h *DashboardAppHandler) Create(c *gin.Context) {
}
// Get retrieves a dashboard app by ID.
// GET /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id
// GET /api/v1/accounts/:account_id/dashboard_apps/:id
func (h *DashboardAppHandler) Get(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -95,10 +95,10 @@ func (h *DashboardAppHandler) Get(c *gin.Context) {
}
// Update modifies an existing dashboard app.
// PUT /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id
// PUT /api/v1/accounts/:account_id/dashboard_apps/:id
// Chatwoot: @dashboard_app.update!(permitted_payload)
func (h *DashboardAppHandler) Update(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -127,10 +127,10 @@ func (h *DashboardAppHandler) Update(c *gin.Context) {
}
// Delete removes a dashboard app.
// DELETE /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id
// DELETE /api/v1/accounts/:account_id/dashboard_apps/:id
// Chatwoot: @dashboard_app.destroy!; head :no_content (204)
func (h *DashboardAppHandler) Delete(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -155,10 +155,10 @@ func (h *DashboardAppHandler) Delete(c *gin.Context) {
response.NoContent(c)
}
// Patch partially updates a dashboard app (GoChat extension — Chatwoot has no PATCH).
// PATCH /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id
// Patch partially updates a dashboard app through Rails resource update.
// PATCH /api/v1/accounts/:account_id/dashboard_apps/:id
func (h *DashboardAppHandler) Patch(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -230,6 +230,13 @@ func bindDashboardAppCreate(c *gin.Context) (*service.CreateDashboardAppRequest,
return &req, nil
}
func parseDashboardAppIDParam(c *gin.Context) (uint, error) {
if c.Param("id") != "" {
return parseUintParam(c, "id")
}
return parseUintParam(c, "dashboard_app_id")
}
func bindDashboardAppUpdate(c *gin.Context) (*service.UpdateDashboardAppRequest, error) {
var raw map[string]json.RawMessage
if err := c.ShouldBindJSON(&raw); err != nil {
@@ -294,9 +301,9 @@ func (h *DashboardAppHandler) Search(c *gin.Context) {
}
// GetWidgets returns all widgets in a dashboard app's content.
// GET /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id/widgets
// GET /api/v1/accounts/:account_id/dashboard_apps/:id/widgets
func (h *DashboardAppHandler) GetWidgets(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -313,9 +320,9 @@ func (h *DashboardAppHandler) GetWidgets(c *gin.Context) {
}
// AddWidget adds a widget to a dashboard app's content array.
// POST /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id/widgets
// POST /api/v1/accounts/:account_id/dashboard_apps/:id/widgets
func (h *DashboardAppHandler) AddWidget(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -338,9 +345,9 @@ func (h *DashboardAppHandler) AddWidget(c *gin.Context) {
}
// RemoveWidget removes a widget from a dashboard app's content array by index.
// DELETE /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id/widgets/:widget_index
// DELETE /api/v1/accounts/:account_id/dashboard_apps/:id/widgets/:widget_index
func (h *DashboardAppHandler) RemoveWidget(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -363,10 +370,10 @@ func (h *DashboardAppHandler) RemoveWidget(c *gin.Context) {
}
// UpdateWidget updates a widget in a dashboard app's content array by index.
// PUT /api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id/widgets/:widget_index
// PUT /api/v1/accounts/:account_id/dashboard_apps/:id/widgets/:widget_index
// GoChat extension — Chatwoot has no widget sub-resource CRUD.
func (h *DashboardAppHandler) UpdateWidget(c *gin.Context) {
id, err := parseUintParam(c, "dashboard_app_id")
id, err := parseDashboardAppIDParam(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid dashboard_app_id")
return
@@ -137,7 +137,7 @@ func (s *DashboardAppHandlerTestSuite) TestCreate_Success() {
func (s *DashboardAppHandlerTestSuite) TestGet_Success() {
r := gin.New()
r.GET("/api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id", s.handler.Get)
r.GET("/api/v1/accounts/:account_id/dashboard_apps/:id", s.handler.Get)
// Seed via service (same DB path as handler)
seedApp, err := s.handler.svc.Create(context.Background(), s.account.ID, &s.user.ID, &service.CreateDashboardAppRequest{
@@ -163,7 +163,7 @@ func (s *DashboardAppHandlerTestSuite) TestGet_Success() {
func (s *DashboardAppHandlerTestSuite) TestPatch_RawPayloadAndAccountScope() {
r := gin.New()
r.PATCH("/api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id", s.handler.Patch)
r.PATCH("/api/v1/accounts/:account_id/dashboard_apps/:id", s.handler.Patch)
seedApp, err := s.handler.svc.Create(context.Background(), s.account.ID, &s.user.ID, &service.CreateDashboardAppRequest{
Title: "Before",
@@ -192,7 +192,7 @@ func (s *DashboardAppHandlerTestSuite) TestPatch_RawPayloadAndAccountScope() {
func (s *DashboardAppHandlerTestSuite) TestDelete_Success() {
r := gin.New()
r.DELETE("/api/v1/accounts/:account_id/dashboard_apps/:dashboard_app_id", s.handler.Delete)
r.DELETE("/api/v1/accounts/:account_id/dashboard_apps/:id", s.handler.Delete)
// Seed via service
seedApp, err := s.handler.svc.Create(context.Background(), s.account.ID, &s.user.ID, &service.CreateDashboardAppRequest{