2021-06-24 00:42:54 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AccountDel godoc
|
|
|
|
// @Summary Delete an account
|
2021-06-24 01:55:47 +02:00
|
|
|
// @Description Requires Authorization-header with role "admin" or a matching account id
|
2021-06-24 00:42:54 +02:00
|
|
|
// @Description Example: Authorization: bearer xxx
|
|
|
|
// @Description Where "xxx" is a valid JWT token
|
|
|
|
// @ID account-del
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Account ID"
|
|
|
|
// @Success 204 {string} string ""
|
|
|
|
// @Failure 400 {object} []ResJSONError
|
|
|
|
// @Failure 401 {object} []ResJSONError
|
|
|
|
// @Failure 403 {object} []ResJSONError
|
|
|
|
// @Failure 404 {object} []ResJSONError
|
|
|
|
// @Failure 415 {object} []ResJSONError
|
|
|
|
// @Failure 500 {object} []ResJSONError
|
2023-05-08 15:29:19 +02:00
|
|
|
// @Router /accounts/:id [delete]
|
2021-06-24 00:42:54 +02:00
|
|
|
func (h Handlers) AccountDel(c *fiber.Ctx) error {
|
|
|
|
accountID := c.Params("accountID")
|
|
|
|
|
|
|
|
_, uuidErr := uuid.Parse(accountID)
|
|
|
|
if uuidErr != nil {
|
|
|
|
return c.Status(400).JSON([]ResJSONError{{Error: "Invalid uuid format"}})
|
|
|
|
}
|
|
|
|
|
2021-06-24 00:46:28 +02:00
|
|
|
authErr := h.RequireAdminRoleOrAccountID(c, accountID)
|
2021-06-24 00:42:54 +02:00
|
|
|
if authErr != nil {
|
|
|
|
return c.Status(403).JSON([]ResJSONError{{Error: authErr.Error()}})
|
|
|
|
}
|
|
|
|
|
|
|
|
err := h.Db.AccountDel(accountID)
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "No account found for given accountID" {
|
|
|
|
return c.Status(404).JSON([]ResJSONError{{Error: err.Error()}})
|
|
|
|
} else {
|
|
|
|
return c.Status(500).JSON([]ResJSONError{{Error: "Database error when trying to remove account"}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(204).Send(nil)
|
|
|
|
}
|