---
issue: 071
title: "Backend: getReferrals N+1 — PurchaseRequest + PointTransaction per referral"
severity: high
domain: Points
labels: [performance, backend, n-plus-1]
status: open
created: 2026-05-30
source: Full Codebase Audit 2026-05-30
---

# Backend: getReferrals N+1 — PurchaseRequest + PointTransaction per referral

**Severity:** high
**Domain:** Points
**Labels:** performance, backend, n-plus-1

## Description

`PointsService.ts:312` issues two sequential DB queries per referred user: one `PurchaseRequest.findOne` and one `PointTransaction.find`. With N referrals, this results in 2×N queries. Under load this will cause slow responses and high DB load.

## Options

1. Batch with `$in` queries and aggregate grouped by referred user.
2. Precompute referral stats in a maintained summary doc.
3. Add pagination plus batched lookups.

## Recommendation

Replace per-referral queries with batched `$in` queries and an aggregation grouped by user; add pagination.

## Affected Files

- `backend/src/services/points/PointsService.ts:312`

## References

- [Full Codebase Audit 2026-05-30](../09%20-%20Audits/Full%20Codebase%20Audit%20-%202026-05-30.md) — DEC-41
