Add budget overspend alert notification (#74)#107
Merged
Uyoxy merged 1 commit intoJun 24, 2026
Merged
Conversation
9 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR implements budget overspend notifications that alert users when they approach or exceed their budget limits. Previously, the budgets service did not send any notifications when a user exceeded or was close to their budget limits.
Now, whenever budgets are queried or updated, the system checks the current spending (summing completed transactions within the budget category, asset, and date range) and triggers notifications via NotificationsService at two key thresholds:
80% Budget Usage (Warning)
100% Budget Usage (Error/Limit Exceeded)
To prevent duplicate alerts, the system checks the database to verify if a notification has already been created for that specific threshold and budget ID.
Proposed Changes
Notifications Module
src/modules/notifications/notifications.service.ts:
Added findOneByCategory(userId, category) to check if a specific threshold notification already exists for the budget.
Added createBudgetAlertNotification(userId, budgetId, categoryName, limit, usagePercent) to create template-based alert notifications for 80% and 100% usage thresholds.
Budgets Module
src/modules/budgets/budgets.module.ts:
Imported TypeOrmModule.forFeature([Transaction]) and NotificationsModule to inject dependencies into BudgetsService.
src/modules/budgets/budgets.service.ts:
Modified the constructor to accept optional notificationsService and transactionRepository parameters. Using optional dependencies keeps compatibility with existing unit tests.
Implemented checkAndTriggerOverspendAlerts(budget) to calculate the total spent amount for a budget's active period and category, triggering notifications if the 80% or 100% thresholds are met.
Integrated the overspend alert check into the findById, findByUserId, create, and update methods.
Tests
src/modules/budgets/budgets.service.spec.ts:
Added unit test cases to verify the overspend check triggers correctly at 80% and 100% usage, avoids duplicate notifications on repeated checks, and runs safely without throwing when dependencies are not present.
Verification & Testing
Automated Tests
Ran the unit tests for the budgets service, and all 105 tests passed successfully:
bash
npx jest src/modules/budgets/budgets.service.spec.ts
Output:
Test Suites: 1 passed, 1 total
Tests: 105 passed, 105 total
Snapshots: 0 total
Time: 5.345 s
Linting
Verified that all modified files comply with the codebase's strict lint rules:
bash
npm run lint
Output:
Zero linting errors or warnings in the modified files.
close #74