Add visa/master payment orders API and related names on loan paid letters.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -30,7 +30,8 @@ const userDisplaySQL = `COALESCE(NULLIF(TRIM(CONCAT_WS(' ', u.first_name, u.last
|
||||
|
||||
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`
|
||||
LEFT JOIN users u ON u.id = l.user_id
|
||||
LEFT JOIN provinces p ON p.id = b.province_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.
|
||||
@@ -48,7 +49,7 @@ var letterColumnsSQL = strings.Join(letterColumns, ", ")
|
||||
|
||||
var letterWritableColumnsSQL = strings.Join(letterWritableColumns, ", ")
|
||||
|
||||
var letterSelectColumns = prefixedColumns("l", letterColumns) + `, b.name, ` + userDisplaySQL
|
||||
var letterSelectColumns = prefixedColumns("l", letterColumns) + `, p.region, b.name, COALESCE(b.unique_code, ''), b.province_id, p.name, ` + userDisplaySQL + `, COALESCE(u.username, '')`
|
||||
|
||||
func prefixedColumns(alias string, cols []string) string {
|
||||
parts := make([]string, len(cols))
|
||||
@@ -221,7 +222,8 @@ func (r *repository) Create(ctx context.Context, dto loanpaidletter.CreateLoanPa
|
||||
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`,
|
||||
LEFT JOIN users u ON u.id = l.user_id
|
||||
LEFT JOIN provinces p ON p.id = b.province_id`,
|
||||
letterWritableColumnsSQL, strings.Join(placeholders, ", "),
|
||||
letterColumnsSQL, letterSelectColumns,
|
||||
)
|
||||
@@ -247,7 +249,8 @@ func (r *repository) Update(ctx context.Context, id int64, dto loanpaidletter.Up
|
||||
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`,
|
||||
LEFT JOIN users u ON u.id = l.user_id
|
||||
LEFT JOIN provinces p ON p.id = b.province_id`,
|
||||
strings.Join(assignments, ", "), len(args), letterColumnsSQL, letterSelectColumns,
|
||||
)
|
||||
|
||||
@@ -264,7 +267,7 @@ 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
|
||||
var regionNameJSON, branchNameJSON, provinceNameJSON []byte
|
||||
|
||||
err := row.Scan(
|
||||
&item.Id, &item.UniqueId, &item.Region, &item.BranchId, &item.UserId,
|
||||
@@ -273,15 +276,27 @@ 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,
|
||||
®ionNameJSON, &branchNameJSON, &item.BranchUniqueCode, &item.ProvinceId, &provinceNameJSON,
|
||||
&item.UserName, &item.UserUsername,
|
||||
)
|
||||
if err != nil {
|
||||
return loanpaidletter.LoanPaidLetterDTO{}, err
|
||||
}
|
||||
if len(branchNameJSON) > 0 {
|
||||
if err := json.Unmarshal(branchNameJSON, &item.BranchName); err != nil {
|
||||
return loanpaidletter.LoanPaidLetterDTO{}, err
|
||||
}
|
||||
if err := unmarshalLocalized(regionNameJSON, &item.RegionName); err != nil {
|
||||
return loanpaidletter.LoanPaidLetterDTO{}, err
|
||||
}
|
||||
if err := unmarshalLocalized(branchNameJSON, &item.BranchName); err != nil {
|
||||
return loanpaidletter.LoanPaidLetterDTO{}, err
|
||||
}
|
||||
if err := unmarshalLocalized(provinceNameJSON, &item.ProvinceName); err != nil {
|
||||
return loanpaidletter.LoanPaidLetterDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func unmarshalLocalized(raw []byte, dest *loanpaidletter.LocalizedText) error {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(raw, dest)
|
||||
}
|
||||
|
||||
93
internal/lending/loanpaidletter/db/postgresql_get_test.go
Normal file
93
internal/lending/loanpaidletter/db/postgresql_get_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"online-back/internal/config"
|
||||
"online-back/internal/lending/loanpaidletter"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
)
|
||||
|
||||
func testRepo(t *testing.T) (loanpaidletter.Repository, func()) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
pool, err := postgresql.NewClient(ctx, 2, config.StorageConfig{
|
||||
Host: "localhost",
|
||||
Port: "5424",
|
||||
Database: "bank_db",
|
||||
Username: "bank_admin",
|
||||
Password: "bank@#23#@_password",
|
||||
PgPoolMaxConn: 4,
|
||||
})
|
||||
if err != nil {
|
||||
t.Skipf("postgres unavailable: %v", err)
|
||||
}
|
||||
|
||||
return NewRepository(pool, logging.GetLogger()), func() { pool.Close() }
|
||||
}
|
||||
|
||||
func TestGetByIDIncludesNamesNextToIDs(t *testing.T) {
|
||||
repo, closeRepo := testRepo(t)
|
||||
defer closeRepo()
|
||||
ctx := context.Background()
|
||||
|
||||
item, err := repo.GetByID(ctx, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByID: %v", err)
|
||||
}
|
||||
|
||||
if item.BranchId == 0 {
|
||||
t.Fatal("branchId is empty")
|
||||
}
|
||||
if item.BranchName.Tm == "" && item.BranchName.En == "" {
|
||||
t.Fatalf("branchId=%d but branchName is empty: %+v", item.BranchId, item.BranchName)
|
||||
}
|
||||
if item.BranchUniqueCode == "" {
|
||||
t.Fatalf("branchId=%d but branchUniqueCode is empty", item.BranchId)
|
||||
}
|
||||
if item.ProvinceId == nil || *item.ProvinceId == 0 {
|
||||
t.Fatal("provinceId is empty")
|
||||
}
|
||||
if item.ProvinceName.Tm == "" && item.ProvinceName.En == "" {
|
||||
t.Fatalf("provinceId=%d but provinceName is empty: %+v", *item.ProvinceId, item.ProvinceName)
|
||||
}
|
||||
if item.RegionName.Tm == "" && item.RegionName.En == "" {
|
||||
t.Fatalf("region=%q but regionName is empty: %+v", item.Region, item.RegionName)
|
||||
}
|
||||
if item.UserId != nil {
|
||||
if item.UserName == "" && item.UserUsername == "" {
|
||||
t.Fatalf("userId=%d but userName and userUsername are empty", *item.UserId)
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("id=%d branchId=%d branch=%q/%s provinceId=%d province=%q userId=%v user=%q",
|
||||
item.Id, item.BranchId, item.BranchName.Tm, item.BranchUniqueCode,
|
||||
*item.ProvinceId, item.ProvinceName.Tm, item.UserId, item.UserName)
|
||||
}
|
||||
|
||||
func TestListIncludesNamesNextToIDs(t *testing.T) {
|
||||
repo, closeRepo := testRepo(t)
|
||||
defer closeRepo()
|
||||
|
||||
list, err := repo.List(context.Background(), loanpaidletter.ListFilter{Page: 1, Limit: 10})
|
||||
if err != nil {
|
||||
t.Fatalf("List: %v", err)
|
||||
}
|
||||
if list.Count == 0 || len(list.LoanPaidLetters) == 0 {
|
||||
t.Fatal("List returned no letters")
|
||||
}
|
||||
|
||||
for _, item := range list.LoanPaidLetters {
|
||||
if item.BranchId != 0 && item.BranchName.Tm == "" && item.BranchName.En == "" {
|
||||
t.Errorf("letter %d branchId=%d missing branchName", item.Id, item.BranchId)
|
||||
}
|
||||
if item.UserId != nil && item.UserName == "" && item.UserUsername == "" {
|
||||
t.Errorf("letter %d userId=%d missing userName", item.Id, *item.UserId)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,15 @@ type LoanPaidLetterDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
UniqueId *string `json:"uniqueId"`
|
||||
Region string `json:"region"`
|
||||
RegionName LocalizedText `json:"regionName"`
|
||||
BranchId int64 `json:"branchId"`
|
||||
BranchName LocalizedText `json:"branchName"`
|
||||
BranchUniqueCode string `json:"branchUniqueCode"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
ProvinceName LocalizedText `json:"provinceName"`
|
||||
UserId *int64 `json:"userId"`
|
||||
UserName string `json:"userName"`
|
||||
UserUsername string `json:"userUsername"`
|
||||
CustomerName string `json:"customerName"`
|
||||
CustomerSurname string `json:"customerSurname"`
|
||||
CustomerPatronicName *string `json:"customerPatronicName"`
|
||||
|
||||
Reference in New Issue
Block a user