Add backups, currencies and dashboard APIs, and scope access by guard_name.
Web users may only CRUD their own records, operators stay in assigned branches, and admin/manager/superadmin keep full access. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
0d000f9af6
commit
28509eb4ca
65 files changed
+9132
-296
No files matched your search
@@ -1,64 +0,0 @@
|
||||
package loanorders_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"online-back/internal/lending/loanorders"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
func mobileCreateBody() []byte {
|
||||
body, _ := json.Marshal(map[string]any{
|
||||
"source": "mobile", "loanType": "1", "region": "Ahal", "branchId": "1",
|
||||
"customerName": "Kerim", "customerSurname": "Atayew",
|
||||
"passportAddress": "Ashgabat", "realAddress": "Ashgabat",
|
||||
"passportSerie": "I-AG", "passportId": "101010",
|
||||
"passportGivenAt": "2017-01-15", "passportGivenBy": "Police",
|
||||
"bornPlace": "Ashgabat", "bornAt": "1989-03-21",
|
||||
"phone": "+993 61 92 92 48", "education": "higher", "marriageStatus": "married",
|
||||
"passportOne": "a.jpg", "passportTwo": "b.jpg", "passportThree": "c.jpg", "passportFour": "d.jpg",
|
||||
"satisfiable": "yes", "guarantorNote": "Brother", "loanAmount": "20000",
|
||||
})
|
||||
return body
|
||||
}
|
||||
|
||||
func TestCreateLoanOrderDTOBindMobilePayload(t *testing.T) {
|
||||
var dto loanorders.CreateLoanOrderDTO
|
||||
if err := json.Unmarshal(mobileCreateBody(), &dto); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if dto.LoanType.Int64() != 1 || dto.BranchId.Int64() != 1 {
|
||||
t.Fatalf("ids: loanType=%d branchId=%d", dto.LoanType.Int64(), dto.BranchId.Int64())
|
||||
}
|
||||
if dto.Region != "Ahal" {
|
||||
t.Fatalf("region=%q", dto.Region)
|
||||
}
|
||||
if dto.BornAt.Time().Format("2006-01-02") != "1989-03-21" {
|
||||
t.Fatalf("bornAt=%v", dto.BornAt.Time())
|
||||
}
|
||||
if dto.PassportGivenAt.Time().Format("2006-01-02") != "2017-01-15" {
|
||||
t.Fatalf("passportGivenAt=%v", dto.PassportGivenAt.Time())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateLoanOrderGinBindRequired(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(mobileCreateBody()))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
var dto loanorders.CreateLoanOrderDTO
|
||||
if err := c.ShouldBindBodyWith(&dto, binding.JSON); err != nil {
|
||||
t.Fatalf("ShouldBindJSON: %v", err)
|
||||
}
|
||||
if dto.LoanType.Int64() != 1 || dto.Region != "Ahal" {
|
||||
t.Fatalf("unexpected dto loanType=%d region=%q", dto.LoanType.Int64(), dto.Region)
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
|
||||
// list godoc
|
||||
// @Summary List loan orders
|
||||
// @Description Returns paginated loan orders with optional filters
|
||||
// @Description Returns paginated loan orders created from the web with optional filters
|
||||
// @Tags loan-orders
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
@@ -58,6 +58,7 @@ func (h *handler) list(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
filter.Source = "web"
|
||||
h.respondList(c, filter)
|
||||
}
|
||||
|
||||
@@ -127,7 +128,7 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !rbac.GuardBranch(c, item.BranchId) {
|
||||
if !rbac.GuardOrder(c, item.UserId, item.BranchId) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -324,7 +325,7 @@ func queryValue(c *gin.Context, keys ...string) string {
|
||||
}
|
||||
|
||||
func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
||||
if !rbac.HasBranchScope(c) {
|
||||
if rbac.SkipRecordGuard(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
@@ -336,5 +337,5 @@ func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return false
|
||||
}
|
||||
return rbac.GuardBranch(c, item.BranchId)
|
||||
return rbac.GuardOrder(c, item.UserId, item.BranchId)
|
||||
}
|
||||
@@ -128,6 +128,11 @@ func buildListQuery(filter loanpaidletter.ListFilter) (whereClause string, args
|
||||
conditions = append(conditions, fmt.Sprintf("l.status = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.UserId != nil {
|
||||
args = append(args, *filter.UserId)
|
||||
conditions = append(conditions, fmt.Sprintf("l.user_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(filter.BranchIds) > 0 {
|
||||
conditions, args = rbac.AppendBranchIDs(conditions, args, "l.branch_id", filter.BranchIds)
|
||||
} else if filter.BranchId != nil {
|
||||
|
||||
@@ -7,32 +7,32 @@ import (
|
||||
)
|
||||
|
||||
type LoanPaidLetterDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
Region string `json:"region"`
|
||||
Id int64 `json:"id"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
Region string `json:"region"`
|
||||
RegionName shared.LocalizedText `json:"regionName"`
|
||||
BranchId int64 `json:"-"`
|
||||
Branch *shared.BranchInfo `json:"branch,omitempty"`
|
||||
ProvinceId *int64 `json:"-"`
|
||||
Province *shared.NamedInfo `json:"province,omitempty"`
|
||||
UserId *int64 `json:"-"`
|
||||
User *shared.UserInfo `json:"user,omitempty"`
|
||||
CustomerName string `json:"customerName"`
|
||||
CustomerSurname string `json:"customerSurname"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *time.Time `json:"bornAt"`
|
||||
Phone string `json:"phone"`
|
||||
PassportSerie string `json:"passportSerie"`
|
||||
PassportId string `json:"passportId"`
|
||||
Status *string `json:"status"`
|
||||
Notes *string `json:"notes"`
|
||||
LoanContractNumber *string `json:"loanContractNumber"`
|
||||
LoanContractDate *time.Time `json:"loanContractDate"`
|
||||
LoanAmount *string `json:"loanAmount"`
|
||||
LoanReason *string `json:"loanReason"`
|
||||
LoanFile *string `json:"loanFile"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
BranchId int64 `json:"-"`
|
||||
Branch *shared.BranchInfo `json:"branch,omitempty"`
|
||||
ProvinceId *int64 `json:"-"`
|
||||
Province *shared.NamedInfo `json:"province,omitempty"`
|
||||
UserId *int64 `json:"-"`
|
||||
User *shared.UserInfo `json:"user,omitempty"`
|
||||
CustomerName string `json:"customerName"`
|
||||
CustomerSurname string `json:"customerSurname"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *time.Time `json:"bornAt"`
|
||||
Phone string `json:"phone"`
|
||||
PassportSerie string `json:"passportSerie"`
|
||||
PassportId string `json:"passportId"`
|
||||
Status *string `json:"status"`
|
||||
Notes *string `json:"notes"`
|
||||
LoanContractNumber *string `json:"loanContractNumber"`
|
||||
LoanContractDate *time.Time `json:"loanContractDate"`
|
||||
LoanAmount *string `json:"loanAmount"`
|
||||
LoanReason *string `json:"loanReason"`
|
||||
LoanFile *string `json:"loanFile"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
type CreateLoanPaidLetterDTO struct {
|
||||
@@ -81,6 +81,7 @@ type ListFilter struct {
|
||||
PassportId string
|
||||
Name string
|
||||
Status *string
|
||||
UserId *int64
|
||||
BranchId *int64
|
||||
BranchIds []int64
|
||||
Region string
|
||||
@@ -95,12 +96,12 @@ type ListLoanPaidLetterDTO struct {
|
||||
|
||||
// BranchByProvinceDTO is a compact branch row for province-scoped pickers.
|
||||
type BranchByProvinceDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Id int64 `json:"id"`
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Name shared.LocalizedText `json:"name"`
|
||||
Region string `json:"region"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
Active bool `json:"active"`
|
||||
Region string `json:"region"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type ListBranchesByProvinceDTO struct {
|
||||
|
||||
@@ -138,7 +138,7 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
if !rbac.GuardBranch(c, item.BranchId) {
|
||||
if !rbac.GuardOrder(c, item.UserId, item.BranchId) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -314,12 +314,24 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.BranchId = &id
|
||||
}
|
||||
|
||||
raw := strings.TrimSpace(c.Query("user_id"))
|
||||
if raw == "" {
|
||||
raw = strings.TrimSpace(c.Query("userId"))
|
||||
}
|
||||
if raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.UserId = &id
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
return filter, true
|
||||
}
|
||||
|
||||
func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
||||
if !rbac.HasBranchScope(c) {
|
||||
if rbac.SkipRecordGuard(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
@@ -331,5 +343,5 @@ func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return false
|
||||
}
|
||||
return rbac.GuardBranch(c, item.BranchId)
|
||||
return rbac.GuardOrder(c, item.UserId, item.BranchId)
|
||||
}
|
||||
@@ -81,6 +81,11 @@ func buildListQuery(filter loansrepayment.ListFilter) (whereClause string, args
|
||||
conditions = append(conditions, fmt.Sprintf("l.passport_id ILIKE $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.UserId != nil {
|
||||
args = append(args, *filter.UserId)
|
||||
conditions = append(conditions, fmt.Sprintf("l.user_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(filter.BranchIds) > 0 {
|
||||
args = append(args, filter.BranchIds)
|
||||
conditions = append(conditions, rbac.UserInBranchSQL("l.user_id", len(args)))
|
||||
|
||||
@@ -33,6 +33,7 @@ type UpdateLoanRepaymentDTO struct {
|
||||
|
||||
type ListFilter struct {
|
||||
PassportId string
|
||||
UserId *int64
|
||||
BranchIds []int64
|
||||
Page int
|
||||
Limit int
|
||||
|
||||
@@ -250,12 +250,24 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.PassportId = raw
|
||||
}
|
||||
|
||||
raw := strings.TrimSpace(c.Query("user_id"))
|
||||
if raw == "" {
|
||||
raw = strings.TrimSpace(c.Query("userId"))
|
||||
}
|
||||
if raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.UserId = &id
|
||||
}
|
||||
|
||||
rbac.BindBranchIDs(c, &filter.BranchIds)
|
||||
return filter, true
|
||||
}
|
||||
|
||||
func (h *handler) guardUserByID(c *gin.Context, id int64) bool {
|
||||
if !rbac.HasBranchScope(c) {
|
||||
if rbac.SkipRecordGuard(c) {
|
||||
return true
|
||||
}
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
|
||||
Reference in new issue
Block a user