Expose GET /loan-paid-off-letters/branches?province_id= for form pickers, require loans.user_id, and proxy the Vite frontend under /online/. Co-authored-by: Cursor <cursoragent@cursor.com>
221 lines
7.2 KiB
Go
221 lines
7.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/lending/loanpaidletter"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
"online-back/pkg/repository/genericrepo"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
generic *genericrepo.Repository[loanpaidletter.LoanPaidLetterDTO]
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) loanpaidletter.Repository {
|
|
return &repository{
|
|
client: client,
|
|
generic: genericrepo.New(client, "loan_paid_off_letters", letterColumnsSQL, scanLoanPaidLetter),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// letterWritableColumns lists every loan_paid_off_letters column except id, created_at
|
|
// and updated_at, in the exact order CreateLoanPaidLetterDTO/UpdateLoanPaidLetterDTO fields are read.
|
|
var letterWritableColumns = []string{
|
|
"unique_id", "region", "branch_id", "user_id",
|
|
"customer_name", "customer_surname", "customer_patronic_name", "born_at",
|
|
"phone", "passport_serie", "passport_id",
|
|
"status", "notes", "loan_contract_number", "loan_contract_date",
|
|
"loan_amount", "loan_reason", "loan_file",
|
|
}
|
|
|
|
var letterColumns = append(append([]string{"id"}, letterWritableColumns...), "created_at", "updated_at")
|
|
|
|
var letterColumnsSQL = strings.Join(letterColumns, ", ")
|
|
|
|
var letterWritableColumnsSQL = strings.Join(letterWritableColumns, ", ")
|
|
|
|
func createArgs(dto loanpaidletter.CreateLoanPaidLetterDTO) []interface{} {
|
|
return []interface{}{
|
|
dto.UniqueId, dto.Region, dto.BranchId, dto.UserId,
|
|
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, dto.BornAt,
|
|
dto.Phone, dto.PassportSerie, dto.PassportId,
|
|
dto.Status, dto.Notes, dto.LoanContractNumber, dto.LoanContractDate,
|
|
dto.LoanAmount, dto.LoanReason, dto.LoanFile,
|
|
}
|
|
}
|
|
|
|
func updateArgs(dto loanpaidletter.UpdateLoanPaidLetterDTO) []interface{} {
|
|
return []interface{}{
|
|
dto.UniqueId, dto.Region, dto.BranchId, dto.UserId,
|
|
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, dto.BornAt,
|
|
dto.Phone, dto.PassportSerie, dto.PassportId,
|
|
dto.Status, dto.Notes, dto.LoanContractNumber, dto.LoanContractDate,
|
|
dto.LoanAmount, dto.LoanReason, dto.LoanFile,
|
|
}
|
|
}
|
|
|
|
func (r *repository) List(ctx context.Context, filter loanpaidletter.ListFilter) (loanpaidletter.ListLoanPaidLetterDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id DESC")
|
|
if err != nil {
|
|
return loanpaidletter.ListLoanPaidLetterDTO{}, err
|
|
}
|
|
|
|
return loanpaidletter.ListLoanPaidLetterDTO{Count: count, LoanPaidLetters: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter loanpaidletter.ListFilter) (whereClause string, args []interface{}) {
|
|
var conditions []string
|
|
|
|
if filter.PassportId != "" {
|
|
args = append(args, likePattern(filter.PassportId))
|
|
conditions = append(conditions, fmt.Sprintf("passport_id ILIKE $%d", len(args)))
|
|
}
|
|
|
|
if filter.Name != "" {
|
|
args = append(args, likePattern(filter.Name))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`(
|
|
customer_name ILIKE $%[1]d OR customer_surname ILIKE $%[1]d OR COALESCE(customer_patronic_name, '') ILIKE $%[1]d
|
|
)`, n))
|
|
}
|
|
|
|
if filter.Status != nil {
|
|
args = append(args, *filter.Status)
|
|
conditions = append(conditions, fmt.Sprintf("status = $%d", len(args)))
|
|
}
|
|
|
|
if filter.BranchId != nil {
|
|
args = append(args, *filter.BranchId)
|
|
conditions = append(conditions, fmt.Sprintf("branch_id = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Region != "" {
|
|
args = append(args, filter.Region)
|
|
conditions = append(conditions, fmt.Sprintf("region = $%d", len(args)))
|
|
}
|
|
|
|
if len(conditions) == 0 {
|
|
return "", args
|
|
}
|
|
|
|
return " WHERE " + strings.Join(conditions, " AND "), args
|
|
}
|
|
|
|
func likePattern(term string) string {
|
|
term = strings.ReplaceAll(term, `\`, `\\`)
|
|
term = strings.ReplaceAll(term, `%`, `\%`)
|
|
term = strings.ReplaceAll(term, `_`, `\_`)
|
|
return "%" + term + "%"
|
|
}
|
|
|
|
func (r *repository) ListBranchesByProvince(ctx context.Context, provinceID int64, activeOnly bool) (loanpaidletter.ListBranchesByProvinceDTO, error) {
|
|
args := []interface{}{provinceID}
|
|
where := ` WHERE province_id = $1`
|
|
if activeOnly {
|
|
where += ` AND active = true`
|
|
}
|
|
|
|
query := `
|
|
SELECT id, COALESCE(unique_code, ''), name, region, province_id, active
|
|
FROM branches` + where + `
|
|
ORDER BY name->>'tm', name->>'en', id`
|
|
|
|
rows, err := r.client.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return loanpaidletter.ListBranchesByProvinceDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
branches := make([]loanpaidletter.BranchByProvinceDTO, 0)
|
|
for rows.Next() {
|
|
var item loanpaidletter.BranchByProvinceDTO
|
|
var nameJSON []byte
|
|
if err := rows.Scan(&item.Id, &item.UniqueCode, &nameJSON, &item.Region, &item.ProvinceId, &item.Active); err != nil {
|
|
return loanpaidletter.ListBranchesByProvinceDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(nameJSON, &item.Name); err != nil {
|
|
return loanpaidletter.ListBranchesByProvinceDTO{}, err
|
|
}
|
|
branches = append(branches, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return loanpaidletter.ListBranchesByProvinceDTO{}, err
|
|
}
|
|
|
|
return loanpaidletter.ListBranchesByProvinceDTO{
|
|
Count: int64(len(branches)),
|
|
Branches: branches,
|
|
}, nil
|
|
}
|
|
|
|
func (r *repository) GetByID(ctx context.Context, id int64) (loanpaidletter.LoanPaidLetterDTO, error) {
|
|
return r.generic.GetByID(ctx, id)
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto loanpaidletter.CreateLoanPaidLetterDTO) (loanpaidletter.LoanPaidLetterDTO, error) {
|
|
args := createArgs(dto)
|
|
|
|
placeholders := make([]string, len(args))
|
|
for i := range args {
|
|
placeholders[i] = fmt.Sprintf("$%d", i+1)
|
|
}
|
|
|
|
query := fmt.Sprintf(
|
|
`INSERT INTO loan_paid_off_letters (%s) VALUES (%s) RETURNING %s`,
|
|
letterWritableColumnsSQL, strings.Join(placeholders, ", "), letterColumnsSQL,
|
|
)
|
|
|
|
return scanLoanPaidLetter(r.client.QueryRow(ctx, query, args...))
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto loanpaidletter.UpdateLoanPaidLetterDTO) (loanpaidletter.LoanPaidLetterDTO, error) {
|
|
args := updateArgs(dto)
|
|
|
|
assignments := make([]string, len(letterWritableColumns))
|
|
for i, column := range letterWritableColumns {
|
|
assignments[i] = fmt.Sprintf("%s = $%d", column, i+1)
|
|
}
|
|
args = append(args, id)
|
|
|
|
query := fmt.Sprintf(
|
|
`UPDATE loan_paid_off_letters SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') WHERE id = $%d RETURNING %s`,
|
|
strings.Join(assignments, ", "), len(args), letterColumnsSQL,
|
|
)
|
|
|
|
item, err := scanLoanPaidLetter(r.client.QueryRow(ctx, query, args...))
|
|
if err != nil {
|
|
return loanpaidletter.LoanPaidLetterDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
return r.generic.Delete(ctx, id)
|
|
}
|
|
|
|
func scanLoanPaidLetter(row genericrepo.Scanner) (loanpaidletter.LoanPaidLetterDTO, error) {
|
|
var item loanpaidletter.LoanPaidLetterDTO
|
|
err := row.Scan(
|
|
&item.Id, &item.UniqueId, &item.Region, &item.BranchId, &item.UserId,
|
|
&item.CustomerName, &item.CustomerSurname, &item.CustomerPatronicName, &item.BornAt,
|
|
&item.Phone, &item.PassportSerie, &item.PassportId,
|
|
&item.Status, &item.Notes, &item.LoanContractNumber, &item.LoanContractDate,
|
|
&item.LoanAmount, &item.LoanReason, &item.LoanFile,
|
|
&item.CreatedAt, &item.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return loanpaidletter.LoanPaidLetterDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|