Skip to content

Conversation

@Juhitha-Reddy
Copy link

@Juhitha-Reddy Juhitha-Reddy commented Sep 12, 2025

Backend PR — Injury Trend APIs + POST injuries

Description

Adds backend support for Injury Trend line chart and provides a production POST endpoint for creating injury records. Trend data returns monthly counts per severity across a date range and project.

Implements #Phase2-InjuryTrend-API

Related PRs (if any):

Main changes explained:

  • New/updated endpoints under /api/bm/injuries:
    • GET /trend-data: returns { months, serious, medium, low } aggregated by month and severity, filtered by projectId, startDate, endDate (inclusive start, month-exclusive end for YYYY-MM-DD).
    • GET /project-injury: lists projects that have injuries (for Project dropdown).
    • GET /injury-severities / GET /injury-types: supporting datasets.
    • POST / (i.e. /api/bm/injuries): create injuries (single or bulk).
  • Data model:
    • Uses existing InjuryCategory schema:
      • projectId: ObjectId (required)
      • projectName: string
      • date: Date (required)
      • injuryType: string
      • workerCategory: string
      • severity: 'Serious' | 'Medium' | 'Low' (required)
      • count: number (default 1, ≥ 0)
  • Validation:
    • projectId must be a valid ObjectId
    • date must parse (YYYY‑MM‑DD or ISO)
    • severity normalized (case-insensitive) to “Serious | Medium | Low”
    • Returns 400 on validation errors; 201 on success with { insertedCount, docs }
  • ESLint fixes:
    • Camel case for helpers
    • Replaced restricted for...of with array iteration

How to test:

  1. cd HGNRest && npm install
  2. npm run build && NODE_ENV=development PORT=4500 node dist/server.js
  3. POST (create injuries) via Postman:
    • URL: http://localhost:4500/api/bm/injuries
    • Headers: Authorization: <JWT>, Content-Type: application/json
    • Body (raw JSON):
[
  {
    "projectId": "65419e61105441587e2dec99",
    "projectName": "Building 1",
    "date": "2025-02-05",
    "injuryType": "Fall",
    "workerCategory": "Electrician",
    "severity": "Serious",
    "count": 2
  },
  {
    "projectId": "65419e61105441587e2dec99",
    "projectName": "Building 1",
    "date": "2025-03-01",
    "injuryType": "Cut",
    "workerCategory": "Carpenter",
    "severity": "Medium",
    "count": 1
  }
]
  1. GET trend data:
    • GET http://localhost:4500/api/bm/injuries/trend-data?projectId=65419e61105441587e2dec99&startDate=2025-01-01&endDate=2025-12-31
    • Expect: { months: [...], serious: [...], medium: [...], low: [...] }
  2. Confirm the frontend chart updates when you select the same project and date range.

Screenshots or videos of changes:

Uploading Screen Recording 2025-09-12 at 1.01.13 PM.mov…

Copy link

@aditya2512 aditya2512 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR provides a strong and well-architected backend solution for supporting the Injury Trend chart feature. The new endpoints efficiently aggregate injury data by month and severity, returning results in a frontend-ready format that will streamline data visualization and analytics. The validation logic on fields like projectId, date, and severity is rigorous, promoting data integrity in both POST and GET operations. I appreciate that error messages are clear, and the design allows both single and bulk injury creation via a flexible API payload. Before merging, please resolve the outstanding branch conflicts and consider providing brief API endpoint documentation for frontend and QA reference. Overall, this is a well-tested and maintainable addition that will meaningfully improve the project’s reporting capabilities.

@one-community one-community added the High Priority - Please Review First This is an important PR we'd like to get merged as soon as possible label Dec 22, 2025
Copy link

@Anusha-Gali Anusha-Gali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Juhitha,

I have reviewed your PR locally and the backend works as expected.
Screenshot 2025-12-23 at 11 39 08 PM
Screenshot 2025-12-23 at 11 40 14 PM
Screenshot 2025-12-23 at 11 41 57 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

High Priority - Please Review First This is an important PR we'd like to get merged as soon as possible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants