Enrich cards API responses with joined relations and share common helpers.
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled

Nest user/branch/cardType/cardState on card, order, pin-order, and visa-master
reads; accept flexible dates/IDs on pin create; widen pin region; ignore undefined
list query params; extract internal/cards/shared to remove duplicated types and helpers.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
guwandykandCursor committed 2026-08-26 12:46:50 +05:00
1 parent 1fd0abbcb2
commit 0098422798
27 files changed
+942 -777

No files matched your search

+118 -25
View File
@@ -6,6 +6,7 @@ import (
"strings"
"online-back/internal/cards/cardpinorders"
"online-back/internal/cards/shared"
"online-back/internal/identity/rbac"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
@@ -26,8 +27,6 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) cardpinorde
}
}
// cardPinOrderWritableColumns lists every card_pin_orders column except id, created_at and
// updated_at, in the exact order CreateCardPinOrderDTO/UpdateCardPinOrderDTO fields are read.
var cardPinOrderWritableColumns = []string{
"unique_id", "user_id", "card_type_id", "card_number", "region", "branch_id",
"customer_name", "customer_surname", "customer_patronic_name", "born_at", "phone",
@@ -41,10 +40,18 @@ var cardPinOrderColumnsSQL = strings.Join(cardPinOrderColumns, ", ")
var cardPinOrderWritableColumnsSQL = strings.Join(cardPinOrderWritableColumns, ", ")
const cardPinOrderFromClause = ` FROM card_pin_orders cpo
LEFT JOIN card_types ct ON ct.id = cpo.card_type_id
LEFT JOIN branches b ON b.id = cpo.branch_id
LEFT JOIN users u ON u.id = cpo.user_id`
var cardPinOrderSelectColumns = shared.PrefixedColumns("cpo", cardPinOrderColumns) + `,
ct.name, b.name, ` + shared.UserDisplaySQL + `, COALESCE(u.username, ''), COALESCE(u.phone, ''), COALESCE(u.email, '')`
func createArgs(dto cardpinorders.CreateCardPinOrderDTO) []interface{} {
return []interface{}{
dto.UniqueId, dto.UserId, dto.CardTypeId, dto.CardNumber, dto.Region, dto.BranchId,
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, dto.BornAt, dto.Phone,
dto.UniqueId, shared.OptionalInt64(dto.UserId), dto.CardTypeId.Int64(), dto.CardNumber, dto.Region, dto.BranchId.Int64(),
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, shared.OptionalTime(dto.BornAt), dto.Phone,
dto.PassportSerie, dto.PassportId, dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour,
dto.Status, dto.Notes,
}
@@ -52,8 +59,8 @@ func createArgs(dto cardpinorders.CreateCardPinOrderDTO) []interface{} {
func updateArgs(dto cardpinorders.UpdateCardPinOrderDTO) []interface{} {
return []interface{}{
dto.UniqueId, dto.UserId, dto.CardTypeId, dto.CardNumber, dto.Region, dto.BranchId,
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, dto.BornAt, dto.Phone,
dto.UniqueId, shared.OptionalInt64(dto.UserId), dto.CardTypeId.Int64(), dto.CardNumber, dto.Region, dto.BranchId.Int64(),
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, shared.OptionalTime(dto.BornAt), dto.Phone,
dto.PassportSerie, dto.PassportId, dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour,
dto.Status, dto.Notes,
}
@@ -62,10 +69,36 @@ func updateArgs(dto cardpinorders.UpdateCardPinOrderDTO) []interface{} {
func (r *repository) List(ctx context.Context, filter cardpinorders.ListFilter) (cardpinorders.ListCardPinOrderDTO, error) {
whereClause, args := buildListQuery(filter)
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
var count int64
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) FROM card_pin_orders cpo`+whereClause, args...).Scan(&count); err != nil {
return cardpinorders.ListCardPinOrderDTO{}, err
}
offset := (filter.Page - 1) * filter.Limit
limitPlaceholder := len(args) + 1
offsetPlaceholder := len(args) + 2
query := fmt.Sprintf(
`SELECT %s%s%s ORDER BY cpo.id LIMIT $%d OFFSET $%d`,
cardPinOrderSelectColumns, cardPinOrderFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
)
rows, err := r.client.Query(ctx, query, append(args, filter.Limit, offset)...)
if err != nil {
return cardpinorders.ListCardPinOrderDTO{}, err
}
defer rows.Close()
var items []cardpinorders.CardPinOrderDTO
for rows.Next() {
item, err := scanCardPinOrderWithJoins(rows)
if err != nil {
return cardpinorders.ListCardPinOrderDTO{}, err
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return cardpinorders.ListCardPinOrderDTO{}, err
}
return cardpinorders.ListCardPinOrderDTO{Count: count, CardPinOrders: items}, nil
}
@@ -75,29 +108,29 @@ func buildListQuery(filter cardpinorders.ListFilter) (whereClause string, args [
if filter.Status != nil {
args = append(args, *filter.Status)
conditions = append(conditions, fmt.Sprintf("status = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("cpo.status = $%d", len(args)))
}
if len(filter.BranchIds) > 0 {
conditions, args = rbac.AppendBranchIDs(conditions, args, "branch_id", filter.BranchIds)
conditions, args = rbac.AppendBranchIDs(conditions, args, "cpo.branch_id", filter.BranchIds)
} else if filter.BranchId != nil {
args = append(args, *filter.BranchId)
conditions = append(conditions, fmt.Sprintf("branch_id = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("cpo.branch_id = $%d", len(args)))
}
if filter.UserId != nil {
args = append(args, *filter.UserId)
conditions = append(conditions, fmt.Sprintf("user_id = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("cpo.user_id = $%d", len(args)))
}
if filter.CardTypeId != nil {
args = append(args, *filter.CardTypeId)
conditions = append(conditions, fmt.Sprintf("card_type_id = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("cpo.card_type_id = $%d", len(args)))
}
if filter.Region != "" {
args = append(args, filter.Region)
conditions = append(conditions, fmt.Sprintf("region = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("cpo.region = $%d", len(args)))
}
if len(conditions) == 0 {
@@ -108,7 +141,8 @@ func buildListQuery(filter cardpinorders.ListFilter) (whereClause string, args [
}
func (r *repository) GetByID(ctx context.Context, id int64) (cardpinorders.CardPinOrderDTO, error) {
return r.generic.GetByID(ctx, id)
query := `SELECT ` + cardPinOrderSelectColumns + cardPinOrderFromClause + ` WHERE cpo.id = $1`
return scanCardPinOrderWithJoins(r.client.QueryRow(ctx, query, id))
}
func (r *repository) Create(ctx context.Context, dto cardpinorders.CreateCardPinOrderDTO) (cardpinorders.CardPinOrderDTO, error) {
@@ -119,12 +153,21 @@ func (r *repository) Create(ctx context.Context, dto cardpinorders.CreateCardPin
placeholders[i] = fmt.Sprintf("$%d", i+1)
}
query := fmt.Sprintf(
`INSERT INTO card_pin_orders (%s) VALUES (%s) RETURNING %s`,
cardPinOrderWritableColumnsSQL, strings.Join(placeholders, ", "), cardPinOrderColumnsSQL,
query := fmt.Sprintf(`
WITH inserted AS (
INSERT INTO card_pin_orders (%s) VALUES (%s)
RETURNING %s
)
SELECT %s
FROM inserted cpo
LEFT JOIN card_types ct ON ct.id = cpo.card_type_id
LEFT JOIN branches b ON b.id = cpo.branch_id
LEFT JOIN users u ON u.id = cpo.user_id`,
cardPinOrderWritableColumnsSQL, strings.Join(placeholders, ", "),
cardPinOrderColumnsSQL, cardPinOrderSelectColumns,
)
return scanCardPinOrder(r.client.QueryRow(ctx, query, args...))
return scanCardPinOrderWithJoins(r.client.QueryRow(ctx, query, args...))
}
func (r *repository) Update(ctx context.Context, id int64, dto cardpinorders.UpdateCardPinOrderDTO) (cardpinorders.CardPinOrderDTO, error) {
@@ -136,12 +179,21 @@ func (r *repository) Update(ctx context.Context, id int64, dto cardpinorders.Upd
}
args = append(args, id)
query := fmt.Sprintf(
`UPDATE card_pin_orders SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') WHERE id = $%d RETURNING %s`,
strings.Join(assignments, ", "), len(args), cardPinOrderColumnsSQL,
query := fmt.Sprintf(`
WITH updated AS (
UPDATE card_pin_orders SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $%d
RETURNING %s
)
SELECT %s
FROM updated cpo
LEFT JOIN card_types ct ON ct.id = cpo.card_type_id
LEFT JOIN branches b ON b.id = cpo.branch_id
LEFT JOIN users u ON u.id = cpo.user_id`,
strings.Join(assignments, ", "), len(args), cardPinOrderColumnsSQL, cardPinOrderSelectColumns,
)
item, err := scanCardPinOrder(r.client.QueryRow(ctx, query, args...))
item, err := scanCardPinOrderWithJoins(r.client.QueryRow(ctx, query, args...))
if err != nil {
return cardpinorders.CardPinOrderDTO{}, err
}
@@ -153,16 +205,57 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
}
func scanCardPinOrder(row genericrepo.Scanner) (cardpinorders.CardPinOrderDTO, error) {
return scanCardPinOrderRow(row, false)
}
func scanCardPinOrderWithJoins(row genericrepo.Scanner) (cardpinorders.CardPinOrderDTO, error) {
return scanCardPinOrderRow(row, true)
}
func scanCardPinOrderRow(row genericrepo.Scanner, withJoins bool) (cardpinorders.CardPinOrderDTO, error) {
var item cardpinorders.CardPinOrderDTO
err := row.Scan(
dest := []interface{}{
&item.Id, &item.UniqueId, &item.UserId, &item.CardTypeId, &item.CardNumber, &item.Region, &item.BranchId,
&item.CustomerName, &item.CustomerSurname, &item.CustomerPatronicName, &item.BornAt, &item.Phone,
&item.PassportSerie, &item.PassportId, &item.PassportOne, &item.PassportTwo, &item.PassportThree, &item.PassportFour,
&item.Status, &item.Notes,
&item.CreatedAt, &item.UpdatedAt,
)
if err != nil {
}
var cardTypeNameJSON, branchNameJSON []byte
var userName, userUsername, userPhone, userEmail string
if withJoins {
dest = append(dest, &cardTypeNameJSON, &branchNameJSON, &userName, &userUsername, &userPhone, &userEmail)
}
if err := row.Scan(dest...); err != nil {
return cardpinorders.CardPinOrderDTO{}, err
}
if !withJoins {
return item, nil
}
var cardTypeName, branchName shared.LocalizedText
if err := shared.UnmarshalLocalized(cardTypeNameJSON, &cardTypeName); err != nil {
return cardpinorders.CardPinOrderDTO{}, err
}
if err := shared.UnmarshalLocalized(branchNameJSON, &branchName); err != nil {
return cardpinorders.CardPinOrderDTO{}, err
}
if item.CardTypeId != 0 {
item.CardType = &shared.NamedInfo{Id: item.CardTypeId, Name: cardTypeName}
}
if item.BranchId != 0 {
item.Branch = &shared.NamedInfo{Id: item.BranchId, Name: branchName}
}
if item.UserId != nil {
item.User = &shared.UserInfo{
Id: *item.UserId,
Name: userName,
Username: userUsername,
Phone: userPhone,
Email: userEmail,
}
}
return item, nil
}