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
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/cardorder"
|
||||
"online-back/pkg/client/postgresql"
|
||||
@@ -25,8 +26,6 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) cardorder.R
|
||||
}
|
||||
}
|
||||
|
||||
// cardOrderWritableColumns lists every card_orders column except id, created_at and
|
||||
// updated_at, in the exact order CreateCardOrderDTO/UpdateCardOrderDTO fields are read.
|
||||
var cardOrderWritableColumns = []string{
|
||||
"unique_id", "card_state_id", "card_type_id", "region", "branch_id",
|
||||
"customer_name", "customer_surname", "customer_patronic_name", "born_at", "old_surname", "citizenship",
|
||||
@@ -58,24 +57,40 @@ func resolvePaid(paid *bool) bool {
|
||||
|
||||
func createArgs(dto cardorder.CreateCardOrderDTO) []interface{} {
|
||||
return []interface{}{
|
||||
dto.UniqueId, dto.CardStateId, dto.CardTypeId, dto.Region, dto.BranchId,
|
||||
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, dto.BornAt, dto.OldSurname, dto.Citizenship,
|
||||
dto.PassportSerie, dto.PassportId, dto.PassportGivenAt, dto.PassportGivenBy, dto.BornPlace,
|
||||
dto.UniqueId, dto.CardStateId.Int64(), dto.CardTypeId.Int64(), dto.Region, dto.BranchId.Int64(),
|
||||
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, optionalTime(dto.BornAt), dto.OldSurname, dto.Citizenship,
|
||||
dto.PassportSerie, dto.PassportId, dto.PassportGivenAt.Time(), dto.PassportGivenBy, dto.BornPlace,
|
||||
dto.JobLocation, dto.PassportAddress, dto.RealAddress, dto.Phone, dto.PhoneAdditional,
|
||||
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, dto.UserId, resolvePaid(dto.Paid),
|
||||
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, optionalInt64(dto.UserId), resolvePaid(dto.Paid),
|
||||
}
|
||||
}
|
||||
|
||||
func updateArgs(dto cardorder.UpdateCardOrderDTO) []interface{} {
|
||||
return []interface{}{
|
||||
dto.UniqueId, dto.CardStateId, dto.CardTypeId, dto.Region, dto.BranchId,
|
||||
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, dto.BornAt, dto.OldSurname, dto.Citizenship,
|
||||
dto.PassportSerie, dto.PassportId, dto.PassportGivenAt, dto.PassportGivenBy, dto.BornPlace,
|
||||
dto.UniqueId, dto.CardStateId.Int64(), dto.CardTypeId.Int64(), dto.Region, dto.BranchId.Int64(),
|
||||
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, optionalTime(dto.BornAt), dto.OldSurname, dto.Citizenship,
|
||||
dto.PassportSerie, dto.PassportId, dto.PassportGivenAt.Time(), dto.PassportGivenBy, dto.BornPlace,
|
||||
dto.JobLocation, dto.PassportAddress, dto.RealAddress, dto.Phone, dto.PhoneAdditional,
|
||||
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, dto.UserId, dto.Paid,
|
||||
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, optionalInt64(dto.UserId), dto.Paid,
|
||||
}
|
||||
}
|
||||
|
||||
func optionalTime(t *cardorder.FlexibleTime) *time.Time {
|
||||
if t == nil || t.IsZero() {
|
||||
return nil
|
||||
}
|
||||
tt := t.Time()
|
||||
return &tt
|
||||
}
|
||||
|
||||
func optionalInt64(n *cardorder.FlexibleInt64) *int64 {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
v := n.Int64()
|
||||
return &v
|
||||
}
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter cardorder.ListFilter) (cardorder.ListCardOrderDTO, error) {
|
||||
whereClause, args := buildListQuery(filter)
|
||||
|
||||
|
||||
+167
-59
@@ -1,6 +1,114 @@
|
||||
package cardorder
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FlexibleTime time.Time
|
||||
|
||||
func (t FlexibleTime) Time() time.Time { return time.Time(t) }
|
||||
|
||||
func (t FlexibleTime) IsZero() bool { return time.Time(t).IsZero() }
|
||||
|
||||
func (t FlexibleTime) MarshalJSON() ([]byte, error) {
|
||||
return time.Time(t).MarshalJSON()
|
||||
}
|
||||
|
||||
func (t *FlexibleTime) UnmarshalJSON(b []byte) error {
|
||||
*t = FlexibleTime{}
|
||||
raw := strings.TrimSpace(string(b))
|
||||
if raw == "" || raw == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var n json.Number
|
||||
if err := json.Unmarshal(b, &n); err == nil {
|
||||
s := n.String()
|
||||
if parsed, ok := parseFlexibleTime(s); ok {
|
||||
*t = parsed
|
||||
return nil
|
||||
}
|
||||
if unix, err := n.Int64(); err == nil && unix > 0 {
|
||||
*t = FlexibleTime(time.Unix(unix, 0).UTC())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return nil
|
||||
}
|
||||
if parsed, ok := parseFlexibleTime(s); ok {
|
||||
*t = parsed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseFlexibleTime(s string) (FlexibleTime, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return FlexibleTime{}, false
|
||||
}
|
||||
switch strings.ToLower(s) {
|
||||
case "string", "undefined", "null", "none", "invalid", "nan", "-":
|
||||
return FlexibleTime{}, false
|
||||
}
|
||||
for _, layout := range []string{
|
||||
time.RFC3339Nano,
|
||||
time.RFC3339,
|
||||
"2006-01-02T15:04:05Z07:00",
|
||||
"2006-01-02T15:04:05.000Z",
|
||||
"2006-01-02T15:04:05",
|
||||
"2006-01-02 15:04:05",
|
||||
"2006-01-02",
|
||||
"02.01.2006",
|
||||
"02/01/2006",
|
||||
"02-01-2006",
|
||||
"2006/01/02",
|
||||
"20060102",
|
||||
} {
|
||||
if parsed, err := time.Parse(layout, s); err == nil {
|
||||
return FlexibleTime(parsed), true
|
||||
}
|
||||
}
|
||||
return FlexibleTime{}, false
|
||||
}
|
||||
|
||||
type FlexibleInt64 int64
|
||||
|
||||
func (n FlexibleInt64) Int64() int64 { return int64(n) }
|
||||
|
||||
func (n *FlexibleInt64) UnmarshalJSON(b []byte) error {
|
||||
if string(b) == "null" {
|
||||
*n = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
var v int64
|
||||
if err := json.Unmarshal(b, &v); err == nil {
|
||||
*n = FlexibleInt64(v)
|
||||
return nil
|
||||
}
|
||||
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
*n = 0
|
||||
return nil
|
||||
}
|
||||
v, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*n = FlexibleInt64(v)
|
||||
return nil
|
||||
}
|
||||
|
||||
type CardOrderDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
@@ -38,67 +146,67 @@ type CardOrderDTO struct {
|
||||
}
|
||||
|
||||
type CreateCardOrderDTO struct {
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId int64 `json:"cardStateId" binding:"required"`
|
||||
CardTypeId int64 `json:"cardTypeId" binding:"required"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId int64 `json:"branchId" binding:"required"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *time.Time `json:"bornAt"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt time.Time `json:"passportGivenAt" binding:"required"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UserId *int64 `json:"userId"`
|
||||
Paid *bool `json:"paid"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
|
||||
CardTypeId FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UserId *FlexibleInt64 `json:"userId" swaggertype:"integer"`
|
||||
Paid *bool `json:"paid"`
|
||||
}
|
||||
|
||||
type UpdateCardOrderDTO struct {
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId int64 `json:"cardStateId" binding:"required"`
|
||||
CardTypeId int64 `json:"cardTypeId" binding:"required"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId int64 `json:"branchId" binding:"required"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *time.Time `json:"bornAt"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt time.Time `json:"passportGivenAt" binding:"required"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UserId *int64 `json:"userId"`
|
||||
Paid bool `json:"paid"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
CardStateId FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
|
||||
CardTypeId FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
BranchId FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
|
||||
CustomerName string `json:"customerName" binding:"required"`
|
||||
CustomerSurname string `json:"customerSurname" binding:"required"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
BornAt *FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
|
||||
OldSurname *string `json:"oldSurname"`
|
||||
Citizenship string `json:"citizenship" binding:"required"`
|
||||
PassportSerie string `json:"passportSerie" binding:"required"`
|
||||
PassportId string `json:"passportId" binding:"required"`
|
||||
PassportGivenAt FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
|
||||
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
|
||||
BornPlace string `json:"bornPlace" binding:"required"`
|
||||
JobLocation *string `json:"jobLocation"`
|
||||
PassportAddress *string `json:"passportAddress"`
|
||||
RealAddress *string `json:"realAddress"`
|
||||
Phone *string `json:"phone"`
|
||||
PhoneAdditional *string `json:"phoneAdditional"`
|
||||
Status *string `json:"status"`
|
||||
PassportOne string `json:"passportOne" binding:"required"`
|
||||
PassportTwo string `json:"passportTwo" binding:"required"`
|
||||
PassportThree string `json:"passportThree" binding:"required"`
|
||||
PassportFour string `json:"passportFour" binding:"required"`
|
||||
Notes *string `json:"notes"`
|
||||
UserId *FlexibleInt64 `json:"userId" swaggertype:"integer"`
|
||||
Paid bool `json:"paid"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
|
||||
@@ -116,7 +116,8 @@ func (h *handler) getByID(c *gin.Context) {
|
||||
// @Router /card-orders [post]
|
||||
func (h *handler) create(c *gin.Context) {
|
||||
var dto CreateCardOrderDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
if err := c.ShouldBindBodyWithJSON(&dto); err != nil {
|
||||
h.logger.Errorf("create card order bind failed: %v", err)
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
@@ -153,7 +154,8 @@ func (h *handler) update(c *gin.Context) {
|
||||
}
|
||||
|
||||
var dto UpdateCardOrderDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
if err := c.ShouldBindBodyWithJSON(&dto); err != nil {
|
||||
h.logger.Errorf("update card order bind failed: %v", err)
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
@@ -223,39 +225,31 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
Limit: defaultLimit,
|
||||
}
|
||||
|
||||
if raw := c.Query("branch_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.BranchId = &id
|
||||
if id, ok := optionalQueryID(c, "branch_id", "branchId"); !ok {
|
||||
return ListFilter{}, false
|
||||
} else if id != nil {
|
||||
filter.BranchId = id
|
||||
}
|
||||
|
||||
if raw := c.Query("user_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.UserId = &id
|
||||
if id, ok := optionalQueryID(c, "user_id", "userId"); !ok {
|
||||
return ListFilter{}, false
|
||||
} else if id != nil {
|
||||
filter.UserId = id
|
||||
}
|
||||
|
||||
if raw := c.Query("card_state_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.CardStateId = &id
|
||||
if id, ok := optionalQueryID(c, "card_state_id", "cardStateId"); !ok {
|
||||
return ListFilter{}, false
|
||||
} else if id != nil {
|
||||
filter.CardStateId = id
|
||||
}
|
||||
|
||||
if raw := c.Query("card_type_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.CardTypeId = &id
|
||||
if id, ok := optionalQueryID(c, "card_type_id", "cardTypeId"); !ok {
|
||||
return ListFilter{}, false
|
||||
} else if id != nil {
|
||||
filter.CardTypeId = id
|
||||
}
|
||||
|
||||
if raw := c.Query("page"); raw != "" {
|
||||
if raw := queryValue(c, "page"); raw != "" {
|
||||
page, err := strconv.Atoi(raw)
|
||||
if err != nil || page < 1 {
|
||||
return ListFilter{}, false
|
||||
@@ -263,21 +257,45 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.Page = page
|
||||
}
|
||||
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
if raw := queryValue(c, "limit"); raw != "" {
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit < 1 || limit > maxLimit {
|
||||
if err != nil || limit < 1 {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
if limit > maxLimit {
|
||||
limit = maxLimit
|
||||
}
|
||||
filter.Limit = limit
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("status")); raw != "" {
|
||||
if raw := queryValue(c, "status"); raw != "" {
|
||||
filter.Status = &raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("region")); raw != "" {
|
||||
if raw := queryValue(c, "region"); raw != "" {
|
||||
filter.Region = raw
|
||||
}
|
||||
|
||||
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 optionalQueryID(c *gin.Context, keys ...string) (*int64, bool) {
|
||||
raw := queryValue(c, keys...)
|
||||
if raw == "" {
|
||||
return nil, true
|
||||
}
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return &id, true
|
||||
}
|
||||
@@ -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