Skip to content

Commit

Permalink
feat: change from FinishAt to ClosesAt
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemarlon committed Nov 25, 2024
1 parent ce0a17f commit 9092c3a
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 101 deletions.
68 changes: 34 additions & 34 deletions coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ github.com/tribeshq/tribes/internal/domain/entity/contracts.go:45.20,47.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/contracts.go:48.2,48.37 1 1
github.com/tribeshq/tribes/internal/domain/entity/contracts.go:48.37,50.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/contracts.go:51.2,51.12 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:51.177,61.48 2 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:61.48,63.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:64.2,64.26 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:67.41,68.37 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:68.37,70.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:71.2,71.30 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:71.30,73.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:74.2,74.55 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:74.55,76.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:77.2,77.35 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:77.35,79.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:80.2,80.22 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:80.22,82.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:83.2,83.42 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:83.42,85.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:86.2,86.22 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:86.22,88.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:89.2,89.32 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:89.32,91.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:92.2,92.23 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:92.23,94.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:95.2,95.12 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:52.176,62.48 2 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:62.48,64.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:65.2,65.26 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:68.41,69.37 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:69.37,71.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:72.2,72.30 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:72.30,74.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:75.2,75.55 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:75.55,77.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:78.2,78.35 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:78.35,80.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:81.2,81.21 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:81.21,83.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:84.2,84.39 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:84.39,86.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:87.2,87.22 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:87.22,89.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:90.2,90.31 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:90.31,92.3 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:93.2,93.23 1 1
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:93.23,95.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/crowdfunding.go:96.2,96.12 1 1
github.com/tribeshq/tribes/internal/domain/entity/order.go:49.142,58.41 2 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:58.41,60.3 1 0
github.com/tribeshq/tribes/internal/domain/entity/order.go:61.2,61.19 1 0
Expand Down Expand Up @@ -417,8 +417,8 @@ github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sql
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:213.70,215.3 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:216.2,216.23 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:216.23,218.3 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.2,219.26 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.26,221.3 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.2,219.25 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.25,221.3 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:222.2,222.27 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:222.27,224.3 1 0
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:225.2,237.22 3 0
Expand Down Expand Up @@ -539,8 +539,8 @@ github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfund
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:52.60,54.9 2 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:58.2,58.32 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:58.32,60.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.2,63.61 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.61,65.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.2,63.60 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.60,65.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:68.2,69.16 2 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:69.16,71.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:74.2,74.41 1 0
Expand Down Expand Up @@ -667,8 +667,8 @@ github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:66.2,6
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:67.16,69.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:71.2,72.45 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:72.45,73.60 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:73.60,74.56 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:74.56,76.5 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:73.60,74.55 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:74.55,76.5 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:77.4,78.9 2 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:81.2,81.31 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:81.31,83.3 1 0
Expand Down Expand Up @@ -1434,8 +1434,8 @@ github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sql
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:213.70,215.3 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:216.2,216.23 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:216.23,218.3 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.2,219.26 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.26,221.3 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.2,219.25 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:219.25,221.3 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:222.2,222.27 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:222.27,224.3 1 1
github.com/tribeshq/tribes/internal/infra/repository/crowdfunding_repository_sqlite.go:225.2,237.22 3 1
Expand Down Expand Up @@ -1556,8 +1556,8 @@ github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfund
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:52.60,54.9 2 1
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:58.2,58.32 1 1
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:58.32,60.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.2,63.61 1 1
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.61,65.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.2,63.60 1 1
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:63.60,65.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:68.2,69.16 2 1
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:69.16,71.3 1 0
github.com/tribeshq/tribes/internal/usecase/crowdfunding_usecase/close_crowdfunding.go:74.2,74.41 1 1
Expand Down Expand Up @@ -1684,8 +1684,8 @@ github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:66.2,6
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:67.16,69.3 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:71.2,72.45 2 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:72.45,73.60 1 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:73.60,74.56 1 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:74.56,76.5 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:73.60,74.55 1 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:74.55,76.5 1 0
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:77.4,78.9 2 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:81.2,81.31 1 1
github.com/tribeshq/tribes/internal/usecase/order_usecase/create_order.go:81.31,83.3 1 0
Expand Down
13 changes: 7 additions & 6 deletions internal/domain/entity/crowdfunding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
)
Expand Down Expand Up @@ -42,19 +43,19 @@ type Crowdfunding struct {
TotalObligation *uint256.Int `json:"total_obligation,omitempty" gorm:"type:text;not null;default:0"`
State CrowdfundingState `json:"state,omitempty" gorm:"type:text;not null"`
Orders []*Order `json:"orders,omitempty" gorm:"foreignKey:CrowdfundingId;constraint:OnDelete:CASCADE"`
ExpiresAt int64 `json:"expires_at,omitempty" gorm:"not null"`
ClosesAt int64 `json:"closes_at,omitempty" gorm:"not null"`
MaturityAt int64 `json:"maturity_at,omitempty" gorm:"not null"`
CreatedAt int64 `json:"created_at,omitempty" gorm:"not null"`
UpdatedAt int64 `json:"updated_at,omitempty" gorm:"default:0"`
}

func NewCrowdfunding(creator common.Address, debt_issued *uint256.Int, maxInterestRate *uint256.Int, expiresAt int64, maturityAt int64, createdAt int64) (*Crowdfunding, error) {
func NewCrowdfunding(creator common.Address, debt_issued *uint256.Int, maxInterestRate *uint256.Int, closesAt int64, maturityAt int64, createdAt int64) (*Crowdfunding, error) {
crowdfunding := &Crowdfunding{
Creator: creator,
DebtIssued: debt_issued,
MaxInterestRate: maxInterestRate,
State: CrowdfundingStateUnderReview,
ExpiresAt: expiresAt,
ClosesAt: closesAt,
MaturityAt: maturityAt,
CreatedAt: createdAt,
}
Expand All @@ -77,16 +78,16 @@ func (a *Crowdfunding) Validate() error {
if a.MaxInterestRate.Sign() == 0 {
return fmt.Errorf("%w: max interest rate cannot be zero", ErrInvalidCrowdfunding)
}
if a.ExpiresAt == 0 {
if a.ClosesAt == 0 {
return fmt.Errorf("%w: expiration date is missing", ErrInvalidCrowdfunding)
}
if a.ExpiresAt > a.CreatedAt + 15552000 {
if a.ClosesAt > a.CreatedAt+15552000 {
return fmt.Errorf("%w: expiration date cannot be greater than 6 months", ErrInvalidCrowdfunding)
}
if a.CreatedAt == 0 {
return fmt.Errorf("%w: creation date is missing", ErrInvalidCrowdfunding)
}
if a.CreatedAt >= a.ExpiresAt {
if a.CreatedAt >= a.ClosesAt {
return fmt.Errorf("%w: creation date cannot be greater than or equal to expiration date", ErrInvalidCrowdfunding)
}
if a.MaturityAt == 0 {
Expand Down
24 changes: 12 additions & 12 deletions internal/domain/entity/crowdfunding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestNewCrowdfunding(t *testing.T) {
creator := common.HexToAddress("0x123")
debtIssued := uint256.NewInt(1000000000)
maxInterestRate := uint256.NewInt(50000000)
expiresAt := time.Now().Add(24 * time.Hour).Unix()
closesAt := time.Now().Add(24 * time.Hour).Unix()
maturityAt := time.Now().Add(48 * time.Hour).Unix() // Adiciona MaturityAt válido
createdAt := time.Now().Unix()

t.Run("Valid Crowdfunding", func(t *testing.T) {
crowdfunding, err := NewCrowdfunding(creator, debtIssued, maxInterestRate, expiresAt, maturityAt, createdAt)
crowdfunding, err := NewCrowdfunding(creator, debtIssued, maxInterestRate, closesAt, maturityAt, createdAt)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
Expand All @@ -30,34 +30,34 @@ func TestNewCrowdfunding(t *testing.T) {
assert.Equal(t, debtIssued, crowdfunding.DebtIssued, "DebtIssued mismatch")
assert.Equal(t, maxInterestRate, crowdfunding.MaxInterestRate, "MaxInterestRate mismatch")
assert.Equal(t, CrowdfundingStateUnderReview, crowdfunding.State, "State mismatch")
assert.Equal(t, expiresAt, crowdfunding.ExpiresAt, "ExpiresAt mismatch")
assert.Equal(t, closesAt, crowdfunding.ClosesAt, "ClosesAt mismatch")
assert.Equal(t, maturityAt, crowdfunding.MaturityAt, "MaturityAt mismatch")
assert.Equal(t, createdAt, crowdfunding.CreatedAt, "CreatedAt mismatch")
})

t.Run("Invalid Creator", func(t *testing.T) {
_, err := NewCrowdfunding(common.Address{}, debtIssued, maxInterestRate, expiresAt, maturityAt, createdAt)
_, err := NewCrowdfunding(common.Address{}, debtIssued, maxInterestRate, closesAt, maturityAt, createdAt)
assert.Error(t, err, "Error expected for invalid creator address")
assert.True(t, errors.Is(err, ErrInvalidCrowdfunding), "Error should be ErrInvalidCrowdfunding")
assert.Contains(t, err.Error(), "invalid creator address", "Error message should mention invalid creator")
})

t.Run("Invalid Debt Issued", func(t *testing.T) {
_, err := NewCrowdfunding(creator, uint256.NewInt(0), maxInterestRate, expiresAt, maturityAt, createdAt)
_, err := NewCrowdfunding(creator, uint256.NewInt(0), maxInterestRate, closesAt, maturityAt, createdAt)
assert.Error(t, err, "Error expected for zero DebtIssued")
assert.True(t, errors.Is(err, ErrInvalidCrowdfunding), "Error should be ErrInvalidCrowdfunding")
assert.Contains(t, err.Error(), "debt issued cannot be zero", "Error message should mention DebtIssued")
})

t.Run("Debt Issued Exceeds Maximum", func(t *testing.T) {
_, err := NewCrowdfunding(creator, uint256.NewInt(15000000001), maxInterestRate, expiresAt, maturityAt, createdAt)
_, err := NewCrowdfunding(creator, uint256.NewInt(15000000001), maxInterestRate, closesAt, maturityAt, createdAt)
assert.Error(t, err, "Error expected for exceeding max DebtIssued")
assert.True(t, errors.Is(err, ErrInvalidCrowdfunding), "Error should be ErrInvalidCrowdfunding")
assert.Contains(t, err.Error(), "debt issued exceeds the maximum allowed value", "Error message should mention maximum allowed value")
})

t.Run("Invalid Max Interest Rate", func(t *testing.T) {
_, err := NewCrowdfunding(creator, debtIssued, uint256.NewInt(0), expiresAt, maturityAt, createdAt)
_, err := NewCrowdfunding(creator, debtIssued, uint256.NewInt(0), closesAt, maturityAt, createdAt)
assert.Error(t, err, "Error expected for zero MaxInterestRate")
assert.True(t, errors.Is(err, ErrInvalidCrowdfunding), "Error should be ErrInvalidCrowdfunding")
assert.Contains(t, err.Error(), "max interest rate cannot be zero", "Error message should mention MaxInterestRate")
Expand All @@ -82,7 +82,7 @@ func TestCrowdfunding_Validate(t *testing.T) {
creator := common.HexToAddress("0x123")
debtIssued := uint256.NewInt(1000000000)
maxInterestRate := uint256.NewInt(50000000)
expiresAt := time.Now().Add(24 * time.Hour).Unix()
closesAt := time.Now().Add(24 * time.Hour).Unix()
maturityAt := time.Now().Add(48 * time.Hour).Unix() // Adiciona MaturityAt válido
createdAt := time.Now().Unix()

Expand All @@ -91,7 +91,7 @@ func TestCrowdfunding_Validate(t *testing.T) {
Creator: creator,
DebtIssued: debtIssued,
MaxInterestRate: maxInterestRate,
ExpiresAt: expiresAt,
ClosesAt: closesAt,
MaturityAt: maturityAt,
CreatedAt: createdAt,
}
Expand All @@ -104,7 +104,7 @@ func TestCrowdfunding_Validate(t *testing.T) {
Creator: common.Address{},
DebtIssued: debtIssued,
MaxInterestRate: maxInterestRate,
ExpiresAt: expiresAt,
ClosesAt: closesAt,
MaturityAt: maturityAt,
CreatedAt: createdAt,
}
Expand All @@ -118,7 +118,7 @@ func TestCrowdfunding_Validate(t *testing.T) {
Creator: creator,
DebtIssued: uint256.NewInt(0),
MaxInterestRate: maxInterestRate,
ExpiresAt: expiresAt,
ClosesAt: closesAt,
MaturityAt: maturityAt,
CreatedAt: createdAt,
}
Expand All @@ -132,7 +132,7 @@ func TestCrowdfunding_Validate(t *testing.T) {
Creator: creator,
DebtIssued: debtIssued,
MaxInterestRate: maxInterestRate,
ExpiresAt: 0,
ClosesAt: 0,
MaturityAt: maturityAt,
CreatedAt: createdAt,
}
Expand Down
Loading

0 comments on commit 9092c3a

Please sign in to comment.