Put the user's role and branch on GET /profile, upsert role_user and branch_user on POST, and drop their list endpoints.
Also accept flexible card-order form values, move mock seed under db/mock, and regenerate Swagger. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7225dc0399
commit
8795ae2195
47 files changed
+1308
-1785
No files matched your search
@@ -188,46 +188,6 @@ func likePattern(term string) string {
|
||||
return "%" + term + "%"
|
||||
}
|
||||
|
||||
func (r *repository) ListBranchesByProvince(ctx context.Context, provinceID int64, activeOnly bool) (visamaster.ListBranchesByProvinceDTO, error) {
|
||||
args := []interface{}{provinceID}
|
||||
where := ` WHERE province_id = $1`
|
||||
if activeOnly {
|
||||
where += ` AND active = true`
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, COALESCE(unique_code, ''), name, region, province_id, active
|
||||
FROM branches` + where + `
|
||||
ORDER BY name->>'tm', name->>'en', id`
|
||||
|
||||
rows, err := r.client.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return visamaster.ListBranchesByProvinceDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
branches := make([]visamaster.BranchByProvinceDTO, 0)
|
||||
for rows.Next() {
|
||||
var item visamaster.BranchByProvinceDTO
|
||||
var nameJSON []byte
|
||||
if err := rows.Scan(&item.Id, &item.UniqueCode, &nameJSON, &item.Region, &item.ProvinceId, &item.Active); err != nil {
|
||||
return visamaster.ListBranchesByProvinceDTO{}, err
|
||||
}
|
||||
if err := json.Unmarshal(nameJSON, &item.Name); err != nil {
|
||||
return visamaster.ListBranchesByProvinceDTO{}, err
|
||||
}
|
||||
branches = append(branches, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return visamaster.ListBranchesByProvinceDTO{}, err
|
||||
}
|
||||
|
||||
return visamaster.ListBranchesByProvinceDTO{
|
||||
Count: int64(len(branches)),
|
||||
Branches: branches,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (visamaster.VisaMasterPaymentOrderDTO, error) {
|
||||
query := `SELECT ` + orderSelectColumns + orderFromClause + ` WHERE o.id = $1`
|
||||
return scanOrder(r.client.QueryRow(ctx, query, id))
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/visamaster"
|
||||
"online-back/internal/config"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
)
|
||||
|
||||
func testRepo(t *testing.T) (visamaster.Repository, func()) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
pool, err := postgresql.NewClient(ctx, 2, config.StorageConfig{
|
||||
Host: "localhost",
|
||||
Port: "5424",
|
||||
Database: "bank_db",
|
||||
Username: "bank_admin",
|
||||
Password: "bank@#23#@_password",
|
||||
PgPoolMaxConn: 4,
|
||||
})
|
||||
if err != nil {
|
||||
t.Skipf("postgres unavailable: %v", err)
|
||||
}
|
||||
|
||||
return NewRepository(pool, logging.GetLogger()), func() { pool.Close() }
|
||||
}
|
||||
|
||||
func TestGetByIDIncludesBranchAndUser(t *testing.T) {
|
||||
repo, closeRepo := testRepo(t)
|
||||
defer closeRepo()
|
||||
ctx := context.Background()
|
||||
|
||||
item, err := repo.GetByID(ctx, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByID: %v", err)
|
||||
}
|
||||
|
||||
if item.BranchId == 0 {
|
||||
t.Fatal("branchId is empty")
|
||||
}
|
||||
if item.Branch.Id != item.BranchId {
|
||||
t.Fatalf("branch.id=%d does not match branchId=%d", item.Branch.Id, item.BranchId)
|
||||
}
|
||||
if item.Branch.Name.Tm == "" && item.Branch.Name.En == "" {
|
||||
t.Fatalf("branchId=%d but branch.name is empty: %+v", item.BranchId, item.Branch.Name)
|
||||
}
|
||||
if item.UserId != nil {
|
||||
if item.User == nil {
|
||||
t.Fatalf("userId=%d but user object is nil", *item.UserId)
|
||||
}
|
||||
if item.User.Name == "" && item.User.Username == "" {
|
||||
t.Fatalf("userId=%d but user name/username are empty", *item.UserId)
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("id=%d branchId=%d branch=%q userId=%v user=%v",
|
||||
item.Id, item.BranchId, item.Branch.Name.Tm, item.UserId, item.User)
|
||||
}
|
||||
|
||||
func TestListIncludesBranchAndUser(t *testing.T) {
|
||||
repo, closeRepo := testRepo(t)
|
||||
defer closeRepo()
|
||||
|
||||
list, err := repo.List(context.Background(), visamaster.ListFilter{Page: 1, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if list.Count == 0 || len(list.VisaMasterPaymentOrders) == 0 {
|
||||
t.Fatal("List returned no orders")
|
||||
}
|
||||
|
||||
for _, item := range list.VisaMasterPaymentOrders {
|
||||
if item.BranchId != 0 && item.Branch.Name.Tm == "" && item.Branch.Name.En == "" {
|
||||
t.Errorf("order %d branchId=%d missing branch.name", item.Id, item.BranchId)
|
||||
}
|
||||
if item.UserId != nil && (item.User == nil || (item.User.Name == "" && item.User.Username == "")) {
|
||||
t.Errorf("order %d userId=%d missing user details", item.Id, *item.UserId)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,17 +129,3 @@ type ListVisaMasterPaymentOrderDTO struct {
|
||||
Count int64 `json:"count"`
|
||||
VisaMasterPaymentOrders []VisaMasterPaymentOrderDTO `json:"visaMasterPaymentOrders"`
|
||||
}
|
||||
|
||||
type BranchByProvinceDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Name LocalizedText `json:"name"`
|
||||
Region string `json:"region"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type ListBranchesByProvinceDTO struct {
|
||||
Count int64 `json:"count"`
|
||||
Branches []BranchByProvinceDTO `json:"branches"`
|
||||
}
|
||||
@@ -28,7 +28,6 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
||||
group.GET("", h.list)
|
||||
group.GET("/branches", h.listBranchesByProvince)
|
||||
group.GET("/:id", h.getByID)
|
||||
group.POST("", h.create)
|
||||
group.PUT("/:id", h.update)
|
||||
@@ -69,45 +68,6 @@ func (h *handler) list(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// listBranchesByProvince godoc
|
||||
// @Summary List branches by province
|
||||
// @Description Returns branches for the selected province for visa/master payment order forms
|
||||
// @Tags visa-master-payment-orders
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param province_id query int true "Province ID"
|
||||
// @Param active query bool false "Only active branches" default(true)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /visa-master-payment-orders/branches [get]
|
||||
func (h *handler) listBranchesByProvince(c *gin.Context) {
|
||||
provinceID, ok := parseID(c.Query("province_id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
activeOnly := true
|
||||
if raw := c.Query("active"); raw != "" {
|
||||
active, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
activeOnly = active
|
||||
}
|
||||
|
||||
result, err := h.repository.ListBranchesByProvince(c.Request.Context(), provinceID, activeOnly)
|
||||
if err != nil {
|
||||
h.logger.Errorf("list branches by province failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// getByID godoc
|
||||
// @Summary Get visa/master payment order by ID
|
||||
// @Description Returns a single visa/master payment order with branch and user details
|
||||
|
||||
@@ -4,7 +4,6 @@ import "context"
|
||||
|
||||
type Repository interface {
|
||||
List(ctx context.Context, filter ListFilter) (ListVisaMasterPaymentOrderDTO, error)
|
||||
ListBranchesByProvince(ctx context.Context, provinceID int64, activeOnly bool) (ListBranchesByProvinceDTO, error)
|
||||
GetByID(ctx context.Context, id int64) (VisaMasterPaymentOrderDTO, error)
|
||||
Create(ctx context.Context, dto CreateVisaMasterPaymentOrderDTO) (VisaMasterPaymentOrderDTO, error)
|
||||
Update(ctx context.Context, id int64, dto UpdateVisaMasterPaymentOrderDTO) (VisaMasterPaymentOrderDTO, error)
|
||||
|
||||
Reference in new issue
Block a user