mirror of
https://github.com/we-promise/sure.git
synced 2026-05-07 12:54:04 +00:00
feat(entries): Add amount validation and robustify monetizable concern (#1680)
* feat(entries): Add amount validation and robustify monetizable concern * fix(valuations): localize blank amount errors --------- Co-authored-by: sentry[bot] <39604003+sentry[bot]@users.noreply.github.com> Co-authored-by: SureBot <sure-bot@we-promise.com>
This commit is contained in:
@@ -7,6 +7,12 @@ class ValuationsController < ApplicationController
|
||||
|
||||
@entry = @account.entries.build(entry_params.merge(currency: @account.currency))
|
||||
|
||||
if entry_params[:amount].blank?
|
||||
@error_message = t("valuations.errors.amount_required")
|
||||
render :new, status: :unprocessable_entity
|
||||
return
|
||||
end
|
||||
|
||||
@reconciliation_dry_run = @entry.account.create_reconciliation(
|
||||
balance: entry_params[:amount],
|
||||
date: entry_params[:date],
|
||||
@@ -21,6 +27,13 @@ class ValuationsController < ApplicationController
|
||||
return unless require_account_permission!(@entry.account)
|
||||
|
||||
@account = @entry.account
|
||||
|
||||
if entry_params[:amount].blank?
|
||||
@error_message = t("valuations.errors.amount_required")
|
||||
render :show, status: :unprocessable_entity
|
||||
return
|
||||
end
|
||||
|
||||
@entry.assign_attributes(entry_params.merge(currency: @account.currency))
|
||||
|
||||
@reconciliation_dry_run = @entry.account.update_reconciliation(
|
||||
|
||||
@@ -7,7 +7,7 @@ module Monetizable
|
||||
define_method("#{field}_money") do |**args|
|
||||
value = self.send(field, **args)
|
||||
|
||||
return nil if value.nil? || monetizable_currency.nil?
|
||||
return nil if value.blank? || monetizable_currency.nil?
|
||||
|
||||
Money.new(value, monetizable_currency)
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
---
|
||||
en:
|
||||
valuations:
|
||||
errors:
|
||||
amount_required: Amount is required
|
||||
form:
|
||||
amount: Amount
|
||||
submit: Add balance update
|
||||
|
||||
@@ -50,4 +50,31 @@ class ValuationsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_equal 22000, @entry.amount
|
||||
assert_equal "Test notes", @entry.notes
|
||||
end
|
||||
|
||||
test "confirm_create with blank amount returns unprocessable entity" do
|
||||
account = accounts(:investment)
|
||||
|
||||
post confirm_create_valuations_url, params: {
|
||||
entry: {
|
||||
amount: "",
|
||||
date: Date.current.to_s,
|
||||
account_id: account.id
|
||||
}
|
||||
}
|
||||
|
||||
assert_response :unprocessable_entity
|
||||
assert_match I18n.t("valuations.errors.amount_required"), response.body
|
||||
end
|
||||
|
||||
test "confirm_update with blank amount returns unprocessable entity" do
|
||||
post confirm_update_valuation_url(@entry), params: {
|
||||
entry: {
|
||||
amount: "",
|
||||
date: Date.current.to_s
|
||||
}
|
||||
}
|
||||
|
||||
assert_response :unprocessable_entity
|
||||
assert_match I18n.t("valuations.errors.amount_required"), response.body
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user