Skip to content

PayoutsService getRevenueBreakdown issues N+1 queries without pagination #809

Description

@RUKAYAT-CODER

Overview

src/payments/payouts/payouts.service.ts loads all courses for an instructor, then all payments for all course IDs, then all refunds for all payment IDs — three sequential queries that load entire datasets into memory. An instructor with thousands of sales will cause OOM and slow API responses.

Specifications

Features:

  • Use a single aggregation query (or at most two) to compute revenue breakdown.
  • Support pagination so large result sets are streamed in pages.

Tasks:

  • Replace separate find() calls with a TypeORM QueryBuilder that joins Course → Payment → Refund and uses SUM/GROUP BY.
  • Accept page and limit parameters and apply OFFSET/LIMIT to the course list.
  • Add a database index on payments.courseId and refunds.paymentId if not already present.
  • Benchmark before and after with 10k payments.

Impacted Files:

  • src/payments/payouts/payouts.service.ts

Acceptance Criteria

  • Revenue breakdown for 10k payments returns in under 200ms.
  • Memory usage during the call does not scale with dataset size.
  • Results are identical to the current output (verified by unit test).

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programbugSomething isn't workingperformance

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions