feat(api): expose budget state (#1640)

* feat(api): expose budget state

* fix(api): guard malformed budget ids

* fix(api): address budget state review

* fix(api): address budget state review

* fix(api): document budget id formats

* fix(api): align budget category docs auth

* fix(api): lighten budget category index payload

* fix(api): use shared pagination clamp

* fix(api): centralize budget filter handling
This commit is contained in:
ghost
2026-05-06 12:50:46 -06:00
committed by GitHub
parent 4b93bdb447
commit 2d38cfb011
18 changed files with 1442 additions and 20 deletions

View File

@@ -0,0 +1,34 @@
# frozen_string_literal: true
money_to_minor_units = lambda do |money|
(money.amount * money.currency.minor_unit_conversion).round(0).to_i if money
end
include_derived_amounts = local_assigns.fetch(:include_derived_amounts, true)
json.id budget_category.id
json.budget_id budget_category.budget_id
json.currency budget_category.currency
json.subcategory budget_category.subcategory?
json.inherits_parent_budget budget_category.inherits_parent_budget?
json.budgeted_spending budget_category.budgeted_spending_money.format
json.budgeted_spending_cents money_to_minor_units.call(budget_category.budgeted_spending_money)
json.display_budgeted_spending Money.new(budget_category.display_budgeted_spending, budget_category.currency).format
json.display_budgeted_spending_cents money_to_minor_units.call(Money.new(budget_category.display_budgeted_spending, budget_category.currency))
if include_derived_amounts
json.actual_spending budget_category.actual_spending_money.format
json.actual_spending_cents money_to_minor_units.call(budget_category.actual_spending_money)
json.available_to_spend budget_category.available_to_spend_money.format
json.available_to_spend_cents money_to_minor_units.call(budget_category.available_to_spend_money)
end
json.category do
json.id budget_category.category.id
json.name budget_category.category.name
json.color budget_category.category.color
json.lucide_icon budget_category.category.lucide_icon
json.parent_id budget_category.category.parent_id
end
json.created_at budget_category.created_at.iso8601
json.updated_at budget_category.updated_at.iso8601

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
json.budget_categories @budget_categories do |budget_category|
json.partial! "budget_category", budget_category: budget_category, include_derived_amounts: false
end
json.pagination do
json.page @pagy.page
json.per_page @per_page
json.total_count @pagy.count
json.total_pages @pagy.pages
end

View File

@@ -0,0 +1,3 @@
# frozen_string_literal: true
json.partial! "budget_category", budget_category: @budget_category

View File

@@ -0,0 +1,36 @@
# frozen_string_literal: true
money_to_minor_units = lambda do |money|
(money.amount * money.currency.minor_unit_conversion).round(0).to_i if money
end
include_derived_amounts = local_assigns.fetch(:include_derived_amounts, true)
json.id budget.id
json.start_date budget.start_date
json.end_date budget.end_date
json.name budget.name
json.currency budget.currency
json.initialized budget.initialized?
json.current budget.current?
json.budgeted_spending budget.budgeted_spending_money&.format
json.budgeted_spending_cents money_to_minor_units.call(budget.budgeted_spending_money)
json.expected_income budget.expected_income_money&.format
json.expected_income_cents money_to_minor_units.call(budget.expected_income_money)
json.allocated_spending budget.allocated_spending_money.format
json.allocated_spending_cents money_to_minor_units.call(budget.allocated_spending_money)
if include_derived_amounts
json.actual_spending budget.actual_spending_money.format
json.actual_spending_cents money_to_minor_units.call(budget.actual_spending_money)
json.actual_income budget.actual_income_money.format
json.actual_income_cents money_to_minor_units.call(budget.actual_income_money)
json.available_to_spend budget.available_to_spend_money.format
json.available_to_spend_cents money_to_minor_units.call(budget.available_to_spend_money)
json.available_to_allocate budget.available_to_allocate_money.format
json.available_to_allocate_cents money_to_minor_units.call(budget.available_to_allocate_money)
end
json.created_at budget.created_at.iso8601
json.updated_at budget.updated_at.iso8601

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
json.budgets @budgets do |budget|
json.partial! "budget", budget: budget, include_derived_amounts: false
end
json.pagination do
json.page @pagy.page
json.per_page @per_page
json.total_count @pagy.count
json.total_pages @pagy.pages
end

View File

@@ -0,0 +1,3 @@
# frozen_string_literal: true
json.partial! "budget", budget: @budget