Return role IDs on users and related names on loan APIs.
Loan orders, paid-off letters, and repayments now include user, branch, type, and document names alongside foreign-key IDs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2853bed2ec
commit
d7cdd53597
10 files changed
+659
-268
No files matched your search
@@ -26,6 +26,12 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) loanpaidlet
|
||||
}
|
||||
}
|
||||
|
||||
const userDisplaySQL = `COALESCE(NULLIF(TRIM(CONCAT_WS(' ', u.first_name, u.last_name)), ''), NULLIF(u.name, ''), u.username, '')`
|
||||
|
||||
const letterFromClause = ` FROM loan_paid_off_letters l
|
||||
LEFT JOIN branches b ON b.id = l.branch_id
|
||||
LEFT JOIN users u ON u.id = l.user_id`
|
||||
|
||||
// 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{
|
||||
@@ -42,6 +48,16 @@ var letterColumnsSQL = strings.Join(letterColumns, ", ")
|
||||
|
||||
var letterWritableColumnsSQL = strings.Join(letterWritableColumns, ", ")
|
||||
|
||||
var letterSelectColumns = prefixedColumns("l", letterColumns) + `, b.name, ` + userDisplaySQL
|
||||
|
||||
func prefixedColumns(alias string, cols []string) string {
|
||||
parts := make([]string, len(cols))
|
||||
for i, col := range cols {
|
||||
parts[i] = alias + "." + col
|
||||
}
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
func createArgs(dto loanpaidletter.CreateLoanPaidLetterDTO) []interface{} {
|
||||
return []interface{}{
|
||||
dto.UniqueId, dto.Region, dto.BranchId, dto.UserId,
|
||||
@@ -65,10 +81,36 @@ func updateArgs(dto loanpaidletter.UpdateLoanPaidLetterDTO) []interface{} {
|
||||
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")
|
||||
var count int64
|
||||
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) FROM loan_paid_off_letters l`+whereClause, args...).Scan(&count); err != nil {
|
||||
return loanpaidletter.ListLoanPaidLetterDTO{}, err
|
||||
}
|
||||
|
||||
offset := (filter.Page - 1) * filter.Limit
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
query := fmt.Sprintf(
|
||||
`SELECT %s%s%s ORDER BY l.id DESC LIMIT $%d OFFSET $%d`,
|
||||
letterSelectColumns, letterFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||
)
|
||||
|
||||
rows, err := r.client.Query(ctx, query, append(args, filter.Limit, offset)...)
|
||||
if err != nil {
|
||||
return loanpaidletter.ListLoanPaidLetterDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []loanpaidletter.LoanPaidLetterDTO
|
||||
for rows.Next() {
|
||||
item, err := scanLoanPaidLetter(rows)
|
||||
if err != nil {
|
||||
return loanpaidletter.ListLoanPaidLetterDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return loanpaidletter.ListLoanPaidLetterDTO{}, err
|
||||
}
|
||||
|
||||
return loanpaidletter.ListLoanPaidLetterDTO{Count: count, LoanPaidLetters: items}, nil
|
||||
}
|
||||
@@ -78,30 +120,30 @@ func buildListQuery(filter loanpaidletter.ListFilter) (whereClause string, args
|
||||
|
||||
if filter.PassportId != "" {
|
||||
args = append(args, likePattern(filter.PassportId))
|
||||
conditions = append(conditions, fmt.Sprintf("passport_id ILIKE $%d", len(args)))
|
||||
conditions = append(conditions, fmt.Sprintf("l.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
|
||||
l.customer_name ILIKE $%[1]d OR l.customer_surname ILIKE $%[1]d OR COALESCE(l.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)))
|
||||
conditions = append(conditions, fmt.Sprintf("l.status = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.BranchId != nil {
|
||||
args = append(args, *filter.BranchId)
|
||||
conditions = append(conditions, fmt.Sprintf("branch_id = $%d", len(args)))
|
||||
conditions = append(conditions, fmt.Sprintf("l.branch_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.Region != "" {
|
||||
args = append(args, filter.Region)
|
||||
conditions = append(conditions, fmt.Sprintf("region = $%d", len(args)))
|
||||
conditions = append(conditions, fmt.Sprintf("l.region = $%d", len(args)))
|
||||
}
|
||||
|
||||
if len(conditions) == 0 {
|
||||
@@ -159,7 +201,8 @@ func (r *repository) ListBranchesByProvince(ctx context.Context, provinceID int6
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (loanpaidletter.LoanPaidLetterDTO, error) {
|
||||
return r.generic.GetByID(ctx, id)
|
||||
query := `SELECT ` + letterSelectColumns + letterFromClause + ` WHERE l.id = $1`
|
||||
return scanLoanPaidLetter(r.client.QueryRow(ctx, query, id))
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto loanpaidletter.CreateLoanPaidLetterDTO) (loanpaidletter.LoanPaidLetterDTO, error) {
|
||||
@@ -170,9 +213,17 @@ func (r *repository) Create(ctx context.Context, dto loanpaidletter.CreateLoanPa
|
||||
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,
|
||||
query := fmt.Sprintf(`
|
||||
WITH inserted AS (
|
||||
INSERT INTO loan_paid_off_letters (%s) VALUES (%s)
|
||||
RETURNING %s
|
||||
)
|
||||
SELECT %s
|
||||
FROM inserted l
|
||||
LEFT JOIN branches b ON b.id = l.branch_id
|
||||
LEFT JOIN users u ON u.id = l.user_id`,
|
||||
letterWritableColumnsSQL, strings.Join(placeholders, ", "),
|
||||
letterColumnsSQL, letterSelectColumns,
|
||||
)
|
||||
|
||||
return scanLoanPaidLetter(r.client.QueryRow(ctx, query, args...))
|
||||
@@ -187,9 +238,17 @@ func (r *repository) Update(ctx context.Context, id int64, dto loanpaidletter.Up
|
||||
}
|
||||
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,
|
||||
query := fmt.Sprintf(`
|
||||
WITH updated AS (
|
||||
UPDATE loan_paid_off_letters SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $%d
|
||||
RETURNING %s
|
||||
)
|
||||
SELECT %s
|
||||
FROM updated l
|
||||
LEFT JOIN branches b ON b.id = l.branch_id
|
||||
LEFT JOIN users u ON u.id = l.user_id`,
|
||||
strings.Join(assignments, ", "), len(args), letterColumnsSQL, letterSelectColumns,
|
||||
)
|
||||
|
||||
item, err := scanLoanPaidLetter(r.client.QueryRow(ctx, query, args...))
|
||||
@@ -205,6 +264,8 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
|
||||
func scanLoanPaidLetter(row genericrepo.Scanner) (loanpaidletter.LoanPaidLetterDTO, error) {
|
||||
var item loanpaidletter.LoanPaidLetterDTO
|
||||
var branchNameJSON []byte
|
||||
|
||||
err := row.Scan(
|
||||
&item.Id, &item.UniqueId, &item.Region, &item.BranchId, &item.UserId,
|
||||
&item.CustomerName, &item.CustomerSurname, &item.CustomerPatronicName, &item.BornAt,
|
||||
@@ -212,9 +273,15 @@ func scanLoanPaidLetter(row genericrepo.Scanner) (loanpaidletter.LoanPaidLetterD
|
||||
&item.Status, &item.Notes, &item.LoanContractNumber, &item.LoanContractDate,
|
||||
&item.LoanAmount, &item.LoanReason, &item.LoanFile,
|
||||
&item.CreatedAt, &item.UpdatedAt,
|
||||
&branchNameJSON, &item.UserName,
|
||||
)
|
||||
if err != nil {
|
||||
return loanpaidletter.LoanPaidLetterDTO{}, err
|
||||
}
|
||||
if len(branchNameJSON) > 0 {
|
||||
if err := json.Unmarshal(branchNameJSON, &item.BranchName); err != nil {
|
||||
return loanpaidletter.LoanPaidLetterDTO{}, err
|
||||
}
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
Reference in new issue
Block a user