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:
2026-07-06 22:48:19 +05:00
parent 08be61cbcd
commit ef56625321
9 changed files with 66 additions and 89 deletions

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 reasons_cards;
DROP TABLE IF EXISTS payment_cards;

View File

@@ -75,20 +75,19 @@ CREATE TABLE IF NOT EXISTS reasons_cards (
CREATE TABLE IF NOT EXISTS loan_types (
id bigserial PRIMARY KEY,
title json NOT NULL DEFAULT '{}'::json,
name json NOT NULL DEFAULT '{}'::json,
tax numeric(12, 2) NOT NULL DEFAULT 0,
loan_term character varying(250) NOT NULL DEFAULT '',
content json NOT NULL DEFAULT '{}'::json,
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,
title json NOT NULL DEFAULT '{}'::json,
content_en text NOT NULL DEFAULT '',
content_ru text NOT NULL DEFAULT '',
content_tm text NOT NULL DEFAULT '',
maturity character varying(250) NOT NULL DEFAULT '',
notes json NOT NULL DEFAULT '{}'::json,
active boolean NOT NULL DEFAULT true,
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_order_required_docs (
id bigserial PRIMARY KEY,
name text NOT NULL DEFAULT '',
value text NOT NULL DEFAULT '',
active boolean NOT NULL DEFAULT true,
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL

View File

@@ -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 provinces RENAME COLUMN active TO status;
ALTER TABLE users RENAME COLUMN active TO status;

View File

@@ -1,4 +1,4 @@
ALTER TABLE users RENAME COLUMN status TO active;
ALTER TABLE provinces 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;

View File

@@ -2,7 +2,6 @@ package db
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
@@ -23,9 +22,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) loansdocume
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) {
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{}) {
fromClause = ` FROM loan_required_documents d`
fromClause = ` FROM loan_order_required_docs d`
var conditions []string
if filter.DocumentName != "" {
args = append(args, likePattern(filter.DocumentName))
n := len(args)
conditions = append(conditions, fmt.Sprintf(`(
d.title->>'en' ILIKE $%[1]d OR d.title->>'ru' ILIKE $%[1]d OR d.title->>'tm' ILIKE $%[1]d
)`, n))
conditions = append(conditions, fmt.Sprintf(`d.name ILIKE $%d`, n))
}
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) {
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))
if errors.Is(err, pgx.ErrNoRows) {
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) {
title, err := json.Marshal(dto.Title)
if err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
active := true
if dto.Active != nil {
active = *dto.Active
}
query := fmt.Sprintf(`
INSERT INTO loan_required_documents (title, content_en, content_ru, content_tm, active)
VALUES ($1, $2, $3, $4, $5)
INSERT INTO loan_order_required_docs (name, value, active)
VALUES ($1, $2, $3)
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) {
title, err := json.Marshal(dto.Title)
if err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
query := fmt.Sprintf(`
UPDATE loan_required_documents SET
title = $1, content_en = $2, content_ru = $3, content_tm = $4, active = $5,
UPDATE loan_order_required_docs SET
name = $1, value = $2, active = $3,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $6
WHERE id = $4
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) {
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) {
query := fmt.Sprintf(`
UPDATE loan_required_documents SET
UPDATE loan_order_required_docs SET
active = $1,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
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 {
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 {
return err
}
@@ -176,16 +163,11 @@ type scannable interface {
func scanLoanDocument(row scannable) (loansdocument.LoanDocumentDTO, error) {
var item loansdocument.LoanDocumentDTO
var titleJSON []byte
if err := row.Scan(
&item.Id, &titleJSON, &item.ContentEn, &item.ContentRu, &item.ContentTm,
&item.Active, &item.CreatedAt, &item.UpdatedAt,
&item.Id, &item.Name, &item.Value, &item.Active, &item.CreatedAt, &item.UpdatedAt,
); err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
return item, nil
}

View File

@@ -2,37 +2,25 @@ package loansdocument
import "time"
type LocalizedTitle struct {
En string `json:"en"`
Ru string `json:"ru"`
Tm string `json:"tm"`
}
type LoanDocumentDTO struct {
Id int64 `json:"id"`
Title LocalizedTitle `json:"title"`
ContentEn string `json:"contentEn"`
ContentRu string `json:"contentRu"`
ContentTm string `json:"contentTm"`
Active bool `json:"active"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Id int64 `json:"id"`
Name string `json:"name"`
Value string `json:"value"`
Active bool `json:"active"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type CreateLoanDocumentDTO struct {
Title LocalizedTitle `json:"title" binding:"required"`
ContentEn string `json:"contentEn" binding:"required"`
ContentRu string `json:"contentRu" binding:"required"`
ContentTm string `json:"contentTm" binding:"required"`
Active *bool `json:"active"`
Name string `json:"name" binding:"required"`
Value string `json:"value" binding:"required"`
Active *bool `json:"active"`
}
type UpdateLoanDocumentDTO struct {
Title LocalizedTitle `json:"title" binding:"required"`
ContentEn string `json:"contentEn" binding:"required"`
ContentRu string `json:"contentRu" binding:"required"`
ContentTm string `json:"contentTm" binding:"required"`
Active bool `json:"active"`
Name string `json:"name" binding:"required"`
Value string `json:"value" binding:"required"`
Active bool `json:"active"`
}
type UpdateLoanDocumentActiveDTO struct {

View File

@@ -41,7 +41,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
// @Tags loan-document
// @Produce json
// @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 page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(10)

View File

@@ -23,9 +23,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) loantype.Re
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) {
fromClause, whereClause, args := buildListQuery(filter)
@@ -72,7 +72,7 @@ func buildListQuery(filter loantype.ListFilter) (fromClause, whereClause string,
args = append(args, likePattern(filter.LoanTypeName))
n := len(args)
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))
}
@@ -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) {
title, content, err := marshalJSON(dto.Title, dto.Content)
name, notes, err := marshalJSON(dto.Title, dto.Content)
if err != nil {
return loantype.LoanTypeDTO{}, err
}
active := true
if dto.Active != nil {
active = *dto.Active
}
query := fmt.Sprintf(`
INSERT INTO loan_types (title, tax, loan_term, content)
VALUES ($1, $2, $3, $4)
INSERT INTO loan_types (name, tax, maturity, notes, active)
VALUES ($1, $2, $3, $4, $5)
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) {
title, content, err := marshalJSON(dto.Title, dto.Content)
name, notes, err := marshalJSON(dto.Title, dto.Content)
if err != nil {
return loantype.LoanTypeDTO{}, err
}
query := fmt.Sprintf(`
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')
WHERE id = $5
WHERE id = $6
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) {
return loantype.LoanTypeDTO{}, err
}
@@ -162,17 +167,17 @@ type scannable interface {
func scanLoanType(row scannable) (loantype.LoanTypeDTO, error) {
var item loantype.LoanTypeDTO
var titleJSON, contentJSON []byte
var nameJSON, notesJSON []byte
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 {
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
}
if err := json.Unmarshal(contentJSON, &item.Content); err != nil {
if err := json.Unmarshal(notesJSON, &item.Content); err != nil {
return loantype.LoanTypeDTO{}, err
}
return item, nil

View File

@@ -14,6 +14,7 @@ type LoanTypeDTO struct {
Tax float64 `json:"tax"`
LoanTerm string `json:"loanTerm"`
Content LocalizedTitle `json:"content"`
Active bool `json:"active"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
@@ -23,6 +24,7 @@ type CreateLoanTypeDTO struct {
Tax float64 `json:"tax" binding:"required"`
LoanTerm string `json:"loanTerm" binding:"required"`
Content LocalizedTitle `json:"content" binding:"required"`
Active *bool `json:"active"`
}
type UpdateLoanTypeDTO struct {
@@ -30,6 +32,7 @@ type UpdateLoanTypeDTO struct {
Tax float64 `json:"tax" binding:"required"`
LoanTerm string `json:"loanTerm" binding:"required"`
Content LocalizedTitle `json:"content" binding:"required"`
Active bool `json:"active"`
}
type ListFilter struct {