-
-
Notifications
You must be signed in to change notification settings - Fork 40
Aditya-feat: Implement Paid Labor Cost API endpoint with comprehensive filtering and validation #1937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aditya-gam
wants to merge
19
commits into
development
Choose a base branch
from
Aditya-fix/implement-paid-labor-cost-controller
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Aditya-feat: Implement Paid Labor Cost API endpoint with comprehensive filtering and validation #1937
Aditya-gam
wants to merge
19
commits into
development
from
Aditya-fix/implement-paid-labor-cost-controller
Conversation
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
…st controller - Import LaborCost model for database queries - Import logger utility for error tracking - Wrap controller function in try-catch block for error handling - Set up foundation for complete controller implementation
…troller - Extract projects, tasks, and date_range from query parameters - Add helper functions to parse arrays and date_range objects from query strings - Validate projects and tasks are arrays with string values - Validate date_range is object or null - Validate ISO 8601 date format for start_date and end_date - Validate date range logic (start_date <= end_date) - Return appropriate error responses (400, 422) for validation failures
- Build date filter with support for null date_range, partial ranges (only start or end), and full ranges - Use MongoDB operators $gte and $lte for date filtering with inclusive boundaries - Build project filter using $in operator when projects array has values - Build task filter using $in operator when tasks array has values - Combine all filters into single query object with AND logic - Convert date strings to Date objects and set appropriate time boundaries for inclusive filtering
- Execute MongoDB query using LaborCost.find() with combined filters
- Sort results chronologically by date using .sort({ date: 1 })
- Use .lean() for better query performance
- Return individual records without aggregation (frontend handles grouping)
- Database errors handled gracefully by existing try-catch block
- Map database fields to frontend structure (project_name → project) - Convert date objects to ISO 8601 string format - Ensure cost values are numbers (not strings) - Calculate totalCost as sum of all cost values - Return response with totalCost and data array structure - Handle empty results case (totalCost = 0)
- Explicitly construct response object with totalCost and data fields - Return 200 OK status code on successful response - Handle empty results case (totalCost: 0, data: []) - Ensure response is valid JSON using res.json() method - Add clear comments documenting response structure
- Distinguish between database errors and unexpected errors - Add error context logging with request parameters (query, method, URL) - Return user-friendly error messages without exposing internal details - Handle MongoDB/database connection errors with specific error code - Maintain existing validation error handling (400, 422 status codes) - Log errors with sufficient detail for debugging using logger utility
- Explicitly handle null/undefined request query parameters - Enhance date validation to catch invalid months/days (e.g., "2025-13-01") - Document empty results handling (valid case, not error) - Document partial date range support (open-ended ranges) - Add comments clarifying edge case behaviors - Ensure dates far in past/future return empty results gracefully - Validate date components match parsed values to prevent silent adjustments
- Update route to use GET method as per frontend requirements - Controller already reads from req.query which is appropriate for GET requests
…t controller - Fix date validation to use UTC methods for accurate date-only string comparison - Add validation to detect JSON parsing failures for date_range parameter - Add validation to ensure date values are strings (not numbers or other types) - Improve error messages with examples for invalid date_range JSON format - Add comments clarifying UTC method usage for date-only string parsing
- Add validation to detect JSON objects provided instead of arrays - Add validation to detect mixed formats (JSON objects/arrays with plain strings) - Add helper function to detect JSON-like strings in comma-separated values - Return descriptive error messages for invalid array parameter formats - Improve validation for both projects and tasks array parameters
…troller - Change setHours to setUTCHours for start date boundary - Change setHours to setUTCHours for end date boundary - Add comments clarifying UTC method usage for consistent timezone handling - Ensure date filters work correctly across different timezones
- Export helper functions via testExports for direct unit testing - Add comprehensive tests for looksLikeJson() (12 test cases) - Add comprehensive tests for parseArrayParam() (15 test cases) - Add comprehensive tests for parseDateRangeParam() (10 test cases) - Add comprehensive tests for isValidDateValue() (22 test cases) - All 59 test cases passing, covering edge cases and error conditions
- Add comprehensive tests for parameter validation (projects, tasks, date_range) - Add tests for MongoDB query building with various filter combinations - Add tests for response formatting and data transformation - Add tests for error handling (database errors and unexpected errors) - Achieve 97% code coverage with 105 test cases - All tests passing, covering edge cases and error conditions
- Add authorization tests for GET /api/labor-cost (401 without auth) - Add route functionality tests (POST method, route existence) - Follow same pattern as existing integration tests - Tests verify route configuration and authentication requirements - Install @aws-sdk/client-s3 dependency required for integration tests
- Add boundary condition tests (empty strings, long names, Unicode, special chars) - Add tests for large arrays (100+ projects) - Add tests for dates far in past/future - Add request object edge case tests (missing/null/empty query) - All 115 tests passing with 97% coverage maintained
- Add comprehensive JSDoc comments and section headers to test files - Consolidate request object edge case tests into parameterized test - Maintain 97% coverage (well above 90% target) - All 119 tests passing - Improve test file readability and maintainability
…for paid labor cost controller
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.
Description
Implements a complete backend API for the Paid Labor Cost grouped-bar graph feature. The controller was previously a stub returning an empty array. This PR provides a fully functional

GET /api/labor-costendpoint supporting date filtering, project filtering, task filtering, and proper data formatting with comprehensive validation and error handling.Related PRs (if any):
Main changes explained:
Created/Updated Files:
src/controllers/bmdashboard/bmPaidLaborCostController.jsComplete controller implementation with:
looksLikeJson(),parseArrayParam(),parseDateRangeParam(),isValidDateValue()projects,tasks,date_rangefrom query params$inoperatorLaborCost.find()with.lean()and.sort({ date: 1 })project_name→project) and calculatestotalCostsrc/routes/bmdashboard/bmPaidLaborCostRouter.jsPOSTtoGETto align with frontend requirements.src/controllers/bmdashboard/__tests__/bmPaidLaborCostController.test.js119 unit tests covering:
src/__tests__/integration/bmPaidLaborCostRoutes.integration.test.js(+113 lines)Integration tests for route configuration, authentication (401 without auth), and GET method verification.
package.json@aws-sdk/client-s3dependency for integration tests.Key Implementation Details:
API Endpoint:
GET /api/labor-cost.Request Parameters (query string):
projects: Optional array (empty = all projects)tasks: Optional array (empty = all tasks)date_range: Optional object withstart_dateand/orend_date(null = all dates)Response Structure:
{ "totalCost": 15000, "data": [ { "project": "Project A", "task": "Task 1", "date": "2025-04-01T00:00:00.000Z", "cost": 5000 } ] }Key Features:
How to test:
Aditya-fix/implement-paid-labor-cost-controller.rm -rf node_modules package-lock.json && npm cache clean --force.npm installto install dependencies, then start the backend locally (npm startornpm run dev)GET /api/labor-cost:Basic functionality:
Edge cases:
Screenshots or videos of changes:
TestVideo.mov
Note:
.lean()for better performance. Consider adding indexes ondate,project_name, andtaskif the dataset is very large