Enrich cards API responses with joined relations and share common helpers.
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:
1 parent
1fd0abbcb2
commit
0098422798
27 files changed
+942
-777
No files matched your search
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"online-back/internal/cards/cardorder"
|
||||
"online-back/internal/cards/shared"
|
||||
"online-back/internal/identity/rbac"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
@@ -41,6 +41,15 @@ var cardOrderColumnsSQL = strings.Join(cardOrderColumns, ", ")
|
||||
|
||||
var cardOrderWritableColumnsSQL = strings.Join(cardOrderWritableColumns, ", ")
|
||||
|
||||
const cardOrderFromClause = ` FROM card_orders co
|
||||
LEFT JOIN card_states cs ON cs.id = co.card_state_id
|
||||
LEFT JOIN card_types ct ON ct.id = co.card_type_id
|
||||
LEFT JOIN branches b ON b.id = co.branch_id
|
||||
LEFT JOIN users u ON u.id = co.user_id`
|
||||
|
||||
var cardOrderSelectColumns = shared.PrefixedColumns("co", cardOrderColumns) + `,
|
||||
cs.name, ct.name, b.name, ` + shared.UserDisplaySQL + `, COALESCE(u.username, ''), COALESCE(u.phone, ''), COALESCE(u.email, '')`
|
||||
|
||||
func resolveStatus(status *string) *string {
|
||||
if status == nil || strings.TrimSpace(*status) == "" {
|
||||
pending := "pending"
|
||||
@@ -59,46 +68,56 @@ func resolvePaid(paid *bool) bool {
|
||||
func createArgs(dto cardorder.CreateCardOrderDTO) []interface{} {
|
||||
return []interface{}{
|
||||
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.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, shared.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, optionalInt64(dto.UserId), resolvePaid(dto.Paid),
|
||||
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, shared.OptionalInt64(dto.UserId), resolvePaid(dto.Paid),
|
||||
}
|
||||
}
|
||||
|
||||
func updateArgs(dto cardorder.UpdateCardOrderDTO) []interface{} {
|
||||
return []interface{}{
|
||||
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.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, shared.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, optionalInt64(dto.UserId), dto.Paid,
|
||||
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, shared.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)
|
||||
|
||||
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_orders co`+whereClause, args...).Scan(&count); err != nil {
|
||||
return cardorder.ListCardOrderDTO{}, err
|
||||
}
|
||||
|
||||
offset := (filter.Page - 1) * filter.Limit
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
query := fmt.Sprintf(
|
||||
`SELECT %s%s%s ORDER BY co.id LIMIT $%d OFFSET $%d`,
|
||||
cardOrderSelectColumns, cardOrderFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||
)
|
||||
|
||||
rows, err := r.client.Query(ctx, query, append(args, filter.Limit, offset)...)
|
||||
if err != nil {
|
||||
return cardorder.ListCardOrderDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []cardorder.CardOrderDTO
|
||||
for rows.Next() {
|
||||
item, err := scanCardOrderWithJoins(rows)
|
||||
if err != nil {
|
||||
return cardorder.ListCardOrderDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return cardorder.ListCardOrderDTO{}, err
|
||||
}
|
||||
|
||||
return cardorder.ListCardOrderDTO{Count: count, CardOrders: items}, nil
|
||||
}
|
||||
@@ -108,34 +127,34 @@ func buildListQuery(filter cardorder.ListFilter) (whereClause string, args []int
|
||||
|
||||
if filter.Status != nil {
|
||||
args = append(args, *filter.Status)
|
||||
conditions = append(conditions, fmt.Sprintf("status = $%d", len(args)))
|
||||
conditions = append(conditions, fmt.Sprintf("co.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, "co.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("co.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("co.user_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.CardStateId != nil {
|
||||
args = append(args, *filter.CardStateId)
|
||||
conditions = append(conditions, fmt.Sprintf("card_state_id = $%d", len(args)))
|
||||
conditions = append(conditions, fmt.Sprintf("co.card_state_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("co.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("co.region = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
@@ -146,7 +165,8 @@ func buildListQuery(filter cardorder.ListFilter) (whereClause string, args []int
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (cardorder.CardOrderDTO, error) {
|
||||
return r.generic.GetByID(ctx, id)
|
||||
query := `SELECT ` + cardOrderSelectColumns + cardOrderFromClause + ` WHERE co.id = $1`
|
||||
return scanCardOrderWithJoins(r.client.QueryRow(ctx, query, id))
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto cardorder.CreateCardOrderDTO) (cardorder.CardOrderDTO, error) {
|
||||
@@ -157,12 +177,22 @@ func (r *repository) Create(ctx context.Context, dto cardorder.CreateCardOrderDT
|
||||
placeholders[i] = fmt.Sprintf("$%d", i+1)
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(
|
||||
`INSERT INTO card_orders (%s) VALUES (%s) RETURNING %s`,
|
||||
cardOrderWritableColumnsSQL, strings.Join(placeholders, ", "), cardOrderColumnsSQL,
|
||||
query := fmt.Sprintf(`
|
||||
WITH inserted AS (
|
||||
INSERT INTO card_orders (%s) VALUES (%s)
|
||||
RETURNING %s
|
||||
)
|
||||
SELECT %s
|
||||
FROM inserted co
|
||||
LEFT JOIN card_states cs ON cs.id = co.card_state_id
|
||||
LEFT JOIN card_types ct ON ct.id = co.card_type_id
|
||||
LEFT JOIN branches b ON b.id = co.branch_id
|
||||
LEFT JOIN users u ON u.id = co.user_id`,
|
||||
cardOrderWritableColumnsSQL, strings.Join(placeholders, ", "),
|
||||
cardOrderColumnsSQL, cardOrderSelectColumns,
|
||||
)
|
||||
|
||||
return scanCardOrder(r.client.QueryRow(ctx, query, args...))
|
||||
return scanCardOrderWithJoins(r.client.QueryRow(ctx, query, args...))
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto cardorder.UpdateCardOrderDTO) (cardorder.CardOrderDTO, error) {
|
||||
@@ -174,12 +204,22 @@ func (r *repository) Update(ctx context.Context, id int64, dto cardorder.UpdateC
|
||||
}
|
||||
args = append(args, id)
|
||||
|
||||
query := fmt.Sprintf(
|
||||
`UPDATE card_orders SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') WHERE id = $%d RETURNING %s`,
|
||||
strings.Join(assignments, ", "), len(args), cardOrderColumnsSQL,
|
||||
query := fmt.Sprintf(`
|
||||
WITH updated AS (
|
||||
UPDATE card_orders SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $%d
|
||||
RETURNING %s
|
||||
)
|
||||
SELECT %s
|
||||
FROM updated co
|
||||
LEFT JOIN card_states cs ON cs.id = co.card_state_id
|
||||
LEFT JOIN card_types ct ON ct.id = co.card_type_id
|
||||
LEFT JOIN branches b ON b.id = co.branch_id
|
||||
LEFT JOIN users u ON u.id = co.user_id`,
|
||||
strings.Join(assignments, ", "), len(args), cardOrderColumnsSQL, cardOrderSelectColumns,
|
||||
)
|
||||
|
||||
item, err := scanCardOrder(r.client.QueryRow(ctx, query, args...))
|
||||
item, err := scanCardOrderWithJoins(r.client.QueryRow(ctx, query, args...))
|
||||
if err != nil {
|
||||
return cardorder.CardOrderDTO{}, err
|
||||
}
|
||||
@@ -191,17 +231,67 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
}
|
||||
|
||||
func scanCardOrder(row genericrepo.Scanner) (cardorder.CardOrderDTO, error) {
|
||||
return scanCardOrderRow(row, false)
|
||||
}
|
||||
|
||||
func scanCardOrderWithJoins(row genericrepo.Scanner) (cardorder.CardOrderDTO, error) {
|
||||
return scanCardOrderRow(row, true)
|
||||
}
|
||||
|
||||
func scanCardOrderRow(row genericrepo.Scanner, withJoins bool) (cardorder.CardOrderDTO, error) {
|
||||
var item cardorder.CardOrderDTO
|
||||
err := row.Scan(
|
||||
dest := []interface{}{
|
||||
&item.Id, &item.UniqueId, &item.CardStateId, &item.CardTypeId, &item.Region, &item.BranchId,
|
||||
&item.CustomerName, &item.CustomerSurname, &item.CustomerPatronicName, &item.BornAt, &item.OldSurname, &item.Citizenship,
|
||||
&item.PassportSerie, &item.PassportId, &item.PassportGivenAt, &item.PassportGivenBy, &item.BornPlace,
|
||||
&item.JobLocation, &item.PassportAddress, &item.RealAddress, &item.Phone, &item.PhoneAdditional,
|
||||
&item.Status, &item.PassportOne, &item.PassportTwo, &item.PassportThree, &item.PassportFour, &item.Notes, &item.UserId, &item.Paid,
|
||||
&item.CreatedAt, &item.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
var cardStateNameJSON, cardTypeNameJSON, branchNameJSON []byte
|
||||
var userName, userUsername, userPhone, userEmail string
|
||||
if withJoins {
|
||||
dest = append(dest,
|
||||
&cardStateNameJSON, &cardTypeNameJSON, &branchNameJSON,
|
||||
&userName, &userUsername, &userPhone, &userEmail,
|
||||
)
|
||||
}
|
||||
|
||||
if err := row.Scan(dest...); err != nil {
|
||||
return cardorder.CardOrderDTO{}, err
|
||||
}
|
||||
if !withJoins {
|
||||
return item, nil
|
||||
}
|
||||
|
||||
var cardStateName, cardTypeName, branchName shared.LocalizedText
|
||||
if err := shared.UnmarshalLocalized(cardStateNameJSON, &cardStateName); err != nil {
|
||||
return cardorder.CardOrderDTO{}, err
|
||||
}
|
||||
if err := shared.UnmarshalLocalized(cardTypeNameJSON, &cardTypeName); err != nil {
|
||||
return cardorder.CardOrderDTO{}, err
|
||||
}
|
||||
if err := shared.UnmarshalLocalized(branchNameJSON, &branchName); err != nil {
|
||||
return cardorder.CardOrderDTO{}, err
|
||||
}
|
||||
if item.CardStateId != 0 {
|
||||
item.CardState = &shared.NamedInfo{Id: item.CardStateId, Name: cardStateName}
|
||||
}
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user