Merge pull request #858 from useautumn/fix/trials-used-cache-bypass

fix: 🐛 trials_used not properly bypassing cache
This commit is contained in:
John Yeo
2026-03-03 10:15:26 +00:00
committed by GitHub
4 changed files with 86 additions and 17 deletions

View File

@@ -0,0 +1,56 @@
import {
customerProducts,
customers,
type FullCustomer,
products,
} from "@autumn/shared";
import { and, eq, isNotNull, or } from "drizzle-orm";
import type { RepoContext } from "@/db/repoContext.js";
/** Fetch all customer_products rows with a free trial for a given customer (or matching fingerprint). */
export const fetchCustomerProductFreeTrials = async ({
ctx,
fullCus,
}: {
ctx: RepoContext;
fullCus: FullCustomer;
}) => {
const { db, org, env } = ctx;
const rows = await db
.select({
plan_id: products.id,
customer_id: customers.id,
fingerprint: customers.fingerprint,
})
.from(customerProducts)
.innerJoin(
products,
eq(customerProducts.internal_product_id, products.internal_id),
)
.innerJoin(
customers,
eq(customerProducts.internal_customer_id, customers.internal_id),
)
.where(
and(
or(
eq(customers.internal_id, fullCus.internal_id),
fullCus.fingerprint
? eq(customers.fingerprint, fullCus.fingerprint)
: undefined,
),
eq(products.org_id, org.id),
eq(products.env, env),
isNotNull(customerProducts.trial_ends_at),
),
);
return rows
.filter((r) => r.customer_id !== null)
.map((r) => ({
plan_id: r.plan_id,
customer_id: r.customer_id as string,
fingerprint: r.fingerprint,
}));
};

View File

@@ -1,5 +1,7 @@
import { batchUpdateCustomerProducts } from "./batchUpdateCustomerProducts";
import { fetchCustomerProductFreeTrials } from "./fetchCustomerProductFreeTrials";
export const customerProductRepo = {
batchUpdate: batchUpdateCustomerProducts,
fetchFreeTrials: fetchCustomerProductFreeTrials,
};

View File

@@ -11,6 +11,7 @@ import { CusService } from "../../CusService.js";
import { getCusPaymentMethodRes } from "../cusResponseUtils/getCusPaymentMethodRes.js";
import { getCusReferrals } from "../cusResponseUtils/getCusReferrals.js";
import { getCusRewards } from "../cusResponseUtils/getCusRewards.js";
import { getCusTrialsUsed } from "../cusResponseUtils/getCusTrialsUsed.js";
export const getApiCustomerExpand = async ({
ctx,
@@ -21,7 +22,7 @@ export const getApiCustomerExpand = async ({
customerId?: string;
fullCus?: FullCustomer;
}): Promise<ApiCusExpand> => {
const { org, env, db, logger, expand } = ctx;
const { org, env, db, expand } = ctx;
// Filter out balances.feature and subscriptions.plan
const filteredExpand = filterExpand({
@@ -45,19 +46,6 @@ export const getApiCustomerExpand = async ({
});
}
const getCusTrialsUsed = () => {
if (expand.includes(CustomerExpand.TrialsUsed)) {
return (
fullCus.trials_used?.map((t) => ({
plan_id: t.product_id,
customer_id: t.customer_id,
fingerprint: t.fingerprint,
})) ?? []
);
}
return undefined;
};
const getApiCusEntities = () => {
if (expand.includes(CustomerExpand.Entities)) {
return fullCus.entities.map((e) => ApiBaseEntitySchema.parse(e));
@@ -67,7 +55,7 @@ export const getApiCustomerExpand = async ({
const cusExpand = expand as CustomerExpand[];
const [rewards, referrals, paymentMethod] = await Promise.all([
const [rewards, referrals, paymentMethod, trialsUsed] = await Promise.all([
getCusRewards({
org,
env,
@@ -77,7 +65,6 @@ export const getApiCustomerExpand = async ({
),
expand: cusExpand,
}),
getCusReferrals({
db,
fullCus,
@@ -89,10 +76,15 @@ export const getApiCustomerExpand = async ({
fullCus,
expand: cusExpand,
}),
getCusTrialsUsed({
ctx,
fullCus,
expand: cusExpand,
}),
]);
return {
trials_used: getCusTrialsUsed() ?? undefined,
trials_used: trialsUsed ?? undefined,
entities: getApiCusEntities() ?? undefined,
rewards: rewards ?? undefined,
// upcoming_invoice: upcomingInvoice,

View File

@@ -0,0 +1,19 @@
import { CustomerExpand, type FullCustomer } from "@autumn/shared";
import type { RepoContext } from "@/db/repoContext.js";
import { customerProductRepo } from "../../cusProducts/repos/index.js";
export const getCusTrialsUsed = async ({
ctx,
fullCus,
expand,
}: {
ctx: RepoContext;
fullCus: FullCustomer;
expand?: CustomerExpand[];
}) => {
if (!expand?.includes(CustomerExpand.TrialsUsed)) {
return undefined;
}
return customerProductRepo.fetchFreeTrials({ ctx, fullCus });
};