From 0e7bf06ed509301522307cc1e8bd9ef5829efe81 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Mon, 3 Jun 2024 12:34:55 +0200 Subject: [PATCH] refactor: update database and entity struct :hammer: --- .../domain/transaction/transaction.go | 24 ++++++++++++++++--- .../000001_create_transaction_table.up.sql | 1 + .../000002_create_operation_table.up.sql | 10 +++++--- ...000003_create_instrument_rate_table.up.sql | 3 +++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/components/transaction/internal/domain/transaction/transaction.go b/components/transaction/internal/domain/transaction/transaction.go index 04200b78..31e5268f 100644 --- a/components/transaction/internal/domain/transaction/transaction.go +++ b/components/transaction/internal/domain/transaction/transaction.go @@ -14,6 +14,7 @@ type TransactionPostgreSQLModel struct { Description string Template string Status string + StatusDescription *string Amount *float64 AmountScale *float64 InstrumentCode string @@ -26,13 +27,24 @@ type TransactionPostgreSQLModel struct { Metadata map[string]any } +// Status structure for marshaling/unmarshalling JSON. +type Status struct { + Code string `json:"code"` + Description *string `json:"description"` +} + +// IsEmpty method that set empty or nil in fields +func (s Status) IsEmpty() bool { + return s.Code == "" && s.Description == nil +} + // Transaction is a struct designed to encapsulate response payload data. type Transaction struct { ID string `json:"id"` ParentTransactionID *string `json:"parentTransactionId,omitempty"` Description string `json:"description"` Template string `json:"template"` - Status string `json:"status"` + Status Status `json:"status"` Amount *float64 `json:"amount"` AmountScale *float64 `json:"amountScale"` InstrumentCode string `json:"InstrumentCode"` @@ -47,12 +59,17 @@ type Transaction struct { // ToEntity converts an TransactionPostgreSQLModel to entity Transaction func (t *TransactionPostgreSQLModel) ToEntity() *Transaction { + status := Status{ + Code: t.Status, + Description: t.StatusDescription, + } + transaction := &Transaction{ ID: t.ID, ParentTransactionID: t.ParentTransactionID, Description: t.Description, Template: t.Template, - Status: t.Status, + Status: status, Amount: t.Amount, AmountScale: t.AmountScale, InstrumentCode: t.InstrumentCode, @@ -78,7 +95,8 @@ func (t *TransactionPostgreSQLModel) FromEntity(transaction *Transaction) { ParentTransactionID: transaction.ParentTransactionID, Description: transaction.Description, Template: transaction.Template, - Status: transaction.Status, + Status: transaction.Status.Code, + StatusDescription: transaction.Status.Description, Amount: transaction.Amount, AmountScale: transaction.AmountScale, InstrumentCode: transaction.InstrumentCode, diff --git a/components/transaction/migrations/000001_create_transaction_table.up.sql b/components/transaction/migrations/000001_create_transaction_table.up.sql index 58fcf055..3f924cdd 100644 --- a/components/transaction/migrations/000001_create_transaction_table.up.sql +++ b/components/transaction/migrations/000001_create_transaction_table.up.sql @@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS "transaction" ( description TEXT NOT NULL, template TEXT NOT NULL, status TEXT NOT NULL, + status_description TEXT, amount NUMERIC NOT NULL, amount_scale NUMERIC NOT NULL, instrument_code TEXT NOT NULL, diff --git a/components/transaction/migrations/000002_create_operation_table.up.sql b/components/transaction/migrations/000002_create_operation_table.up.sql index 4d740ff0..d9fa7553 100644 --- a/components/transaction/migrations/000002_create_operation_table.up.sql +++ b/components/transaction/migrations/000002_create_operation_table.up.sql @@ -2,10 +2,8 @@ CREATE TABLE IF NOT EXISTS operation ( id UUID PRIMARY KEY NOT NULL DEFAULT (uuid_generate_v4()), transaction_id UUID NOT NULL, description TEXT NOT NULL, - ledger_id UUID NOT NULL, type TEXT NOT NULL, instrument_code TEXT NOT NULL, - status TEXT NOT NULL, amount NUMERIC NOT NULL, amount_scale NUMERIC NOT NULL, available_balance NUMERIC NOT NULL, @@ -14,10 +12,16 @@ CREATE TABLE IF NOT EXISTS operation ( available_balance_after NUMERIC NOT NULL, on_hold_balance_after NUMERIC NOT NULL, balance_scale_after NUMERIC NOT NULL, + status TEXT NOT NULL, + status_description TEXT, account_id UUID NOT NULL, account_alias TEXT NOT NULL, portfolio_id UUID NOT NULL, + chart_of_accounts TEXT NOT NULL, + organization_id UUID NOT NULL, + ledger_id UUID NOT NULL, created_at TIMESTAMP WITH TIME ZONE, updated_at TIMESTAMP WITH TIME ZONE, - deleted_at TIMESTAMP WITH TIME ZONE + deleted_at TIMESTAMP WITH TIME ZONE, + FOREIGN KEY (transaction_id) REFERENCES "transaction" (id) ) \ No newline at end of file diff --git a/components/transaction/migrations/000003_create_instrument_rate_table.up.sql b/components/transaction/migrations/000003_create_instrument_rate_table.up.sql index c2158197..797a258b 100644 --- a/components/transaction/migrations/000003_create_instrument_rate_table.up.sql +++ b/components/transaction/migrations/000003_create_instrument_rate_table.up.sql @@ -6,6 +6,9 @@ CREATE TABLE IF NOT EXISTS instrument_rate ( scale NUMERIC NOT NULL, source TEXT NOT NULL, status TEXT NOT NULL, + status_description TEXT, + organization_id UUID NOT NULL, + ledger_id UUID NOT NULL, created_at TIMESTAMP WITH TIME ZONE, updated_at TIMESTAMP WITH TIME ZONE, deleted_at TIMESTAMP WITH TIME ZONE