Files
sure/test/models/family/month_start_day_test.rb
MkDev11 68efe71cdb feat: Customizable Budget Month Start Day (#810)
* Add customizable budget month start day (#253)

Allow users to set a custom month-to-date start date (1st-28th) for
budgeting and MTD calculations. Useful for users who want budget
periods aligned with their pay schedule (e.g., 25th to 24th).

Changes:
- Add month_start_day column to families table (default: 1)
- Add database check constraint for valid range (1-28)
- Add Family#uses_custom_month_start?, custom_month_start_for,
  custom_month_end_for, current_custom_month_period helper methods
- Add Period.current_month_for(family), last_month_for(family) methods
- Update Budget model for custom month boundaries in find_or_bootstrap,
  param_to_date, budget_date_valid?, current?, and name methods
- Add month_start_day setting to Settings > Preferences UI
- Add warning message when custom month start day is configured
- Add comprehensive tests with travel_to for date robustness

Fixes #253

* Add /api/v1/user endpoint for Flutter mobile app and PWA

Expose user preferences including month_start_day via API endpoint
following existing pattern for default_period. This allows Flutter
mobile app and PWA to read/update user preferences through a
consistent API contract.

Endpoints:
- GET /api/v1/user - Read user preferences including family settings
- PATCH /api/v1/user - Update user preferences

Response includes: id, email, first_name, last_name, default_period,
locale, and family settings (currency, timezone, date_format, country,
month_start_day).

* Update Periodable to use family-aware MTD periods

When users select 'current_month' or 'last_month' period filters on
dashboard/reports, now respects the family's custom month_start_day
setting instead of using static calendar month boundaries.

This ensures MTD filter on dashboard is consistent with how budgets
calculate their periods when custom month start day is configured.

* Fix param_to_date to correctly map budget params to custom periods

When a family uses a custom start day, the previous implementation
called custom_month_start_for on the 1st of the month, which incorrectly
shifted dates before the start day to the previous month.

Now we directly construct the date using family.month_start_day, so
'jan-2026' with month_start_day=25 correctly returns Jan 25, 2026
instead of Dec 25, 2025.

* Fix param_to_date and use Current pattern in API controller

- Fix param_to_date to directly construct date with family.month_start_day
  instead of using custom_month_start_for which incorrectly shifted dates
- Replace current_user with Current.user/Current.family in API controller
  to follow project convention used in other API v1 controllers

* Add i18n for budget name method

Use I18n.t for localizable budget period names to follow
project conventions for user-facing strings.

* Remove unused budget_end variable in budget_date_valid?

* Use Date.current for timezone consistency in Budget#current?

* Address PR review feedback

- Remove API users endpoint (mobile won't use yet)
- Remove user route from config/routes.rb
- Remove ai_summary/document_type schema bleed from pdf-import-ai branch

* Pass family to param_to_date for custom month logic

* Run migration to add month_start_day column to schema

* Schema regressions

---------

Co-authored-by: mkdev11 <jaysmth689+github@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-02-04 23:58:09 +01:00

71 lines
1.9 KiB
Ruby

require "test_helper"
class Family::MonthStartDayTest < ActiveSupport::TestCase
setup do
@family = families(:dylan_family)
end
test "month_start_day defaults to 1" do
assert_equal 1, @family.month_start_day
end
test "validates month_start_day is between 1 and 28" do
@family.month_start_day = 0
assert_not @family.valid?
@family.month_start_day = 29
assert_not @family.valid?
@family.month_start_day = 15
assert @family.valid?
end
test "uses_custom_month_start? returns false when month_start_day is 1" do
@family.month_start_day = 1
assert_not @family.uses_custom_month_start?
end
test "uses_custom_month_start? returns true when month_start_day is not 1" do
@family.month_start_day = 25
assert @family.uses_custom_month_start?
end
test "custom_month_start_for returns correct start date when day is after month_start_day" do
@family.month_start_day = 15
travel_to Date.new(2026, 1, 20) do
result = @family.custom_month_start_for(Date.current)
assert_equal Date.new(2026, 1, 15), result
end
end
test "custom_month_start_for returns correct start date when day is before month_start_day" do
@family.month_start_day = 15
travel_to Date.new(2026, 1, 10) do
result = @family.custom_month_start_for(Date.current)
assert_equal Date.new(2025, 12, 15), result
end
end
test "custom_month_end_for returns one day before next custom month start" do
@family.month_start_day = 15
travel_to Date.new(2026, 1, 20) do
result = @family.custom_month_end_for(Date.current)
assert_equal Date.new(2026, 2, 14), result
end
end
test "current_custom_month_period returns correct period" do
@family.month_start_day = 25
travel_to Date.new(2026, 1, 27) do
period = @family.current_custom_month_period
assert_equal Date.new(2026, 1, 25), period.start_date
assert_equal Date.new(2026, 2, 24), period.end_date
end
end
end