From 810c0e5eff52ef743f900630741cff5b7052b8f6 Mon Sep 17 00:00:00 2001 From: M_Kececi Date: Thu, 25 Jun 2026 17:24:42 +0300 Subject: [PATCH] ui: add B2B olmayan stok (orphans) page --- svc/routes/product_series.go | 53 ++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/svc/routes/product_series.go b/svc/routes/product_series.go index 935ac32..acac46a 100644 --- a/svc/routes/product_series.go +++ b/svc/routes/product_series.go @@ -107,11 +107,43 @@ func PostProductSeriesDefinitionHandler(pg *sql.DB) http.HandlerFunc { ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second) defer cancel() err := pg.QueryRowContext(ctx, ` -INSERT INTO dfgrp (code, title, is_active, typ, master, parent_filter, sort_order, is_required, notes) -VALUES ($1, $2, COALESCE($3, TRUE), 'opt', 'zbggseri', $4, $5, FALSE, $6) -RETURNING id +WITH updated AS ( + UPDATE dfgrp + SET title=$2, + is_active=COALESCE($3, TRUE), + parent_filter=$4, + sort_order=$5, + notes=$6 + WHERE master='zbggseri' AND code=$1 + RETURNING id +), +inserted AS ( + INSERT INTO dfgrp (code, title, is_active, typ, master, parent_filter, sort_order, is_required, notes) + SELECT $1, $2, COALESCE($3, TRUE), 'opt', 'zbggseri', $4, $5, FALSE, $6 + WHERE NOT EXISTS (SELECT 1 FROM updated) + RETURNING id +) +SELECT id FROM updated +UNION ALL +SELECT id FROM inserted +LIMIT 1 `, req.Code, req.Title, req.IsActive, req.ParentFilter, req.SortOrder, req.Notes).Scan(&req.ID) if err != nil { + if isPostgresUniqueViolation(err, "ndx_dfgrp_uq4") { + if selErr := pg.QueryRowContext(ctx, ` +UPDATE dfgrp +SET title=$2, + is_active=COALESCE($3, TRUE), + parent_filter=$4, + sort_order=$5, + notes=$6 +WHERE master='zbggseri' AND code=$1 +RETURNING id +`, req.Code, req.Title, req.IsActive, req.ParentFilter, req.SortOrder, req.Notes).Scan(&req.ID); selErr == nil { + writeJSON(w, req) + return + } + } http.Error(w, "Seri tanimi eklenemedi: "+err.Error(), http.StatusInternalServerError) return } @@ -151,6 +183,10 @@ SET code=$2, WHERE id=$1 AND master='zbggseri' `, id, req.Code, req.Title, req.IsActive, req.ParentFilter, req.SortOrder, req.Notes) if err != nil { + if isPostgresUniqueViolation(err, "ndx_dfgrp_uq4") { + http.Error(w, "Bu seri kodu zaten var. Mevcut seri satirini duzenleyin veya farkli kod kullanin.", http.StatusConflict) + return + } http.Error(w, "Seri tanimi guncellenemedi: "+err.Error(), http.StatusInternalServerError) return } @@ -1155,3 +1191,14 @@ func writeJSON(w http.ResponseWriter, payload any) { w.Header().Set("Content-Type", "application/json; charset=utf-8") _ = json.NewEncoder(w).Encode(payload) } + +func isPostgresUniqueViolation(err error, constraint string) bool { + if err == nil { + return false + } + pqErr, ok := err.(*pq.Error) + if !ok { + return false + } + return string(pqErr.Code) == "23505" && (constraint == "" || pqErr.Constraint == constraint) +}