Add region and etrap APIs for branch create form.

Expose distinct regions and etraps-by-region under provinces and branches, and seed real etraps per welayat.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-11 12:12:07 +05:00
parent 51283ffa14
commit 1987d4f596
10 changed files with 822 additions and 15 deletions

View File

@@ -42,6 +42,76 @@ func (r *repository) List(ctx context.Context, filter province.ListFilter) (prov
return province.ListProvinceDTO{Count: count, Provinces: items}, nil
}
func (r *repository) ListRegions(ctx context.Context, activeOnly bool) (province.ListRegionsDTO, error) {
query := `SELECT DISTINCT region FROM provinces`
var args []interface{}
if activeOnly {
query += ` WHERE active = $1`
args = append(args, true)
}
query += ` ORDER BY region->>'tm', region->>'en', region->>'ru'`
rows, err := r.client.Query(ctx, query, args...)
if err != nil {
return province.ListRegionsDTO{}, err
}
defer rows.Close()
regions := make([]province.LocalizedName, 0)
for rows.Next() {
var regionJSON []byte
if err := rows.Scan(&regionJSON); err != nil {
return province.ListRegionsDTO{}, err
}
var region province.LocalizedName
if err := json.Unmarshal(regionJSON, &region); err != nil {
return province.ListRegionsDTO{}, err
}
regions = append(regions, region)
}
if err := rows.Err(); err != nil {
return province.ListRegionsDTO{}, err
}
return province.ListRegionsDTO{Regions: regions}, nil
}
func (r *repository) ListEtrapsByRegion(ctx context.Context, region string, activeOnly bool) (province.ListEtrapsDTO, error) {
args := []interface{}{region}
conditions := []string{`(
LOWER(region->>'en') = LOWER($1)
OR LOWER(region->>'ru') = LOWER($1)
OR LOWER(region->>'tm') = LOWER($1)
)`}
if activeOnly {
args = append(args, true)
conditions = append(conditions, fmt.Sprintf("active = $%d", len(args)))
}
where := " WHERE " + strings.Join(conditions, " AND ")
query := `SELECT ` + provinceSelectColumns + ` FROM provinces` + where + ` ORDER BY name->>'tm', name->>'en', id`
rows, err := r.client.Query(ctx, query, args...)
if err != nil {
return province.ListEtrapsDTO{}, err
}
defer rows.Close()
etraps := make([]province.ProvinceDTO, 0)
for rows.Next() {
item, err := scanProvince(rows)
if err != nil {
return province.ListEtrapsDTO{}, err
}
etraps = append(etraps, item)
}
if err := rows.Err(); err != nil {
return province.ListEtrapsDTO{}, err
}
return province.ListEtrapsDTO{Count: int64(len(etraps)), Etraps: etraps}, nil
}
func buildListQuery(filter province.ListFilter) (whereClause string, args []interface{}) {
var conditions []string