Skip to content

fix: 🐛 trials_used not properly bypassing cache#858

Merged
johnyeocx merged 3 commits intomainfrom
fix/trials-used-cache-bypass
Mar 3, 2026
Merged

fix: 🐛 trials_used not properly bypassing cache#858
johnyeocx merged 3 commits intomainfrom
fix/trials-used-cache-bypass

Conversation

@SirTenzin
Copy link
Member

@SirTenzin SirTenzin commented Mar 2, 2026


Summary by cubic

Fixes trials_used to always read from the database and bypass cached customer data. Also tightens the trial matching to avoid false positives.

  • Bug Fixes
    • Fetch trials via customerProductRepo.fetchFreeTrials with joins on customerProducts, products, and customers; filters by org, env, and non-null trial_ends_at; returns { plan_id, customer_id, fingerprint } from live data.
    • Match trials by customers.internal_id, and only use fingerprint when present to prevent unintended matches.
    • getCusTrialsUsed delegates to the repo and is called in Promise.all within getApiCustomerExpand; removed the stale in-memory mapping.

Written for commit bba2b03. Summary will update on new commits.

Greptile Summary

This PR successfully addresses the cache bypass issue for trials_used by extracting the logic into a separate async function that queries the database directly, rather than relying on potentially stale cached data in the fullCus object.

Key changes:

  • Bug fixes: Extracted getCusTrialsUsed into separate file that performs direct DB query instead of reading from cached fullCus.trials_used
  • Improvements: Now called in Promise.all alongside other async expand operations for better performance

The refactor aligns with the pattern used by other expand functions (getCusRewards, getCusReferrals) and ensures trials_used data is always fresh from the database.

Confidence Score: 3/5

  • This PR has a logical issue that could cause incorrect query results in edge cases
  • The refactor successfully addresses the cache bypass issue, but there's a logic bug in the null ID handling (line 47) that could match unintended customers when fullCus.id is null. The fix is straightforward but critical for correctness.
  • Pay close attention to server/src/internal/customers/cusUtils/cusResponseUtils/getCusTrialsUsed.ts for the null ID handling bug

Important Files Changed

Filename Overview
server/src/internal/customers/cusUtils/apiCusUtils/getApiCustomerExpand.ts Refactored to use extracted async function for trials_used, removing inline synchronous function
server/src/internal/customers/cusUtils/cusResponseUtils/getCusTrialsUsed.ts New async function that queries DB directly but has potential issue with null ID handling

Last reviewed commit: 0e3e241

@vercel
Copy link

vercel bot commented Mar 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
autumn-vite Ready Ready Preview, Comment Mar 3, 2026 10:15am

Request Review

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

.where(
and(
or(
eq(customers.id, fullCus.id ?? ""),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if fullCus.id is null/undefined (allowed by schema), this becomes eq(customers.id, "") which could match customers with empty string IDs

Suggested change
eq(customers.id, fullCus.id ?? ""),
fullCus.id ? eq(customers.id, fullCus.id) : undefined,
Prompt To Fix With AI
This is a comment left during a code review.
Path: server/src/internal/customers/cusUtils/cusResponseUtils/getCusTrialsUsed.ts
Line: 47

Comment:
if `fullCus.id` is null/undefined (allowed by schema), this becomes `eq(customers.id, "")` which could match customers with empty string IDs

```suggestion
					fullCus.id ? eq(customers.id, fullCus.id) : undefined,
```

How can I resolve this? If you propose a fix, please make it concise.

@johnyeocx johnyeocx merged commit ac1cc94 into main Mar 3, 2026
8 of 10 checks passed
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