-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deposit, Withdrawal, and Interest Transactions for Investment View #1075
Merged
zachgoll
merged 4 commits into
main
from
1050-user-can-create-depositwithdrawal-investment-transactions
Aug 10, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {Controller} from "@hotwired/stimulus" | ||
|
||
const TRADE_TYPES = { | ||
BUY: "buy", | ||
SELL: "sell", | ||
TRANSFER_IN: "transfer_in", | ||
TRANSFER_OUT: "transfer_out", | ||
INTEREST: "interest" | ||
} | ||
|
||
const FIELD_VISIBILITY = { | ||
[TRADE_TYPES.BUY]: {ticker: true, qty: true, price: true}, | ||
[TRADE_TYPES.SELL]: {ticker: true, qty: true, price: true}, | ||
[TRADE_TYPES.TRANSFER_IN]: {amount: true, transferAccount: true}, | ||
[TRADE_TYPES.TRANSFER_OUT]: {amount: true, transferAccount: true}, | ||
[TRADE_TYPES.INTEREST]: {amount: true} | ||
} | ||
|
||
// Connects to data-controller="trade-form" | ||
export default class extends Controller { | ||
static targets = ["typeInput", "tickerInput", "amountInput", "transferAccountInput", "qtyInput", "priceInput"] | ||
|
||
connect() { | ||
this.handleTypeChange = this.handleTypeChange.bind(this) | ||
this.typeInputTarget.addEventListener("change", this.handleTypeChange) | ||
this.updateFields(this.typeInputTarget.value || TRADE_TYPES.BUY) | ||
} | ||
|
||
disconnect() { | ||
this.typeInputTarget.removeEventListener("change", this.handleTypeChange) | ||
} | ||
|
||
handleTypeChange(event) { | ||
this.updateFields(event.target.value) | ||
} | ||
|
||
updateFields(type) { | ||
const visibleFields = FIELD_VISIBILITY[type] || {} | ||
|
||
Object.entries(this.fieldTargets).forEach(([field, target]) => { | ||
const isVisible = visibleFields[field] || false | ||
|
||
// Update visibility | ||
target.hidden = !isVisible | ||
|
||
// Update required status based on visibility | ||
if (isVisible) { | ||
target.setAttribute('required', '') | ||
} else { | ||
target.removeAttribute('required') | ||
} | ||
}) | ||
} | ||
|
||
get fieldTargets() { | ||
return { | ||
ticker: this.tickerInputTarget, | ||
amount: this.amountInputTarget, | ||
transferAccount: this.transferAccountInputTarget, | ||
qty: this.qtyInputTarget, | ||
price: this.priceInputTarget | ||
} | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class Account::EntryBuilder | ||
include ActiveModel::Model | ||
|
||
TYPES = %w[ income expense buy sell interest transfer_in transfer_out ].freeze | ||
|
||
attr_accessor :type, :date, :qty, :ticker, :price, :amount, :currency, :account, :transfer_account_id | ||
|
||
validates :type, inclusion: { in: TYPES } | ||
|
||
def save | ||
if valid? | ||
create_builder.save | ||
end | ||
end | ||
|
||
private | ||
|
||
def create_builder | ||
case type | ||
when "buy", "sell" | ||
create_trade_builder | ||
else | ||
create_transaction_builder | ||
end | ||
end | ||
|
||
def create_trade_builder | ||
Account::TradeBuilder.new \ | ||
type: type, | ||
date: date, | ||
qty: qty, | ||
ticker: ticker, | ||
price: price, | ||
account: account | ||
end | ||
|
||
def create_transaction_builder | ||
Account::TransactionBuilder.new \ | ||
type: type, | ||
date: date, | ||
amount: amount, | ||
account: account, | ||
transfer_account_id: transfer_account_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class Account::TransactionBuilder | ||
include ActiveModel::Model | ||
|
||
TYPES = %w[ income expense interest transfer_in transfer_out ].freeze | ||
|
||
attr_accessor :type, :amount, :date, :account, :transfer_account_id | ||
|
||
validates :type, :amount, :date, presence: true | ||
validates :type, inclusion: { in: TYPES } | ||
|
||
def save | ||
if valid? | ||
transfer? ? create_transfer : create_transaction | ||
end | ||
end | ||
|
||
private | ||
|
||
def transfer? | ||
%w[transfer_in transfer_out].include?(type) | ||
end | ||
|
||
def create_transfer | ||
return create_unlinked_transfer(account.id, signed_amount) unless transfer_account_id | ||
|
||
from_account_id = type == "transfer_in" ? transfer_account_id : account.id | ||
to_account_id = type == "transfer_in" ? account.id : transfer_account_id | ||
|
||
outflow = create_unlinked_transfer(from_account_id, signed_amount.abs) | ||
inflow = create_unlinked_transfer(to_account_id, signed_amount.abs * -1) | ||
|
||
Account::Transfer.create! entries: [ outflow, inflow ] | ||
|
||
inflow | ||
end | ||
|
||
def create_unlinked_transfer(account_id, amount) | ||
build_entry(account_id, amount, marked_as_transfer: true).tap(&:save!) | ||
end | ||
|
||
def create_transaction | ||
build_entry(account.id, signed_amount).tap(&:save!) | ||
end | ||
|
||
def build_entry(account_id, amount, marked_as_transfer: false) | ||
Account::Entry.new \ | ||
account_id: account_id, | ||
amount: amount, | ||
currency: account.currency, | ||
date: date, | ||
marked_as_transfer: marked_as_transfer, | ||
entryable: Account::Transaction.new | ||
end | ||
|
||
def signed_amount | ||
case type | ||
when "expense", "transfer_out" | ||
amount.to_d | ||
else | ||
amount.to_d * -1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very rudimentary controller, highly coupled to the trade forms. Ideally we'll abstract this a bit in the future