ch logika
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-18 11:53:37 +05:00
parent e0d03730de
commit d3e188affe
28 changed files with 405 additions and 656 deletions

View File

@@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"strings"
"online-back/internal/identity/rbac"
@@ -20,9 +19,8 @@ import (
)
// regionUniqueCodePrefix maps a branch's region code to the 2-digit prefix
// used when generating its unique_code. The suffix is 2 random digits, so
// each region can hold at most 100 branches before generateUniqueCode's
// retry budget is exhausted.
// its unique_code must start with, so a branch's code always identifies its
// region at a glance.
var regionUniqueCodePrefix = map[string]string{
"AG": "13", // Aşgabat
"AH": "23", // Ahal
@@ -32,14 +30,18 @@ var regionUniqueCodePrefix = map[string]string{
"LB": "83", // Lebap
}
const maxUniqueCodeAttempts = 20
func generateUniqueCode(region string) (string, error) {
// validateUniqueCode checks that a client-supplied unique_code starts with
// the 2-digit prefix for the given region (the DTO's "numeric,len=4" binding
// already guarantees 4 digits).
func validateUniqueCode(region, code string) error {
prefix, ok := regionUniqueCodePrefix[region]
if !ok {
return "", fmt.Errorf("unsupported region")
return fmt.Errorf("unsupported region")
}
return prefix + fmt.Sprintf("%02d", rand.Intn(100)), nil
if !strings.HasPrefix(code, prefix) {
return fmt.Errorf("unique code must start with %s for region %s", prefix, region)
}
return nil
}
type repository struct {
@@ -172,6 +174,9 @@ func (r *repository) Create(ctx context.Context, dto branches.CreateBranchDTO) (
if err := r.ensureProvince(ctx, dto.ProvinceId); err != nil {
return branches.BranchDTO{}, err
}
if err := validateUniqueCode(dto.Region, dto.UniqueCode); err != nil {
return branches.BranchDTO{}, err
}
name, address, err := marshalBranchJSON(dto.Name, dto.Address)
if err != nil {
@@ -197,26 +202,17 @@ func (r *repository) Create(ctx context.Context, dto branches.CreateBranchDTO) (
FROM inserted i
LEFT JOIN provinces p ON p.id = i.province_id`
for attempt := 0; attempt < maxUniqueCodeAttempts; attempt++ {
code, err := generateUniqueCode(dto.Region)
if err != nil {
return branches.BranchDTO{}, err
}
row := r.client.QueryRow(ctx, query,
code, name, address, dto.Region, dto.ProvinceId, dto.PhoneNumbers,
dto.BillingUsername, dto.BillingPassword, dto.BillingSberUsername, dto.BillingSberPassword,
dto.BillingVisaMasterUsername, dto.BillingVisaMasterPassword,
dto.BillingSwiftUsername, dto.BillingSwiftPassword, active,
)
item, err := scanBranch(row)
if isUniqueViolation(err) {
continue
}
return item, err
row := r.client.QueryRow(ctx, query,
dto.UniqueCode, name, address, dto.Region, dto.ProvinceId, dto.PhoneNumbers,
dto.BillingUsername, dto.BillingPassword, dto.BillingSberUsername, dto.BillingSberPassword,
dto.BillingVisaMasterUsername, dto.BillingVisaMasterPassword,
dto.BillingSwiftUsername, dto.BillingSwiftPassword, active,
)
item, err := scanBranch(row)
if isUniqueViolation(err) {
return branches.BranchDTO{}, fmt.Errorf("unique code already exists")
}
return branches.BranchDTO{}, fmt.Errorf("could not generate a unique branch code, try again")
return item, err
}
func (r *repository) Update(ctx context.Context, id int64, dto branches.UpdateBranchDTO) (branches.BranchDTO, error) {

View File

@@ -27,6 +27,7 @@ type BranchDTO struct {
}
type CreateBranchDTO struct {
UniqueCode string `json:"uniqueCode" binding:"required,numeric,len=4"`
Name LocalizedText `json:"name" binding:"required"`
Address LocalizedText `json:"address" binding:"required"`
Region string `json:"region" binding:"required"`

View File

@@ -290,8 +290,11 @@ func (h *handler) delete(c *gin.Context) {
}
func isClientError(err error) bool {
switch err.Error() {
case "province not found", "unsupported region", "could not generate a unique branch code, try again":
switch {
case err.Error() == "province not found",
err.Error() == "unsupported region",
err.Error() == "unique code already exists",
strings.HasPrefix(err.Error(), "unique code must start with"):
return true
default:
return false