Align loan type and required document schema with updated column names.

Rename loan_required_documents to loan_order_required_docs, simplify document fields to name/value, and map loan_types columns to name, maturity, notes, and active.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
guwandykandCursor committed 2026-07-06 22:48:19 +05:00
1 parent 08be61cbcd
commit ef56625321
9 files changed
+66 -89

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
DROP TABLE IF EXISTS loan_required_documents; DROP TABLE IF EXISTS loan_order_required_docs;
DROP TABLE IF EXISTS loan_types; DROP TABLE IF EXISTS loan_types;
DROP TABLE IF EXISTS reasons_cards; DROP TABLE IF EXISTS reasons_cards;
DROP TABLE IF EXISTS payment_cards; DROP TABLE IF EXISTS payment_cards;
+12 -13
View File
@@ -75,20 +75,19 @@ CREATE TABLE IF NOT EXISTS reasons_cards (
CREATE TABLE IF NOT EXISTS loan_types ( CREATE TABLE IF NOT EXISTS loan_types (
id bigserial PRIMARY KEY, id bigserial PRIMARY KEY,
title json NOT NULL DEFAULT '{}'::json, name json NOT NULL DEFAULT '{}'::json,
tax numeric(12, 2) NOT NULL DEFAULT 0, tax numeric(12, 2) NOT NULL DEFAULT 0,
loan_term character varying(250) NOT NULL DEFAULT '', maturity character varying(250) NOT NULL DEFAULT '',
content json NOT NULL DEFAULT '{}'::json, notes json NOT NULL DEFAULT '{}'::json,
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, active boolean NOT NULL DEFAULT true,
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
); updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
);
CREATE TABLE IF NOT EXISTS loan_required_documents (
id bigserial PRIMARY KEY, CREATE TABLE IF NOT EXISTS loan_order_required_docs (
title json NOT NULL DEFAULT '{}'::json, id bigserial PRIMARY KEY,
content_en text NOT NULL DEFAULT '', name text NOT NULL DEFAULT '',
content_ru text NOT NULL DEFAULT '', value text NOT NULL DEFAULT '',
content_tm text NOT NULL DEFAULT '',
active boolean NOT NULL DEFAULT true, active boolean NOT NULL DEFAULT true,
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL, created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
@@ -1,4 +1,4 @@
ALTER TABLE loan_required_documents RENAME COLUMN active TO status; ALTER TABLE loan_order_required_docs RENAME COLUMN active TO status;
ALTER TABLE payment_cards RENAME COLUMN active TO status; ALTER TABLE payment_cards RENAME COLUMN active TO status;
ALTER TABLE provinces RENAME COLUMN active TO status; ALTER TABLE provinces RENAME COLUMN active TO status;
ALTER TABLE users RENAME COLUMN active TO status; ALTER TABLE users RENAME COLUMN active TO status;
@@ -1,4 +1,4 @@
ALTER TABLE users RENAME COLUMN status TO active; ALTER TABLE users RENAME COLUMN status TO active;
ALTER TABLE provinces RENAME COLUMN status TO active; ALTER TABLE provinces RENAME COLUMN status TO active;
ALTER TABLE payment_cards RENAME COLUMN status TO active; ALTER TABLE payment_cards RENAME COLUMN status TO active;
ALTER TABLE loan_required_documents RENAME COLUMN status TO active; ALTER TABLE loan_order_required_docs RENAME COLUMN status TO active;
+15 -33
View File
@@ -2,7 +2,6 @@ package db
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@@ -23,9 +22,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) loansdocume
return &repository{client: client, logger: logger} return &repository{client: client, logger: logger}
} }
const documentColumns = `d.id, d.title, d.content_en, d.content_ru, d.content_tm, d.active, d.created_at, d.updated_at` const documentColumns = `d.id, d.name, d.value, d.active, d.created_at, d.updated_at`
const documentReturningColumns = `id, title, content_en, content_ru, content_tm, active, created_at, updated_at` const documentReturningColumns = `id, name, value, active, created_at, updated_at`
func (r *repository) List(ctx context.Context, filter loansdocument.ListFilter) (loansdocument.ListLoanDocumentDTO, error) { func (r *repository) List(ctx context.Context, filter loansdocument.ListFilter) (loansdocument.ListLoanDocumentDTO, error) {
fromClause, whereClause, args := buildListQuery(filter) fromClause, whereClause, args := buildListQuery(filter)
@@ -65,16 +64,14 @@ func (r *repository) List(ctx context.Context, filter loansdocument.ListFilter)
} }
func buildListQuery(filter loansdocument.ListFilter) (fromClause, whereClause string, args []interface{}) { func buildListQuery(filter loansdocument.ListFilter) (fromClause, whereClause string, args []interface{}) {
fromClause = ` FROM loan_required_documents d` fromClause = ` FROM loan_order_required_docs d`
var conditions []string var conditions []string
if filter.DocumentName != "" { if filter.DocumentName != "" {
args = append(args, likePattern(filter.DocumentName)) args = append(args, likePattern(filter.DocumentName))
n := len(args) n := len(args)
conditions = append(conditions, fmt.Sprintf(`( conditions = append(conditions, fmt.Sprintf(`d.name ILIKE $%d`, n))
d.title->>'en' ILIKE $%[1]d OR d.title->>'ru' ILIKE $%[1]d OR d.title->>'tm' ILIKE $%[1]d
)`, n))
} }
if filter.Active != nil { if filter.Active != nil {
@@ -97,7 +94,7 @@ func likePattern(term string) string {
} }
func (r *repository) GetByID(ctx context.Context, id int64) (loansdocument.LoanDocumentDTO, error) { func (r *repository) GetByID(ctx context.Context, id int64) (loansdocument.LoanDocumentDTO, error) {
query := fmt.Sprintf(`SELECT %s FROM loan_required_documents d WHERE d.id = $1`, documentColumns) query := fmt.Sprintf(`SELECT %s FROM loan_order_required_docs d WHERE d.id = $1`, documentColumns)
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, id)) item, err := scanLoanDocument(r.client.QueryRow(ctx, query, id))
if errors.Is(err, pgx.ErrNoRows) { if errors.Is(err, pgx.ErrNoRows) {
return loansdocument.LoanDocumentDTO{}, err return loansdocument.LoanDocumentDTO{}, err
@@ -106,38 +103,28 @@ func (r *repository) GetByID(ctx context.Context, id int64) (loansdocument.LoanD
} }
func (r *repository) Create(ctx context.Context, dto loansdocument.CreateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) { func (r *repository) Create(ctx context.Context, dto loansdocument.CreateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) {
title, err := json.Marshal(dto.Title)
if err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
active := true active := true
if dto.Active != nil { if dto.Active != nil {
active = *dto.Active active = *dto.Active
} }
query := fmt.Sprintf(` query := fmt.Sprintf(`
INSERT INTO loan_required_documents (title, content_en, content_ru, content_tm, active) INSERT INTO loan_order_required_docs (name, value, active)
VALUES ($1, $2, $3, $4, $5) VALUES ($1, $2, $3)
RETURNING %s`, documentReturningColumns) RETURNING %s`, documentReturningColumns)
return scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, active)) return scanLoanDocument(r.client.QueryRow(ctx, query, dto.Name, dto.Value, active))
} }
func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.UpdateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) { func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.UpdateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) {
title, err := json.Marshal(dto.Title)
if err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
query := fmt.Sprintf(` query := fmt.Sprintf(`
UPDATE loan_required_documents SET UPDATE loan_order_required_docs SET
title = $1, content_en = $2, content_ru = $3, content_tm = $4, active = $5, name = $1, value = $2, active = $3,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $6 WHERE id = $4
RETURNING %s`, documentReturningColumns) RETURNING %s`, documentReturningColumns)
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, dto.Active, id)) item, err := scanLoanDocument(r.client.QueryRow(ctx, query, dto.Name, dto.Value, dto.Active, id))
if errors.Is(err, pgx.ErrNoRows) { if errors.Is(err, pgx.ErrNoRows) {
return loansdocument.LoanDocumentDTO{}, err return loansdocument.LoanDocumentDTO{}, err
} }
@@ -146,7 +133,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.Upd
func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (loansdocument.LoanDocumentDTO, error) { func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (loansdocument.LoanDocumentDTO, error) {
query := fmt.Sprintf(` query := fmt.Sprintf(`
UPDATE loan_required_documents SET UPDATE loan_order_required_docs SET
active = $1, active = $1,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $2 WHERE id = $2
@@ -160,7 +147,7 @@ func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (l
} }
func (r *repository) Delete(ctx context.Context, id int64) error { func (r *repository) Delete(ctx context.Context, id int64) error {
tag, err := r.client.Exec(ctx, `DELETE FROM loan_required_documents WHERE id = $1`, id) tag, err := r.client.Exec(ctx, `DELETE FROM loan_order_required_docs WHERE id = $1`, id)
if err != nil { if err != nil {
return err return err
} }
@@ -176,16 +163,11 @@ type scannable interface {
func scanLoanDocument(row scannable) (loansdocument.LoanDocumentDTO, error) { func scanLoanDocument(row scannable) (loansdocument.LoanDocumentDTO, error) {
var item loansdocument.LoanDocumentDTO var item loansdocument.LoanDocumentDTO
var titleJSON []byte
if err := row.Scan( if err := row.Scan(
&item.Id, &titleJSON, &item.ContentEn, &item.ContentRu, &item.ContentTm, &item.Id, &item.Name, &item.Value, &item.Active, &item.CreatedAt, &item.UpdatedAt,
&item.Active, &item.CreatedAt, &item.UpdatedAt,
); err != nil { ); err != nil {
return loansdocument.LoanDocumentDTO{}, err return loansdocument.LoanDocumentDTO{}, err
} }
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
return item, nil return item, nil
} }
+12 -24
View File
@@ -2,37 +2,25 @@ package loansdocument
import "time" import "time"
type LocalizedTitle struct {
En string `json:"en"`
Ru string `json:"ru"`
Tm string `json:"tm"`
}
type LoanDocumentDTO struct { type LoanDocumentDTO struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Title LocalizedTitle `json:"title"` Name string `json:"name"`
ContentEn string `json:"contentEn"` Value string `json:"value"`
ContentRu string `json:"contentRu"` Active bool `json:"active"`
ContentTm string `json:"contentTm"` CreatedAt time.Time `json:"createdAt"`
Active bool `json:"active"` UpdatedAt time.Time `json:"updatedAt"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
} }
type CreateLoanDocumentDTO struct { type CreateLoanDocumentDTO struct {
Title LocalizedTitle `json:"title" binding:"required"` Name string `json:"name" binding:"required"`
ContentEn string `json:"contentEn" binding:"required"` Value string `json:"value" binding:"required"`
ContentRu string `json:"contentRu" binding:"required"` Active *bool `json:"active"`
ContentTm string `json:"contentTm" binding:"required"`
Active *bool `json:"active"`
} }
type UpdateLoanDocumentDTO struct { type UpdateLoanDocumentDTO struct {
Title LocalizedTitle `json:"title" binding:"required"` Name string `json:"name" binding:"required"`
ContentEn string `json:"contentEn" binding:"required"` Value string `json:"value" binding:"required"`
ContentRu string `json:"contentRu" binding:"required"` Active bool `json:"active"`
ContentTm string `json:"contentTm" binding:"required"`
Active bool `json:"active"`
} }
type UpdateLoanDocumentActiveDTO struct { type UpdateLoanDocumentActiveDTO struct {
+1 -1
View File
@@ -41,7 +41,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
// @Tags loan-document // @Tags loan-document
// @Produce json // @Produce json
// @Security BearerAuth // @Security BearerAuth
// @Param document_name query string false "Filter by title (en, ru, tm)" // @Param document_name query string false "Filter by name"
// @Param active query bool false "Filter by active" // @Param active query bool false "Filter by active"
// @Param page query int false "Page number" default(1) // @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10) // @Param limit query int false "Items per page" default(10)
+20 -15
View File
@@ -23,9 +23,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) loantype.Re
return &repository{client: client, logger: logger} return &repository{client: client, logger: logger}
} }
const loanTypeColumns = `lt.id, lt.title, lt.tax, lt.loan_term, lt.content, lt.created_at, lt.updated_at` const loanTypeColumns = `lt.id, lt.name, lt.tax, lt.maturity, lt.notes, lt.active, lt.created_at, lt.updated_at`
const loanTypeReturningColumns = `id, title, tax, loan_term, content, created_at, updated_at` const loanTypeReturningColumns = `id, name, tax, maturity, notes, active, created_at, updated_at`
func (r *repository) List(ctx context.Context, filter loantype.ListFilter) (loantype.ListLoanTypeDTO, error) { func (r *repository) List(ctx context.Context, filter loantype.ListFilter) (loantype.ListLoanTypeDTO, error) {
fromClause, whereClause, args := buildListQuery(filter) fromClause, whereClause, args := buildListQuery(filter)
@@ -72,7 +72,7 @@ func buildListQuery(filter loantype.ListFilter) (fromClause, whereClause string,
args = append(args, likePattern(filter.LoanTypeName)) args = append(args, likePattern(filter.LoanTypeName))
n := len(args) n := len(args)
conditions = append(conditions, fmt.Sprintf(`( conditions = append(conditions, fmt.Sprintf(`(
lt.title->>'en' ILIKE $%[1]d OR lt.title->>'ru' ILIKE $%[1]d OR lt.title->>'tm' ILIKE $%[1]d lt.name->>'en' ILIKE $%[1]d OR lt.name->>'ru' ILIKE $%[1]d OR lt.name->>'tm' ILIKE $%[1]d
)`, n)) )`, n))
} }
@@ -100,33 +100,38 @@ func (r *repository) GetByID(ctx context.Context, id int64) (loantype.LoanTypeDT
} }
func (r *repository) Create(ctx context.Context, dto loantype.CreateLoanTypeDTO) (loantype.LoanTypeDTO, error) { func (r *repository) Create(ctx context.Context, dto loantype.CreateLoanTypeDTO) (loantype.LoanTypeDTO, error) {
title, content, err := marshalJSON(dto.Title, dto.Content) name, notes, err := marshalJSON(dto.Title, dto.Content)
if err != nil { if err != nil {
return loantype.LoanTypeDTO{}, err return loantype.LoanTypeDTO{}, err
} }
active := true
if dto.Active != nil {
active = *dto.Active
}
query := fmt.Sprintf(` query := fmt.Sprintf(`
INSERT INTO loan_types (title, tax, loan_term, content) INSERT INTO loan_types (name, tax, maturity, notes, active)
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4, $5)
RETURNING %s`, loanTypeReturningColumns) RETURNING %s`, loanTypeReturningColumns)
return scanLoanType(r.client.QueryRow(ctx, query, title, dto.Tax, dto.LoanTerm, content)) return scanLoanType(r.client.QueryRow(ctx, query, name, dto.Tax, dto.LoanTerm, notes, active))
} }
func (r *repository) Update(ctx context.Context, id int64, dto loantype.UpdateLoanTypeDTO) (loantype.LoanTypeDTO, error) { func (r *repository) Update(ctx context.Context, id int64, dto loantype.UpdateLoanTypeDTO) (loantype.LoanTypeDTO, error) {
title, content, err := marshalJSON(dto.Title, dto.Content) name, notes, err := marshalJSON(dto.Title, dto.Content)
if err != nil { if err != nil {
return loantype.LoanTypeDTO{}, err return loantype.LoanTypeDTO{}, err
} }
query := fmt.Sprintf(` query := fmt.Sprintf(`
UPDATE loan_types SET UPDATE loan_types SET
title = $1, tax = $2, loan_term = $3, content = $4, name = $1, tax = $2, maturity = $3, notes = $4, active = $5,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $5 WHERE id = $6
RETURNING %s`, loanTypeReturningColumns) RETURNING %s`, loanTypeReturningColumns)
item, err := scanLoanType(r.client.QueryRow(ctx, query, title, dto.Tax, dto.LoanTerm, content, id)) item, err := scanLoanType(r.client.QueryRow(ctx, query, name, dto.Tax, dto.LoanTerm, notes, dto.Active, id))
if errors.Is(err, pgx.ErrNoRows) { if errors.Is(err, pgx.ErrNoRows) {
return loantype.LoanTypeDTO{}, err return loantype.LoanTypeDTO{}, err
} }
@@ -162,17 +167,17 @@ type scannable interface {
func scanLoanType(row scannable) (loantype.LoanTypeDTO, error) { func scanLoanType(row scannable) (loantype.LoanTypeDTO, error) {
var item loantype.LoanTypeDTO var item loantype.LoanTypeDTO
var titleJSON, contentJSON []byte var nameJSON, notesJSON []byte
if err := row.Scan( if err := row.Scan(
&item.Id, &titleJSON, &item.Tax, &item.LoanTerm, &contentJSON, &item.CreatedAt, &item.UpdatedAt, &item.Id, &nameJSON, &item.Tax, &item.LoanTerm, &notesJSON, &item.Active, &item.CreatedAt, &item.UpdatedAt,
); err != nil { ); err != nil {
return loantype.LoanTypeDTO{}, err return loantype.LoanTypeDTO{}, err
} }
if err := json.Unmarshal(titleJSON, &item.Title); err != nil { if err := json.Unmarshal(nameJSON, &item.Title); err != nil {
return loantype.LoanTypeDTO{}, err return loantype.LoanTypeDTO{}, err
} }
if err := json.Unmarshal(contentJSON, &item.Content); err != nil { if err := json.Unmarshal(notesJSON, &item.Content); err != nil {
return loantype.LoanTypeDTO{}, err return loantype.LoanTypeDTO{}, err
} }
return item, nil return item, nil
+3
View File
@@ -14,6 +14,7 @@ type LoanTypeDTO struct {
Tax float64 `json:"tax"` Tax float64 `json:"tax"`
LoanTerm string `json:"loanTerm"` LoanTerm string `json:"loanTerm"`
Content LocalizedTitle `json:"content"` Content LocalizedTitle `json:"content"`
Active bool `json:"active"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"` UpdatedAt time.Time `json:"updatedAt"`
} }
@@ -23,6 +24,7 @@ type CreateLoanTypeDTO struct {
Tax float64 `json:"tax" binding:"required"` Tax float64 `json:"tax" binding:"required"`
LoanTerm string `json:"loanTerm" binding:"required"` LoanTerm string `json:"loanTerm" binding:"required"`
Content LocalizedTitle `json:"content" binding:"required"` Content LocalizedTitle `json:"content" binding:"required"`
Active *bool `json:"active"`
} }
type UpdateLoanTypeDTO struct { type UpdateLoanTypeDTO struct {
@@ -30,6 +32,7 @@ type UpdateLoanTypeDTO struct {
Tax float64 `json:"tax" binding:"required"` Tax float64 `json:"tax" binding:"required"`
LoanTerm string `json:"loanTerm" binding:"required"` LoanTerm string `json:"loanTerm" binding:"required"`
Content LocalizedTitle `json:"content" binding:"required"` Content LocalizedTitle `json:"content" binding:"required"`
Active bool `json:"active"`
} }
type ListFilter struct { type ListFilter struct {