added soft deleted
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:
guwandyk committed 2026-09-05 17:58:23 +05:00
1 parent d1a6fcaadc
commit e0d03730de
36 files changed
+327 -217

No files matched your search

+32 -19
View File
@@ -76,7 +76,7 @@ func resolveSource(source string) string {
func createArgs(dto loanorders.CreateLoanOrderDTO) []interface{} {
return []interface{}{
dto.UniqueId, resolveSource(dto.Source), shared.OptionalInt64(dto.UserId), dto.LoanType.Int64(), dto.Region, dto.BranchId.Int64(),
resolveSource(dto.Source), shared.OptionalInt64(dto.UserId), dto.LoanType.Int64(), dto.Region, dto.BranchId.Int64(),
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName,
dto.PassportAddress, dto.RealAddress, dto.PassportSerie, dto.PassportId, dto.PassportGivenAt.Time(), dto.PassportGivenBy,
dto.BornPlace, dto.BornAt.Time(), dto.Email, dto.Phone, dto.PhoneAdditional, dto.PhoneHome,
@@ -95,7 +95,7 @@ func createArgs(dto loanorders.CreateLoanOrderDTO) []interface{} {
func updateArgs(dto loanorders.UpdateLoanOrderDTO) []interface{} {
return []interface{}{
dto.UniqueId, resolveSource(dto.Source), shared.OptionalInt64(dto.UserId), dto.LoanType.Int64(), dto.Region, dto.BranchId.Int64(),
resolveSource(dto.Source), shared.OptionalInt64(dto.UserId), dto.LoanType.Int64(), dto.Region, dto.BranchId.Int64(),
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName,
dto.PassportAddress, dto.RealAddress, dto.PassportSerie, dto.PassportId, dto.PassportGivenAt.Time(), dto.PassportGivenBy,
dto.BornPlace, dto.BornAt.Time(), dto.Email, dto.Phone, dto.PhoneAdditional, dto.PhoneHome,
@@ -150,7 +150,7 @@ func (r *repository) List(ctx context.Context, filter loanorders.ListFilter) (lo
}
func buildListQuery(filter loanorders.ListFilter) (whereClause string, args []interface{}) {
var conditions []string
conditions := []string{"lo.deleted_at IS NULL"}
if filter.Status != nil {
args = append(args, *filter.Status)
@@ -179,33 +179,42 @@ func buildListQuery(filter loanorders.ListFilter) (whereClause string, args []in
conditions = append(conditions, fmt.Sprintf("lo.source = $%d", len(args)))
}
if len(conditions) == 0 {
return "", args
}
return " WHERE " + strings.Join(conditions, " AND "), args
}
func (r *repository) GetByID(ctx context.Context, id int64) (loanorders.LoanOrderDTO, error) {
query := `SELECT ` + loanOrderSelectColumns + loanOrderFromClause + ` WHERE lo.id = $1`
query := `SELECT ` + loanOrderSelectColumns + loanOrderFromClause + ` WHERE lo.id = $1 AND lo.deleted_at IS NULL`
return scanLoanOrder(r.client.QueryRow(ctx, query, id))
}
// nextUniqueIDSQL computes unique_id as the last numeric value stored in
// loan_orders plus one, zero-padded to 4 digits. Non-numeric rows (e.g. legacy
// "LO-2026-0001") are ignored so CREATE does not fail with a cast error.
const nextUniqueIDSQL = `(SELECT LPAD((COALESCE(MAX(CASE WHEN unique_id ~ '^[0-9]+$' THEN unique_id::bigint END), 0) + 1)::text, 4, '0') FROM loan_orders)`
// nextUniqueIDExprTemplate builds unique_id as "TB" + the referenced branch's
// unique_code + "-" + the last numeric suffix stored in loan_orders plus one,
// zero-padded to 4 digits, e.g. "TB1303-8027". substring(... from '([0-9]+)$')
// pulls the trailing digit run regardless of prefix, so both legacy plain rows
// ("0008") and new "TB<code>-NNNN" rows are accounted for when computing the
// next value. The single %s is filled in with the placeholder bound to the
// target branch id.
const nextUniqueIDExprTemplate = `(
SELECT 'TB' || COALESCE((SELECT unique_code FROM branches WHERE id = %s), '') || '-' ||
LPAD((SELECT COALESCE(MAX(substring(unique_id from '([0-9]+)$')::bigint), 0) + 1 FROM loan_orders)::text, 4, '0')
)`
func (r *repository) Create(ctx context.Context, dto loanorders.CreateLoanOrderDTO) (loanorders.LoanOrderDTO, error) {
// unique_id is supplied positionally via nextUniqueIDSQL below, so drop it
// from the bound args to avoid double-supplying the first column.
args := createArgs(dto)[1:]
// unique_id is supplied positionally via nextUniqueIDExpr below, so it's not
// part of createArgs; the target branch id is bound a second time (as its
// own dedicated trailing placeholder) so that subquery doesn't need to guess
// which $N slot branch_id already occupies among the other bound columns.
args := createArgs(dto)
placeholders := make([]string, len(args))
for i := range args {
placeholders[i] = fmt.Sprintf("$%d", i+1)
}
branchIDPlaceholder := fmt.Sprintf("$%d", len(args)+1)
args = append(args, dto.BranchId.Int64())
nextUniqueIDExpr := fmt.Sprintf(nextUniqueIDExprTemplate, branchIDPlaceholder)
query := fmt.Sprintf(`
WITH inserted AS (
INSERT INTO loan_orders (%s) VALUES (%s, %s)
@@ -218,7 +227,7 @@ func (r *repository) Create(ctx context.Context, dto loanorders.CreateLoanOrderD
LEFT JOIN users u ON u.id = lo.user_id
LEFT JOIN provinces p ON p.id = lo.work_province_id
LEFT JOIN loan_order_required_docs d ON d.id = lo.loan_order_required_doc_id`,
loanOrderWritableColumnsSQL, nextUniqueIDSQL, strings.Join(placeholders, ", "),
loanOrderWritableColumnsSQL, nextUniqueIDExpr, strings.Join(placeholders, ", "),
loanOrderColumnsSQL, loanOrderSelectColumns,
)
@@ -228,8 +237,12 @@ func (r *repository) Create(ctx context.Context, dto loanorders.CreateLoanOrderD
func (r *repository) Update(ctx context.Context, id int64, dto loanorders.UpdateLoanOrderDTO) (loanorders.LoanOrderDTO, error) {
args := updateArgs(dto)
assignments := make([]string, len(loanOrderWritableColumns))
for i, column := range loanOrderWritableColumns {
// unique_id is assigned once at creation and never touched again, so it's
// excluded here (index 0 of loanOrderWritableColumns) — including it would
// overwrite the assigned reference number with NULL on every update.
writableColumns := loanOrderWritableColumns[1:]
assignments := make([]string, len(writableColumns))
for i, column := range writableColumns {
assignments[i] = fmt.Sprintf("%s = $%d", column, i+1)
}
args = append(args, id)
@@ -237,7 +250,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto loanorders.Update
query := fmt.Sprintf(`
WITH updated AS (
UPDATE loan_orders SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $%d
WHERE id = $%d AND deleted_at IS NULL
RETURNING %s
)
SELECT %s