Skip to content

Commit ac1cc94

Browse files
authored
Merge pull request #858 from useautumn/fix/trials-used-cache-bypass
fix: 🐛 trials_used not properly bypassing cache
2 parents b944872 + bba2b03 commit ac1cc94

File tree

4 files changed

+86
-17
lines changed

4 files changed

+86
-17
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
customerProducts,
3+
customers,
4+
type FullCustomer,
5+
products,
6+
} from "@autumn/shared";
7+
import { and, eq, isNotNull, or } from "drizzle-orm";
8+
import type { RepoContext } from "@/db/repoContext.js";
9+
10+
/** Fetch all customer_products rows with a free trial for a given customer (or matching fingerprint). */
11+
export const fetchCustomerProductFreeTrials = async ({
12+
ctx,
13+
fullCus,
14+
}: {
15+
ctx: RepoContext;
16+
fullCus: FullCustomer;
17+
}) => {
18+
const { db, org, env } = ctx;
19+
20+
const rows = await db
21+
.select({
22+
plan_id: products.id,
23+
customer_id: customers.id,
24+
fingerprint: customers.fingerprint,
25+
})
26+
.from(customerProducts)
27+
.innerJoin(
28+
products,
29+
eq(customerProducts.internal_product_id, products.internal_id),
30+
)
31+
.innerJoin(
32+
customers,
33+
eq(customerProducts.internal_customer_id, customers.internal_id),
34+
)
35+
.where(
36+
and(
37+
or(
38+
eq(customers.internal_id, fullCus.internal_id),
39+
fullCus.fingerprint
40+
? eq(customers.fingerprint, fullCus.fingerprint)
41+
: undefined,
42+
),
43+
eq(products.org_id, org.id),
44+
eq(products.env, env),
45+
isNotNull(customerProducts.trial_ends_at),
46+
),
47+
);
48+
49+
return rows
50+
.filter((r) => r.customer_id !== null)
51+
.map((r) => ({
52+
plan_id: r.plan_id,
53+
customer_id: r.customer_id as string,
54+
fingerprint: r.fingerprint,
55+
}));
56+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { batchUpdateCustomerProducts } from "./batchUpdateCustomerProducts";
2+
import { fetchCustomerProductFreeTrials } from "./fetchCustomerProductFreeTrials";
23

34
export const customerProductRepo = {
45
batchUpdate: batchUpdateCustomerProducts,
6+
fetchFreeTrials: fetchCustomerProductFreeTrials,
57
};

server/src/internal/customers/cusUtils/apiCusUtils/getApiCustomerExpand.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CusService } from "../../CusService.js";
1111
import { getCusPaymentMethodRes } from "../cusResponseUtils/getCusPaymentMethodRes.js";
1212
import { getCusReferrals } from "../cusResponseUtils/getCusReferrals.js";
1313
import { getCusRewards } from "../cusResponseUtils/getCusRewards.js";
14+
import { getCusTrialsUsed } from "../cusResponseUtils/getCusTrialsUsed.js";
1415

1516
export const getApiCustomerExpand = async ({
1617
ctx,
@@ -21,7 +22,7 @@ export const getApiCustomerExpand = async ({
2122
customerId?: string;
2223
fullCus?: FullCustomer;
2324
}): Promise<ApiCusExpand> => {
24-
const { org, env, db, logger, expand } = ctx;
25+
const { org, env, db, expand } = ctx;
2526

2627
// Filter out balances.feature and subscriptions.plan
2728
const filteredExpand = filterExpand({
@@ -45,19 +46,6 @@ export const getApiCustomerExpand = async ({
4546
});
4647
}
4748

48-
const getCusTrialsUsed = () => {
49-
if (expand.includes(CustomerExpand.TrialsUsed)) {
50-
return (
51-
fullCus.trials_used?.map((t) => ({
52-
plan_id: t.product_id,
53-
customer_id: t.customer_id,
54-
fingerprint: t.fingerprint,
55-
})) ?? []
56-
);
57-
}
58-
return undefined;
59-
};
60-
6149
const getApiCusEntities = () => {
6250
if (expand.includes(CustomerExpand.Entities)) {
6351
return fullCus.entities.map((e) => ApiBaseEntitySchema.parse(e));
@@ -67,7 +55,7 @@ export const getApiCustomerExpand = async ({
6755

6856
const cusExpand = expand as CustomerExpand[];
6957

70-
const [rewards, referrals, paymentMethod] = await Promise.all([
58+
const [rewards, referrals, paymentMethod, trialsUsed] = await Promise.all([
7159
getCusRewards({
7260
org,
7361
env,
@@ -77,7 +65,6 @@ export const getApiCustomerExpand = async ({
7765
),
7866
expand: cusExpand,
7967
}),
80-
8168
getCusReferrals({
8269
db,
8370
fullCus,
@@ -89,10 +76,15 @@ export const getApiCustomerExpand = async ({
8976
fullCus,
9077
expand: cusExpand,
9178
}),
79+
getCusTrialsUsed({
80+
ctx,
81+
fullCus,
82+
expand: cusExpand,
83+
}),
9284
]);
9385

9486
return {
95-
trials_used: getCusTrialsUsed() ?? undefined,
87+
trials_used: trialsUsed ?? undefined,
9688
entities: getApiCusEntities() ?? undefined,
9789
rewards: rewards ?? undefined,
9890
// upcoming_invoice: upcomingInvoice,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { CustomerExpand, type FullCustomer } from "@autumn/shared";
2+
import type { RepoContext } from "@/db/repoContext.js";
3+
import { customerProductRepo } from "../../cusProducts/repos/index.js";
4+
5+
export const getCusTrialsUsed = async ({
6+
ctx,
7+
fullCus,
8+
expand,
9+
}: {
10+
ctx: RepoContext;
11+
fullCus: FullCustomer;
12+
expand?: CustomerExpand[];
13+
}) => {
14+
if (!expand?.includes(CustomerExpand.TrialsUsed)) {
15+
return undefined;
16+
}
17+
18+
return customerProductRepo.fetchFreeTrials({ ctx, fullCus });
19+
};

0 commit comments

Comments
 (0)