mirror of
https://github.com/we-promise/sure.git
synced 2026-07-16 14:55:23 +00:00
fix(trades): guard against nil security in create_trade (#2066)
Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com>
This commit is contained in:
@@ -37,11 +37,19 @@ class Trade::CreateForm
|
||||
end
|
||||
|
||||
def create_trade
|
||||
sec = security
|
||||
|
||||
unless sec
|
||||
entry = account.entries.build(entryable: Trade.new)
|
||||
entry.errors.add(:base, I18n.t("trades.form.trade_requires_security"))
|
||||
return entry
|
||||
end
|
||||
|
||||
signed_qty = type == "sell" ? -qty.to_d : qty.to_d
|
||||
signed_amount = signed_qty * price.to_d + fee.to_d
|
||||
|
||||
trade_entry = account.entries.new(
|
||||
name: Trade.build_name(type, qty, security.ticker),
|
||||
name: Trade.build_name(type, qty, sec.ticker),
|
||||
date: date,
|
||||
amount: signed_amount,
|
||||
currency: currency,
|
||||
@@ -50,7 +58,7 @@ class Trade::CreateForm
|
||||
price: price,
|
||||
fee: fee.to_d,
|
||||
currency: currency,
|
||||
security: security,
|
||||
security: sec,
|
||||
investment_activity_label: type.capitalize # "buy" → "Buy", "sell" → "Sell"
|
||||
)
|
||||
)
|
||||
|
||||
@@ -20,6 +20,7 @@ en:
|
||||
type_dividend: Dividend
|
||||
type_interest: Interest
|
||||
dividend_requires_security: Security is required for dividends
|
||||
trade_requires_security: A security (ticker) is required for buy and sell trades
|
||||
header:
|
||||
buy: Buy
|
||||
sell: Sell
|
||||
|
||||
Reference in New Issue
Block a user