ch branches
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
2026-09-30 13:48:35 +05:00
parent 6f54e9c20b
commit 4f3631c70d
4 changed files with 41 additions and 11 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB

View File

@@ -67,6 +67,8 @@ const branchSelectColumns = `b.id, b.unique_code, b.name, b.address, b.region, b
b.billing_visa_master_username, b.billing_visa_master_password,
b.billing_swift_username, b.billing_swift_password, b.active`
const branchListColumns = `b.id, b.unique_code, b.name, p.name, b.phone_numbers, b.active`
const branchReturningColumns = `id, unique_code, name, address, region, province_id, phone_numbers,
billing_username, billing_password, billing_sber_username, billing_sber_password,
billing_visa_master_username, billing_visa_master_password,
@@ -98,7 +100,7 @@ func (r *repository) List(ctx context.Context, filter branches.ListFilter) (bran
offsetPlaceholder := len(args) + 2
listQuery := fmt.Sprintf(
`SELECT %s%s%s ORDER BY b.id LIMIT $%d OFFSET $%d`,
branchSelectColumns, branchFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
branchListColumns, branchFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
)
listArgs := append(args, filter.Limit, offset)
@@ -108,9 +110,9 @@ func (r *repository) List(ctx context.Context, filter branches.ListFilter) (bran
}
defer rows.Close()
var items []branches.BranchDTO
items := []branches.BranchListItemDTO{}
for rows.Next() {
item, err := scanBranch(rows)
item, err := scanBranchListItem(rows)
if err != nil {
return branches.ListBranchDTO{}, err
}
@@ -288,6 +290,28 @@ func isUniqueViolation(err error) bool {
return errors.As(err, &pgErr) && pgErr.Code == "23505"
}
func scanBranchListItem(row genericrepo.Scanner) (branches.BranchListItemDTO, error) {
var item branches.BranchListItemDTO
var nameJSON, provinceNameJSON []byte
var uniqueCode *string
if err := row.Scan(&item.Id, &uniqueCode, &nameJSON, &provinceNameJSON, &item.PhoneNumbers, &item.Active); err != nil {
return branches.BranchListItemDTO{}, err
}
if uniqueCode != nil {
item.UniqueCode = *uniqueCode
}
if err := json.Unmarshal(nameJSON, &item.Name); err != nil {
return branches.BranchListItemDTO{}, err
}
if provinceNameJSON != nil {
if err := json.Unmarshal(provinceNameJSON, &item.ProvinceName); err != nil {
return branches.BranchListItemDTO{}, err
}
}
return item, nil
}
func scanBranch(row genericrepo.Scanner) (branches.BranchDTO, error) {
var item branches.BranchDTO
var nameJSON, addressJSON, provinceNameJSON []byte

View File

@@ -71,7 +71,18 @@ type ListFilter struct {
Limit int
}
type ListBranchDTO struct {
Count int64 `json:"count"`
Branches []BranchDTO `json:"branches"`
// BranchListItemDTO is the list row; address, region and billing credentials
// come from GET /branches/:id.
type BranchListItemDTO struct {
Id int64 `json:"id"`
UniqueCode string `json:"uniqueCode"`
Name LocalizedText `json:"name"`
ProvinceName LocalizedText `json:"provinceName"`
PhoneNumbers string `json:"phoneNumbers"`
Active bool `json:"active"`
}
type ListBranchDTO struct {
Count int64 `json:"count"`
Branches []BranchListItemDTO `json:"branches"`
}

View File

@@ -70,11 +70,6 @@ func (h *handler) list(c *gin.Context) {
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
if !rbac.HasAllScope(c) {
for i := range result.Branches {
clearBranchSecrets(&result.Branches[i])
}
}
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
}