Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server/src/internal/products/ProductService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export class ProductService {
version,
excludeEnts = false,
archived,
group,
}: {
db: DrizzleCli;
orgId: string;
Expand All @@ -226,6 +227,7 @@ export class ProductService {
version?: number;
excludeEnts?: boolean;
archived?: boolean;
group?: string;
}): Promise<FullProduct[]> {
// Use caching for simple queries (no inIds, returnAll, version, or excludeEnts)
const canCache = !inIds && !returnAll && !version && !excludeEnts;
Expand All @@ -235,10 +237,11 @@ export class ProductService {
key: buildProductsCacheKey({
orgId,
env,
queryParams: { archived },
queryParams: { archived, group },
Comment on lines 237 to +240
Copy link
Contributor

Choose a reason for hiding this comment

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

Cache invalidation in invalidateProductsCache() doesn't account for group-filtered cache entries. When products are modified, cached queries with different group parameter values won't be invalidated, leading to stale data.

The invalidateProductsCache() function in productCacheUtils.ts only invalidates cache keys for archived parameter variants, but now that group is part of the cache key hash, queries like ?group=premium will remain cached even after product updates.

Prompt To Fix With AI
This is a comment left during a code review.
Path: server/src/internal/products/ProductService.ts
Line: 237:240

Comment:
Cache invalidation in `invalidateProductsCache()` doesn't account for group-filtered cache entries. When products are modified, cached queries with different `group` parameter values won't be invalidated, leading to stale data.

The `invalidateProductsCache()` function in `productCacheUtils.ts` only invalidates cache keys for `archived` parameter variants, but now that `group` is part of the cache key hash, queries like `?group=premium` will remain cached even after product updates.

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

}),
ttl: PRODUCTS_CACHE_TTL,
fn: () => ProductService._listFullQuery({ db, orgId, env, archived }),
fn: () =>
ProductService._listFullQuery({ db, orgId, env, archived, group }),
});
}

Expand All @@ -251,6 +254,7 @@ export class ProductService {
version,
excludeEnts,
archived,
group,
});
}

Expand All @@ -263,6 +267,7 @@ export class ProductService {
version,
excludeEnts = false,
archived,
group,
}: {
db: DrizzleCli;
orgId: string;
Expand All @@ -272,6 +277,7 @@ export class ProductService {
version?: number;
excludeEnts?: boolean;
archived?: boolean;
group?: string;
}): Promise<FullProduct[]> {
// Optimization: Use a subquery to only fetch the latest version of each product
const latestVersionsSubquery =
Expand Down Expand Up @@ -301,6 +307,7 @@ export class ProductService {
eq(products.env, env),
inIds ? inArray(products.id, inIds) : undefined,
version ? eq(products.version, version) : undefined,
group ? eq(products.group, group) : undefined,
latestVersionsSubquery
? exists(
db
Expand Down
4 changes: 3 additions & 1 deletion server/src/internal/products/handlers/handleListPlans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const handleListPlans = createRoute({
const { org, features, env, db } = ctx;
const query = c.req.valid("query");

const { customer_id, entity_id, include_archived, v1_schema } = query;
const { customer_id, group, entity_id, include_archived, v1_schema } =
query;

const startedAt = Date.now();

Expand All @@ -26,6 +27,7 @@ export const handleListPlans = createRoute({
orgId: org.id,
env,
archived: include_archived ? undefined : false,
group,
}),
customer_id
? CusService.getFull({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ export const handleGetProducts = createRoute({
handler: async (c) => {
const { db, org, env, features } = c.get("ctx");

const products = await ProductService.listFull({ db, orgId: org.id, env });
const group = c.req.query("group");

const products = await ProductService.listFull({
db,
orgId: org.id,
env,
group,
});

const groupToDefaults = getGroupToDefaults({ defaultProds: products });

Expand Down
1 change: 1 addition & 0 deletions shared/api/products/crud/listPlanParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from "zod/v4";

export const ListPlansQuerySchema = z.object({
customer_id: z.string().optional(),
group: z.string().optional(),
entity_id: z.string().optional().meta({
internal: true,
}),
Expand Down