Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M15-127): Per-module fixes: packages/core/src/modules/subscription - R13 and the findings no rule covers

ralph4ae0e5a5fdc9
diff --git a/packages/core/src/modules/subscription/subscription.constants.ts b/packages/core/src/modules/subscription/subscription.constants.ts
index a0845e2a3..34e51cfa5 100644
--- a/packages/core/src/modules/subscription/subscription.constants.ts
+++ b/packages/core/src/modules/subscription/subscription.constants.ts
@@ -1,9 +1,7 @@
-// Moved from packages/validation/src/index.ts (M6-006, ADR-008's module map:
-// subscription owns "C"). packages/validation/src/index.ts becomes a
-// re-export shim of this file (same shape as ./notification.constants.ts
-// since M6-005), so packages/trpc's subscription router and apps/start's
-// billing forms keep resolving these symbols through packages/validation's
-// existing barrel unchanged.
+// Ported from V1's packages/validation/src/index.ts (M6-006, ADR-008's
+// module map: subscription owns "C") — that package doesn't exist in this
+// tree; apps/start's billing forms and this module's own
+// subscription.rpc.ts resolve these symbols through this file directly.
//
// Isomorphic by the AGENTS.md rule: zod and nothing else.
diff --git a/packages/core/src/modules/subscription/subscription.routes.ts b/packages/core/src/modules/subscription/subscription.routes.ts
index 57e421f93..3e17ca429 100644
--- a/packages/core/src/modules/subscription/subscription.routes.ts
+++ b/packages/core/src/modules/subscription/subscription.routes.ts
@@ -1,8 +1,7 @@
-// The Polar webhook (M6-006), `POST /webhook/polar`. V1's Fastify controller
-// (apps/api/src/controllers/webhook.controller.ts's `polarWebhook`) stays the
-// LIVE route (DELEGATE PATTERN) and delegates to
-// subscription.service.ts's `handlePolarWebhookEvent` — the same function
-// this route calls.
+// The Polar webhook (M6-006), `POST /webhook/polar`. Ported from V1's
+// apps/api/src/controllers/webhook.controller.ts's `polarWebhook`, which
+// doesn't exist in this tree — this route is the only caller of
+// subscription.service.ts's `handlePolarWebhookEvent`.
//
// RAW BYTES, deliberately: `validatePolarEvent` verifies Polar's signature
// over the exact request body, so this handler reads `await request.text()`
diff --git a/packages/core/src/modules/subscription/subscription.service.ts b/packages/core/src/modules/subscription/subscription.service.ts
index ded5228a8..7575594f0 100644
--- a/packages/core/src/modules/subscription/subscription.service.ts
+++ b/packages/core/src/modules/subscription/subscription.service.ts
@@ -1,9 +1,8 @@
-// Moved from packages/trpc/src/routers/subscription.ts (checkout, products,
-// usage, cancel/pause/resume, save-discount, portal) and from
-// apps/api/src/controllers/webhook.controller.ts's `polarWebhook` (M6-006).
-// V1's router and controller stay the LIVE routes (DELEGATE PATTERN) and
-// delegate every handler body to these functions, same as every other
-// dissolved service in this package.
+// Ported from V1's packages/trpc/src/routers/subscription.ts (checkout,
+// products, usage, cancel/pause/resume, save-discount, portal) and from
+// apps/api/src/controllers/webhook.controller.ts's `polarWebhook` (M6-006) —
+// neither exists in this tree; every caller reaches these functions through
+// this file directly (subscription.rpc.ts, subscription.routes.ts).
//
// `requireOrganizationAdmin` travels with the business logic here rather than
// living in subscription.rpc.ts the way project.rpc.ts's simple ladder checks
@@ -64,6 +63,15 @@ const POLAR_PRODUCTS_CACHE_KEY = 'polar:products';
const POLAR_PRODUCTS_CACHE_TTL_SECONDS = 60 * 60 * 24;
const DEFAULT_USAGE_WINDOW_DAYS = 30;
+// Every mutating export below gates on this before touching Polar.
+async function requireOrgAdmin(
+ userId: string,
+ organizationId: string
+): Promise<void> {
+ const { requireOrganizationAdmin } = await getAccessChecks();
+ await requireOrganizationAdmin({ userId, organizationId });
+}
+
export async function getCurrentSubscriptionProduct(
deps: ServiceDeps,
organizationId: string
@@ -83,11 +91,7 @@ export async function checkout(
input: ICheckout,
ipAddress: string | undefined
) {
- const { requireOrganizationAdmin } = await getAccessChecks();
- await requireOrganizationAdmin({
- userId,
- organizationId: input.organizationId,
- });
+ await requireOrgAdmin(userId, input.organizationId);
const [user, organization] = await Promise.all([
deps.db.user.findFirstOrThrow({ where: { id: userId } }),
@@ -195,11 +199,7 @@ export async function cancelSubscription(
userId: string,
input: ICancelSubscription
) {
- const { requireOrganizationAdmin } = await getAccessChecks();
- await requireOrganizationAdmin({
- userId,
- organizationId: input.organizationId,
- });
+ await requireOrgAdmin(userId, input.organizationId);
const organization = await getOrganizationById(deps, input.organizationId);
if (!organization.subscriptionId) {
@@ -235,11 +235,7 @@ export async function pauseSubscription(
userId: string,
input: IPauseSubscription
) {
- const { requireOrganizationAdmin } = await getAccessChecks();
- await requireOrganizationAdmin({
- userId,
- organizationId: input.organizationId,
- });
+ await requireOrgAdmin(userId, input.organizationId);
const organization = await getOrganizationById(deps, input.organizationId);
if (!organization.subscriptionId) {
@@ -277,8 +273,7 @@ export async function resumeSubscription(
userId: string,
organizationId: string
) {
- const { requireOrganizationAdmin } = await getAccessChecks();
- await requireOrganizationAdmin({ userId, organizationId });
+ await requireOrgAdmin(userId, organizationId);
const organization = await getOrganizationById(deps, organizationId);
if (!organization.subscriptionId) {
@@ -311,8 +306,7 @@ export async function applySaveDiscount(
userId: string,
organizationId: string
) {
- const { requireOrganizationAdmin } = await getAccessChecks();
- await requireOrganizationAdmin({ userId, organizationId });
+ await requireOrgAdmin(userId, organizationId);
const discountId = deps.config.polar.saveDiscountId;
if (!discountId) {
@@ -354,8 +348,7 @@ export async function portal(
userId: string,
organizationId: string
) {
- const { requireOrganizationAdmin } = await getAccessChecks();
- await requireOrganizationAdmin({ userId, organizationId });
+ await requireOrgAdmin(userId, organizationId);
const organization = await getOrganizationById(deps, organizationId);
if (!organization.subscriptionCustomerId) {
@@ -370,14 +363,13 @@ export async function portal(
}
// -- Polar webhook --------------------------------------------------------
-// Ported from apps/api/src/controllers/webhook.controller.ts's `polarWebhook`
-// + its private helpers (M6-006). V1's Fastify controller stays the LIVE
-// route (DELEGATE PATTERN) and delegates here — the same function this
-// module's own `/webhook/polar` route (subscription.routes.ts) calls.
+// Ported from V1's apps/api/src/controllers/webhook.controller.ts's
+// `polarWebhook` + its private helpers (M6-006); that controller doesn't
+// exist in this tree — this module's own `/webhook/polar` route
+// (subscription.routes.ts) is the only caller.
// `validatePolarEvent` verifies the signature over the RAW body bytes; any
-// body parsing before this call breaks that verification, which is why both
-// callers read `await request.text()` / `request.rawBody` rather than a
-// parsed JSON body.
+// body parsing before this call breaks that verification, which is why the
+// route reads `await request.text()` rather than a parsed JSON body.
type PolarEvent = ReturnType<typeof validatePolarEvent>;
type PolarSubscriptionData = Extract<