Skip to content

Global entity search implementation#75

Merged
benmed00 merged 10 commits intomainfrom
cursor/global-entity-search-implementation-8e2a
Jan 10, 2026
Merged

Global entity search implementation#75
benmed00 merged 10 commits intomainfrom
cursor/global-entity-search-implementation-8e2a

Conversation

@benmed00
Copy link
Owner

@benmed00 benmed00 commented Jan 9, 2026

Global Entity Search Implementation & Critical Platform Enhancements

🎯 Overview

This PR implements a comprehensive global entity search feature that allows users to search across all entities in the platform, while also addressing critical priorities including testing infrastructure, performance optimization, security enhancements, and extended export functionality. This is a major feature enhancement that significantly improves the platform's usability and developer experience.

🔍 Context

Problem Statement

  1. Search Functionality: Users needed a unified search interface to find entities across the entire platform without navigating to specific modules
  2. Testing Infrastructure: Lack of comprehensive test coverage for critical utilities and API routes
  3. Performance: API routes needed caching and standardized pagination
  4. Security: Export endpoints required rate limiting to prevent abuse
  5. Export Capabilities: Limited export formats (only Excel/PDF, missing CSV)

Impact

  • ✅ Unified search experience across all entities
  • ✅ Comprehensive test coverage for critical components
  • ✅ Improved API performance with caching
  • ✅ Enhanced security with rate limiting
  • ✅ Extended export functionality (CSV support)

✨ Changes Made

1. Global Entity Search Implementation

Core Search API (src/app/api/search/route.ts):

  • Unified search endpoint that searches across 11 entity types:
    • Species, Missions, Equipment, Employees, Documents, Publications
    • Users, Expenses, Budgets, Water Quality, Air Quality, Climate Data
  • Intelligent query matching across multiple fields
  • Configurable result limits and entity type filtering
  • Rate limiting for security
  • Dynamic route configuration (force-dynamic)

Search UI Component (src/components/search/global-search.tsx):

  • Real-time search with debouncing
  • Beautiful, modern UI with entity type icons
  • Keyboard navigation support
  • Result grouping by entity type
  • Quick navigation to entity details
  • Integrated into main header

Integration:

  • Search component added to main layout header
  • Accessible from anywhere in the application
  • Responsive design for mobile and desktop

2. Comprehensive Testing Infrastructure

Unit Tests:

  • src/lib/password-policy.test.ts - 29 tests covering password validation, expiration, history, lockout, strength
  • src/lib/two-factor.test.ts - 27 tests covering 2FA secret generation, QR codes, verification, backup codes
  • src/lib/export-utils.test.ts - 9 tests covering export filename generation, blob downloading, API calls

Integration Tests:

  • src/app/api/search/route.test.ts - 4 tests covering search functionality, authentication, rate limiting
  • src/app/api/export/excel/route.test.ts - 3 tests covering Excel export, error handling

E2E Tests (Playwright):

  • e2e/auth.spec.ts - User login and logout flows
  • e2e/search.spec.ts - Global search functionality
  • e2e/export.spec.ts - Excel, PDF, and CSV export functionality

3. Performance Optimizations

HTTP Caching:

  • Added Cache-Control headers to /api/air-quality route
  • 5-minute cache with stale-while-revalidate strategy

Standardized Pagination:

  • Implemented parsePagination and createPaginatedResponse utilities
  • Applied to /api/documents and /api/publications routes
  • Consistent pagination API across the platform

4. Security Enhancements

Rate Limiting:

  • Applied withRateLimit wrapper to all export endpoints:
    • /api/export/excel
    • /api/export/pdf
    • /api/export/csv
  • Prevents abuse and ensures fair resource usage

Two-Factor Authentication (2FA):

  • Complete 2FA implementation with TOTP support
  • QR code generation for easy setup
  • Backup codes for account recovery
  • Enable/disable functionality
  • Security settings UI page

Password Management:

  • Password change API endpoint
  • Password policy enforcement
  • Secure password hashing

5. Extended Export Functionality

CSV Export:

  • New /api/export/csv route
  • Supports all entity types
  • Flat data structure for easy import
  • Rate limiting applied

Export Buttons Component:

  • Updated to include CSV export option
  • Consistent UI across all pages
  • Loading states and error handling

Export Types Extended:

  • Added waterQuality, airQuality, climateData to ExportType
  • Support for all export formats (Excel, PDF, CSV)

6. Client Page Components

Converted several server components to client components for better interactivity:

  • src/app/dashboard/documents/client-page.tsx
  • src/app/dashboard/environment/climate/client-page.tsx
  • src/app/dashboard/publications/client-page.tsx
  • src/app/dashboard/rh/employees/client-page.tsx
  • src/app/dashboard/environment/water/client-page.tsx
  • src/app/dashboard/environment/air/client-page.tsx

🔗 Related Work

Related PRs

Milestone

  • v1.1 - Performance & User Experience (Milestone chore(deps): bump actions/upload-artifact from 4 to 6 #2): This PR is a major contribution to this milestone
    • Global search significantly improves user experience
    • Performance optimizations enhance platform speed
    • Export functionality improves usability

Branch Information

  • Base Branch: main (commit: 43c2e3c)
  • Head Branch: cursor/global-entity-search-implementation-8e2a (commit: e6a96a5)
  • Status: Up-to-date with main, ready for merge

🧪 Testing

Test Coverage

  • 98 unit and integration tests passing
  • E2E tests for critical user flows
  • Linting passes
  • Type checking passes
  • Build completes successfully

Manual Testing

  1. Global Search:

    • Open search from header
    • Search for entities across different types
    • Verify results are grouped correctly
    • Test keyboard navigation
  2. Export Functionality:

    • Test Excel, PDF, and CSV exports
    • Verify rate limiting works
    • Check export file formats
  3. 2FA:

    • Set up 2FA from security settings
    • Verify QR code generation
    • Test backup codes
    • Verify enable/disable functionality

📊 Metrics & Impact

Before

  • ❌ No unified search across entities
  • ❌ Limited test coverage
  • ❌ No caching on API routes
  • ❌ No rate limiting on exports
  • ❌ Only Excel/PDF export formats

After

  • ✅ Global search across 11 entity types
  • ✅ Comprehensive test suite (98 tests)
  • ✅ HTTP caching on key routes
  • ✅ Rate limiting on all export endpoints
  • ✅ CSV export support
  • ✅ Complete 2FA implementation

🚀 Deployment Notes

Breaking Changes

  • None: All changes are backward compatible

Migration Steps

  • No migration required
  • Search is automatically available in header
  • Export buttons automatically include CSV option

Rollback Plan

  • Revert PR if issues arise
  • All changes are feature additions, no breaking changes

📝 Code Quality

Standards Met

  • ✅ Follows project coding standards
  • ✅ TypeScript types are correct
  • ✅ ESLint rules pass
  • ✅ No console.logs or debug code
  • ✅ Proper error handling
  • ✅ Code is self-documenting

Files Changed

  • 58 files modified/added
  • +5,909 additions
  • -1,126 deletions

Key Files

  • src/app/api/search/route.ts - Global search API
  • src/components/search/global-search.tsx - Search UI component
  • src/lib/password-policy.test.ts - Password policy tests
  • src/lib/two-factor.test.ts - 2FA tests
  • src/app/api/export/csv/route.ts - CSV export endpoint
  • e2e/*.spec.ts - E2E test suite

✅ Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Code is commented where necessary
  • No new warnings generated
  • Tests pass locally (98/98)
  • No sensitive data included
  • No debug code left in
  • Build completes successfully
  • Linting passes
  • Type checking passes

👥 Reviewers

@benmed00 - Please review for:

  • Global search implementation and UX
  • Test coverage and quality
  • Performance optimizations
  • Security enhancements
  • Overall code quality and standards

📚 Additional Notes

Technical Details

Search Implementation:

  • Uses Prisma's contains with case-insensitive mode for flexible matching
  • Parallel queries for performance
  • Configurable limits per entity type
  • Rate limiting prevents abuse

2FA Implementation:

  • Uses otplib for TOTP generation
  • QR codes generated using qrcode library
  • Backup codes stored securely
  • Session-based verification

Export Implementation:

  • CSV uses papaparse for generation
  • Excel uses existing /api/export/excel endpoint
  • PDF uses existing /api/export/pdf endpoint
  • All exports include rate limiting

🔄 Future Improvements

Potential enhancements for future PRs:

  • Search result highlighting
  • Search history
  • Advanced search filters
  • Search analytics
  • Export templates
  • Bulk export operations

🔗 Resolved Issues

This PR resolves the following issues:

Closes #79 - Implement global entity search across all platform entities
Closes #80 - Add comprehensive testing infrastructure (unit, integration, E2E)

This PR addresses global search, testing, performance, security, and export functionality. If specific GitHub issues exist for these features, they should be linked below using Closes #XX or Fixes #XX.

Note: To see issues in the GitHub "Development" section, add explicit Closes #XX, Fixes #XX, or Resolves #XX references.


Type: ✨ Feature + 🧪 Testing + ⚡ Performance + 🔒 Security
Priority: High
Module: Core Platform
Milestone: v1.1 - Performance & User Experience

@cursor
Copy link

cursor bot commented Jan 9, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@benmed00 benmed00 added bug Something isn't working module:hr Human resources priority:high High priority type:bug Bug fix type:documentation Documentation type:feature New feature type:performance Performance improvement type:security Security type:testing Testing labels Jan 9, 2026
@benmed00 benmed00 self-assigned this Jan 9, 2026
@benmed00 benmed00 mentioned this pull request Jan 10, 2026
14 tasks
benmed00 added a commit that referenced this pull request Jan 10, 2026
Build error and timing

This PR resolves build errors and introduces build performance monitoring.

## Changes

- Fixed 'Dynamic server usage' build errors by adding `export const dynamic = 'force-dynamic'` to 5 API routes
- Added build timing script (`scripts/time-build.js`) for performance monitoring
- Removed empty test files causing test failures
- Resolved merge conflicts with main branch

## Impact

- ✅ Clean builds without errors or warnings
- ✅ Build performance metrics available
- ✅ All tests passing (98/98)
- ✅ Improved developer experience

## Related

- Closes #77 - Fix Next.js build errors: Dynamic server usage warnings
- Closes #78 - Add build performance monitoring and timing
- Related to PR #76 (Performance optimizations)
- Related to PR #75 (Global search)
- Milestone: v1.3 - Quality & Polish

## Technical Details

- Files changed: 26
- Additions: +1,082
- Deletions: -712
- Commits: 4

PR #72
@cursor cursor bot force-pushed the cursor/global-entity-search-implementation-8e2a branch from 5476de2 to 743316e Compare January 10, 2026 00:29
@github-actions github-actions bot removed the bug Something isn't working label Jan 10, 2026
@benmed00 benmed00 added enhancement New feature or request module:core and removed type:bug Bug fix type:documentation Documentation module:hr Human resources labels Jan 10, 2026
@cursor cursor bot force-pushed the cursor/global-entity-search-implementation-8e2a branch from c619840 to 121a4d2 Compare January 10, 2026 00:33
cursor bot pushed a commit that referenced this pull request Jan 10, 2026
Build error and timing

This PR resolves build errors and introduces build performance monitoring.

## Changes

- Fixed 'Dynamic server usage' build errors by adding `export const dynamic = 'force-dynamic'` to 5 API routes
- Added build timing script (`scripts/time-build.js`) for performance monitoring
- Removed empty test files causing test failures
- Resolved merge conflicts with main branch

## Impact

- ✅ Clean builds without errors or warnings
- ✅ Build performance metrics available
- ✅ All tests passing (98/98)
- ✅ Improved developer experience

## Related

- Closes #77 - Fix Next.js build errors: Dynamic server usage warnings
- Closes #78 - Add build performance monitoring and timing
- Related to PR #76 (Performance optimizations)
- Related to PR #75 (Global search)
- Milestone: v1.3 - Quality & Polish

## Technical Details

- Files changed: 26
- Additions: +1,082
- Deletions: -712
- Commits: 4

PR #72
cursoragent and others added 5 commits January 10, 2026 00:34
Co-authored-by: benyakoub.pro <benyakoub.pro@gmail.com>
- Remove src/lib/validation-helpers.test.ts (empty)
- Remove src/lib/pagination.test.ts (empty)
- Fix apostrophe escaping in security page
- Add eslint-disable for QR code image
Co-authored-by: benyakoub.pro <benyakoub.pro@gmail.com>
- Fix import path to @/components/search/global-search
- Restore required pageSize and totalItems props for Pagination component
@cursor cursor bot force-pushed the cursor/global-entity-search-implementation-8e2a branch from 121a4d2 to cb2e300 Compare January 10, 2026 00:34
Co-authored-by: benyakoub.pro <benyakoub.pro@gmail.com>
@github-actions github-actions bot removed the enhancement New feature or request label Jan 10, 2026
@benmed00 benmed00 closed this Jan 10, 2026
@benmed00 benmed00 deleted the cursor/global-entity-search-implementation-8e2a branch January 10, 2026 00:42
@benmed00 benmed00 restored the cursor/global-entity-search-implementation-8e2a branch January 10, 2026 00:45
@benmed00 benmed00 reopened this Jan 10, 2026
@benmed00 benmed00 marked this pull request as ready for review January 10, 2026 00:46
@benmed00 benmed00 merged commit f1ab11c into main Jan 10, 2026
24 checks passed
@benmed00 benmed00 deleted the cursor/global-entity-search-implementation-8e2a branch January 10, 2026 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:core priority:high High priority type:feature New feature type:performance Performance improvement type:security Security type:testing Testing

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Add comprehensive testing infrastructure (unit, integration, E2E) Implement global entity search across all platform entities

2 participants

Comments