Skip to content

[Refactor] DRY Violations - Reduce Code Duplication in API Routes and Repositories #35

@webmaxru

Description

@webmaxru

Summary

A comprehensive code analysis identified significant DRY (Don't Repeat Yourself) violations across the codebase. Addressing these will reduce maintenance burden, improve consistency, and cut total duplicated code by approximately 76% (~1,360 lines → ~330 lines).

Problem Statement

The current codebase contains extensive code duplication in:

Area Severity Files Affected Lines Duplicated
API Route Handlers 🔴 Critical 8 files ~800 lines
Swagger Documentation 🔴 Critical 8 files ~640 lines
Repository Classes 🟡 Moderate 7 files ~420 lines
TypeScript Interfaces (FE) 🟡 Moderate 3+ files ~60 lines
Error Handling 🟡 Moderate 8 files ~80 lines

Affected Files

API Routes (identical CRUD patterns)

  • api/src/routes/supplier.ts
  • api/src/routes/branch.ts
  • api/src/routes/headquarters.ts
  • api/src/routes/product.ts
  • api/src/routes/order.ts
  • api/src/routes/delivery.ts
  • api/src/routes/orderDetail.ts
  • api/src/routes/orderDetailDelivery.ts

Repositories (identical CRUD methods)

  • api/src/repositories/suppliersRepo.ts
  • api/src/repositories/branchesRepo.ts
  • api/src/repositories/productsRepo.ts
  • api/src/repositories/headquartersRepo.ts
  • api/src/repositories/ordersRepo.ts
  • api/src/repositories/deliveriesRepo.ts
  • api/src/repositories/orderDetailsRepo.ts

Frontend Types (duplicated interfaces)

  • frontend/src/components/entity/product/Products.tsx
  • frontend/src/components/admin/AdminProducts.tsx
  • frontend/src/components/entity/product/ProductForm.tsx

Proposed Solution

1. ⚡ Quick Win: Shared Frontend Types

Create frontend/src/types/models.ts with centralized type definitions.

Effort: < 1 hour | Impact: Eliminates ~60 lines of duplication

2. 📈 Create Generic CRUD Route Factory

Create api/src/utils/routeFactory.ts that generates standard CRUD routes.

Effort: 2-4 hours | Impact: Reduces ~800 lines → ~120 lines (85% reduction)

3. 📈 Create Base Repository Class

Create api/src/repositories/baseRepository.ts with shared CRUD operations.

Effort: 2-4 hours | Impact: Reduces ~420 lines → ~175 lines (58% reduction)

4. 🔧 Swagger Documentation Generator (Backlog)

Create utility to generate standard CRUD Swagger documentation.

Effort: 4-8 hours | Impact: Reduces ~640 lines of JSDoc duplication

Acceptance Criteria

  • Shared TypeScript types created in frontend/src/types/models.ts
  • BaseRepository class created and all repositories refactored to extend it
  • createCrudRouter factory created and all basic CRUD routes use it
  • All existing tests pass after refactoring
  • Custom route handlers (e.g., findByName, updateStatus) remain functional
  • No regression in API functionality

Testing Strategy

  1. Run existing tests before refactoring to establish baseline
  2. Create integration tests for generic implementations
  3. Verify all endpoints work via Swagger UI after changes

Additional Context

This refactoring aligns with the project's architecture guidelines and will make future entity additions significantly easier (new entities would require ~15 lines instead of ~150+ lines).


Generated from DRY violation analysis

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions