aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/models/models.go
blob: c9840125d1af3ed456ec443e13087a4843634c36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package models

import (
	"time"

	"golang.org/x/crypto/bcrypt"
	"gorm.io/gorm"
)

// User represents a user in the system
// We'll store a hashed password, not the plaintext one.
type User struct {
	gorm.Model          // Includes ID, CreatedAt, UpdatedAt, DeletedAt
	Name         string `gorm:"not null"`
	Email        string `gorm:"uniqueIndex;not null"` // Unique email
	PasswordHash string `gorm:"not null"`             // Store hashed password
}

// SetPassword hashes the given password and sets it on the user model.
func (u *User) SetPassword(password string) error {
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return err
	}
	u.PasswordHash = string(hashedPassword)
	return nil
}

// CheckPassword compares a given password with the user's hashed password.
// Returns nil if the password is correct, otherwise returns an error.
func (u *User) CheckPassword(password string) error {
	return bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(password))
}

// Account represents a financial account (e.g., bank account, credit card, cash)
// It belongs to a User.
type Account struct {
	gorm.Model
	UserID uint   `gorm:"not null;index"` // Foreign key to User
	User   User   // Belongs To relationship
	Name   string `gorm:"not null"`
	Type   string `gorm:"not null;index"` // e.g., "Bank", "Credit Card", "Cash", "Loan", "Income Source"
	// Balance is stored in the smallest currency unit (e.g., cents)
	// to avoid floating point issues.
	Balance int64 `gorm:"not null;default:0"`
}

// Transaction represents an income or expense event.
// It belongs to a User and is usually associated with an Account.
type Transaction struct {
	gorm.Model
	UserID      uint     `gorm:"not null;index"` // Foreign key to User
	User        User     // Belongs To relationship
	AccountID   *uint    `gorm:"index"` // Foreign key to Account (nullable, e.g., for cash transactions not tied to a bank)
	Account     *Account // Belongs To relationship (pointer for nullable FK)
	Description string   `gorm:"not null"`
	// Amount stored in the smallest currency unit (e.g., cents)
	Amount   int64     `gorm:"not null"`
	Type     string    `gorm:"not null;index"` // "Income" or "Expense"
	Date     time.Time `gorm:"not null;index"` // Date of the transaction
	Category string    `gorm:"index"`          // e.g., "Salary", "Groceries", "Utilities"
}

// Loan represents a loan taken by the user.
// It belongs to a User and may be linked to an Account representing the loan source/payments.
type Loan struct {
	gorm.Model
	UserID         uint     `gorm:"not null;index"` // Foreign key to User
	User           User     // Belongs To relationship
	AccountID      *uint    `gorm:"index"` // Optional FK to the related Account (e.g., where payments are made from/to)
	Account        *Account // Belongs To relationship
	Name           string   `gorm:"not null"` // e.g., "Car Loan", "Student Loan"
	OriginalAmount int64    `gorm:"not null"` // In smallest currency unit
	CurrentBalance int64    `gorm:"not null"` // In smallest currency unit
	InterestRate   float64  // Annual interest rate (e.g., 0.05 for 5%)
	StartDate      time.Time
	EndDate        time.Time // Expected payoff date
	// Add fields for payment frequency, next due date etc. if needed later
}

// Goal represents a financial goal the user is working towards.
// It belongs to a User.
type Goal struct {
	gorm.Model
	UserID        uint      `gorm:"not null;index"` // Foreign key to User
	User          User      // Belongs To relationship
	Name          string    `gorm:"not null"`           // e.g., "Save for Down Payment", "Pay Off Credit Card"
	TargetAmount  int64     `gorm:"not null"`           // In smallest currency unit
	CurrentAmount int64     `gorm:"not null;default:0"` // In smallest currency unit
	TargetDate    time.Time // Optional target date for the goal
	Status        string    `gorm:"not null;default:'Active';index"` // e.g., "Active", "Achieved", "Cancelled"
	// Link to specific accounts or loans if needed (e.g., goal to pay off a specific loan)
	// RelatedAccountID *uint
	// RelatedLoanID *uint
}

// Add other models below (Goal)