Skip to content

Conversation

@nmokaria27
Copy link
Contributor

@nmokaria27 nmokaria27 commented Nov 21, 2025

Closes #62

Overview

This PR implements sorting and filtering capabilities for the nonprofits GraphQL endpoint. The goal was to enable users to sort nonprofits alphabetically (A-Z and Z-A), by most recent project start date, and by status (ACTIVE/INACTIVE), as well as filter nonprofits by chapter IDs and status.

Key Changes:

  • Added NonprofitSortOption enum with four sorting options: A_TO_Z, Z_TO_A, MOST_RECENT, and STATUS
  • Implemented derived status computation logic that determines if a nonprofit is ACTIVE or INACTIVE based on its associated projects
  • Added status field to the Nonprofit GraphQL type
  • Enhanced the nonprofits query to accept optional chapterIds, statuses, and sort parameters
  • Implemented multi-level sorting that allows combining multiple sort options in order of precedence
  • Added comprehensive filtering by chapter IDs and status types

Implementation Details:

  • Status is derived dynamically from nonprofit_chapter_project relationships (ACTIVE if there's an ongoing project, INACTIVE otherwise)
  • Sorting is performed in-memory after fetching nonprofits to support complex multi-level sorting
  • All query parameters are optional for backward compatibility
  • Chapter filtering uses Prisma relation filters for efficiency

Testing

Unit Tests:

  • Added comprehensive unit tests in tests/unit/services/nonprofits.service.test.ts covering:
    • Status derivation logic (ACTIVE vs INACTIVE based on project dates and status)
    • A-Z and Z-A alphabetical sorting
    • Most recent sorting (by latest project start date)
    • Status sorting (ACTIVE before INACTIVE)
    • Combined multi-level sorting (e.g., STATUS + MOST_RECENT + A_TO_Z)
    • Chapter filtering
    • Status filtering
    • Edge cases (nonprofits without projects, multiple projects, etc.)

Integration Tests:

  • Added new integration test file tests/integration/graphql/nonprofits-sorting.test.ts with end-to-end GraphQL tests:
    • A to Z sorting
    • Z to A sorting
    • Most recent sorting
    • Status sorting
    • Status filtering (ACTIVE only)
    • Chapter filtering
    • Combined sorting (STATUS + MOST_RECENT)

All tests pass successfully. The implementation maintains backward compatibility - existing queries without the new parameters continue to work as before.

Screenshots / Screencasts

N/A - This is a backend API change with no frontend components affected.

Checklist

  • Code is neat, readable, and works
  • Code is commented where appropriate and well-documented
  • Commit messages follow our guidelines
  • Issue number is linked
  • Branch is linked
  • Reviewers are assigned (one of your tech leads)

Notes

  • The status field is computed dynamically on each query rather than being persisted in the database, which ensures accuracy but may have performance implications for very large datasets
  • Nonprofits without any projects are considered INACTIVE
  • When sorting by MOST_RECENT, nonprofits without projects appear last
  • The implementation follows the existing codebase patterns and maintains consistency with other similar features (e.g., project sorting)
  • All linting errors have been resolved and the code passes ESLint checks

- Removed duplicate StatusType enum definition from nonprofits schema
- Moved chapter and project creation from beforeAll to beforeEach to handle global test cleanup
- Added null checks in afterAll cleanup to prevent deletion errors
- Fixed founded_date format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants