330 lines
9.3 KiB
Go
330 lines
9.3 KiB
Go
package loanorders
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"online-back/internal/appresult"
|
|
"online-back/internal/handlers"
|
|
"online-back/internal/identity/rbac"
|
|
"online-back/internal/identity/rbac/perm"
|
|
"online-back/internal/shared"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
const baseURL = "/loan-orders"
|
|
|
|
type handler struct {
|
|
logger *logging.Logger
|
|
repository Repository
|
|
}
|
|
|
|
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
|
return &handler{logger: logger, repository: repository}
|
|
}
|
|
|
|
func (h *handler) Register(router *gin.RouterGroup) {
|
|
group := rbac.Group(router, baseURL, rbac.Require(perm.Loans))
|
|
group.GET("", h.list)
|
|
group.GET("/mobile", h.listMobile)
|
|
group.GET("/:id", h.getByID)
|
|
group.POST("", h.create)
|
|
group.PUT("/:id", h.update)
|
|
group.DELETE("/:id", h.delete)
|
|
}
|
|
|
|
// list godoc
|
|
// @Summary List loan orders
|
|
// @Description Returns paginated loan orders created from the web with optional filters
|
|
// @Tags loan-orders
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param status query string false "Filter by status"
|
|
// @Param branch_id query int false "Filter by branch ID"
|
|
// @Param user_id query int false "Filter by user ID"
|
|
// @Param region query string false "Filter by region"
|
|
// @Param page query int false "Page number" default(1)
|
|
// @Param limit query int false "Items per page" default(10)
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /loan-orders [get]
|
|
func (h *handler) list(c *gin.Context) {
|
|
filter, ok := parseListFilter(c)
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
filter.Source = "web"
|
|
h.respondList(c, filter)
|
|
}
|
|
|
|
// listMobile godoc
|
|
// @Summary List mobile loan orders
|
|
// @Description Returns paginated loan orders created from the mobile app
|
|
// @Tags loan-orders
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param status query string false "Filter by status"
|
|
// @Param branch_id query int false "Filter by branch ID"
|
|
// @Param user_id query int false "Filter by user ID"
|
|
// @Param region query string false "Filter by region"
|
|
// @Param page query int false "Page number" default(1)
|
|
// @Param limit query int false "Items per page" default(10)
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /loan-orders/mobile [get]
|
|
func (h *handler) listMobile(c *gin.Context) {
|
|
filter, ok := parseListFilter(c)
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
filter.Source = "mobile"
|
|
h.respondList(c, filter)
|
|
}
|
|
|
|
func (h *handler) respondList(c *gin.Context, filter ListFilter) {
|
|
result, err := h.repository.List(c.Request.Context(), filter)
|
|
if err != nil {
|
|
h.logger.Errorf("list loan orders failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// getByID godoc
|
|
// @Summary Get loan order by ID
|
|
// @Description Returns a single loan order
|
|
// @Tags loan-orders
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Loan order ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /loan-orders/{id} [get]
|
|
func (h *handler) getByID(c *gin.Context) {
|
|
id, ok := shared.ParseID(c.Param("id"))
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
item, err := h.repository.GetByID(c.Request.Context(), id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.logger.Errorf("get loan order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
if !rbac.GuardOrder(c, item.UserId, item.BranchId) {
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// create godoc
|
|
// @Summary Create loan order
|
|
// @Description Creates a new loan order
|
|
// @Tags loan-orders
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param body body CreateLoanOrderDTO true "Loan order payload"
|
|
// @Success 201 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /loan-orders [post]
|
|
func (h *handler) create(c *gin.Context) {
|
|
var dto CreateLoanOrderDTO
|
|
if err := c.ShouldBindJSON(&dto); err != nil {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
if dto.LoanType.Int64() <= 0 || dto.BranchId.Int64() <= 0 || dto.BornAt.IsZero() || dto.PassportGivenAt.IsZero() {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
item, err := h.repository.Create(c.Request.Context(), dto)
|
|
if err != nil {
|
|
h.logger.Errorf("create loan order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// update godoc
|
|
// @Summary Update loan order
|
|
// @Description Updates an existing loan order
|
|
// @Tags loan-orders
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Loan order ID"
|
|
// @Param body body UpdateLoanOrderDTO true "Loan order payload"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /loan-orders/{id} [put]
|
|
func (h *handler) update(c *gin.Context) {
|
|
id, ok := shared.ParseID(c.Param("id"))
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
if !h.guardBranchByID(c, id) {
|
|
return
|
|
}
|
|
|
|
var dto UpdateLoanOrderDTO
|
|
if err := c.ShouldBindJSON(&dto); err != nil {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
item, err := h.repository.Update(c.Request.Context(), id, dto)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.logger.Errorf("update loan order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// delete godoc
|
|
// @Summary Delete loan order
|
|
// @Description Deletes a loan order
|
|
// @Tags loan-orders
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Loan order ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /loan-orders/{id} [delete]
|
|
func (h *handler) delete(c *gin.Context) {
|
|
id, ok := shared.ParseID(c.Param("id"))
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
if !h.guardBranchByID(c, id) {
|
|
return
|
|
}
|
|
|
|
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
} else if err != nil {
|
|
h.logger.Errorf("delete loan order failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData("loan order removed"))
|
|
}
|
|
|
|
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
|
filter := ListFilter{
|
|
Page: shared.DefaultPage,
|
|
Limit: shared.DefaultLimit,
|
|
}
|
|
|
|
if raw := queryValue(c, "branch_id", "branchId"); raw != "" {
|
|
id, ok := shared.ParseID(raw)
|
|
if !ok {
|
|
return ListFilter{}, false
|
|
}
|
|
filter.BranchId = &id
|
|
}
|
|
|
|
if raw := queryValue(c, "user_id", "userId"); raw != "" {
|
|
id, ok := shared.ParseID(raw)
|
|
if !ok {
|
|
return ListFilter{}, false
|
|
}
|
|
filter.UserId = &id
|
|
}
|
|
|
|
if raw := queryValue(c, "page"); raw != "" {
|
|
page, err := strconv.Atoi(raw)
|
|
if err != nil || page < 1 {
|
|
return ListFilter{}, false
|
|
}
|
|
filter.Page = page
|
|
}
|
|
|
|
if raw := queryValue(c, "limit"); raw != "" {
|
|
limit, err := strconv.Atoi(raw)
|
|
if err != nil || limit < 1 {
|
|
return ListFilter{}, false
|
|
}
|
|
if limit > shared.MaxLimit {
|
|
limit = shared.MaxLimit
|
|
}
|
|
filter.Limit = limit
|
|
}
|
|
|
|
if raw := queryValue(c, "status"); raw != "" {
|
|
filter.Status = &raw
|
|
}
|
|
|
|
if raw := queryValue(c, "region"); raw != "" {
|
|
filter.Region = raw
|
|
}
|
|
|
|
rbac.BindBranchIDs(c, &filter.BranchIds)
|
|
return filter, true
|
|
}
|
|
|
|
func queryValue(c *gin.Context, keys ...string) string {
|
|
for _, key := range keys {
|
|
if raw := strings.TrimSpace(c.Query(key)); raw != "" && raw != "undefined" && raw != "null" {
|
|
return raw
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
|
if rbac.SkipRecordGuard(c) {
|
|
return true
|
|
}
|
|
item, err := h.repository.GetByID(c.Request.Context(), id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return false
|
|
}
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return false
|
|
}
|
|
return rbac.GuardOrder(c, item.UserId, item.BranchId)
|
|
}
|