Skip to content

Commit

Permalink
refactor: update database and entity struct 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinezAvellan committed Jun 3, 2024
1 parent ad65108 commit 0e7bf06
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
24 changes: 21 additions & 3 deletions components/transaction/internal/domain/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type TransactionPostgreSQLModel struct {
Description string
Template string
Status string
StatusDescription *string
Amount *float64
AmountScale *float64
InstrumentCode string
Expand All @@ -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"`
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e7bf06

Please sign in to comment.