feat(captain): align preferences payloads
This commit is contained in:
@@ -2,7 +2,6 @@ package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
@@ -23,8 +22,8 @@ func NewCaptainPreferenceHandler(svc *service.CaptainPreferenceService) *Captain
|
||||
// Create creates a new captain preference for an account.
|
||||
// POST /api/v1/accounts/:id/captain/preferences
|
||||
func (h *CaptainPreferenceHandler) Create(c *gin.Context) {
|
||||
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
@@ -35,7 +34,7 @@ func (h *CaptainPreferenceHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
pref, err := h.svc.Create(c.Request.Context(), uint(accountID), &req)
|
||||
pref, err := h.svc.Create(c.Request.Context(), accountID, &req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Create captain preference: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to create preference")
|
||||
@@ -48,57 +47,61 @@ func (h *CaptainPreferenceHandler) Create(c *gin.Context) {
|
||||
// Get retrieves the captain preference for an account.
|
||||
// GET /api/v1/accounts/:id/captain/preferences
|
||||
func (h *CaptainPreferenceHandler) Get(c *gin.Context) {
|
||||
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
pref, err := h.svc.Get(c.Request.Context(), uint(accountID))
|
||||
pref, err := h.svc.GetConfig(c.Request.Context(), accountID)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Get captain preference: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusNotFound, response.ErrNotFound, "preference not found")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, pref)
|
||||
c.JSON(http.StatusOK, pref)
|
||||
}
|
||||
|
||||
// Update updates the captain preference for an account.
|
||||
// PUT /api/v1/accounts/:id/captain/preferences
|
||||
func (h *CaptainPreferenceHandler) Update(c *gin.Context) {
|
||||
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
if !captainPreferencesCanUpdate(c) {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "administrator role required")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.UpdatePreferenceRequest
|
||||
var req service.UpdateCaptainConfigRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
pref, err := h.svc.Update(c.Request.Context(), uint(accountID), &req)
|
||||
pref, err := h.svc.UpdateConfig(c.Request.Context(), accountID, &req)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Update captain preference: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to update preference")
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, pref)
|
||||
c.JSON(http.StatusOK, pref)
|
||||
}
|
||||
|
||||
// Delete removes the captain preference for an account.
|
||||
// DELETE /api/v1/accounts/:id/captain/preferences
|
||||
func (h *CaptainPreferenceHandler) Delete(c *gin.Context) {
|
||||
accountID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
accountID := parseAccountIDParam(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.Delete(c.Request.Context(), uint(accountID)); err != nil {
|
||||
if err := h.svc.Delete(c.Request.Context(), accountID); err != nil {
|
||||
applogger.L().Errorf("Delete captain preference: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "failed to delete preference")
|
||||
return
|
||||
@@ -106,3 +109,8 @@ func (h *CaptainPreferenceHandler) Delete(c *gin.Context) {
|
||||
|
||||
response.OK(c, gin.H{"message": "preference deleted"})
|
||||
}
|
||||
|
||||
func captainPreferencesCanUpdate(c *gin.Context) bool {
|
||||
role := getRole(c)
|
||||
return role == "administrator" || role == "super_admin"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user