The minimalistic document archiving platform.
refactor(plans): plan ID constants into object (#1160)
Corentin Thomassetbc54dd1ff590
diff --git a/apps/papra-client/src/modules/plans/plans.constants.ts b/apps/papra-client/src/modules/plans/plans.constants.tsdeleted file mode 100644index ff46b52a..00000000--- a/apps/papra-client/src/modules/plans/plans.constants.ts+++ /dev/null@@ -1,3 +0,0 @@-export const FREE_PLAN_ID = 'free';-export const PLUS_PLAN_ID = 'plus';-export const PRO_PLAN_ID = 'pro';diff --git a/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsx b/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsxindex b84b6a64..c286de45 100644--- a/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsx+++ b/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsx@@ -3,7 +3,6 @@ import type { Component, JSX } from 'solid-js';import { safely } from '@corentinth/chisels';import { createSignal, For } from 'solid-js';import { useI18n } from '@/modules/i18n/i18n.provider';-import { PLUS_PLAN_ID, PRO_PLAN_ID } from '@/modules/plans/plans.constants';import { cn } from '@/modules/shared/style/cn';import { Button } from '@/modules/ui/components/button';import {@@ -15,6 +14,7 @@ import {DialogTrigger,} from '@/modules/ui/components/dialog';import { getCheckoutUrl } from '../subscriptions.services';+import { PLAN_IDS } from '@papra/app-server/plans/constants';// Hardcoded global reduction configuration, will be replaced by a dynamic configuration laterconst globalReduction = {@@ -331,12 +331,12 @@ export const UpgradeDialog: Component<UpgradeDialogProps> = (props) => {<PlanCard {...currentPlan} billingInterval={getBillingInterval()} /><PlanCard{...plusPlan}- onUpgrade={() => onUpgrade(PLUS_PLAN_ID)}+ onUpgrade={() => onUpgrade(PLAN_IDS.PLUS)}billingInterval={getBillingInterval()}/><PlanCard{...proPlan}- onUpgrade={() => onUpgrade(PRO_PLAN_ID)}+ onUpgrade={() => onUpgrade(PLAN_IDS.PRO)}billingInterval={getBillingInterval()}/></div>diff --git a/apps/papra-client/src/modules/ui/layouts/organization.layout.tsx b/apps/papra-client/src/modules/ui/layouts/organization.layout.tsxindex fbad20b9..4bc423c4 100644--- a/apps/papra-client/src/modules/ui/layouts/organization.layout.tsx+++ b/apps/papra-client/src/modules/ui/layouts/organization.layout.tsx@@ -27,6 +27,7 @@ import {SelectValue,} from '../components/select';import { SidenavLayout } from './sidenav.layout';+import { PLAN_IDS } from '@papra/app-server/plans/constants';const UpgradeCTAFooter: Component<{ organizationId: string }> = (props) => {const { t } = useI18n();@@ -42,7 +43,7 @@ const UpgradeCTAFooter: Component<{ organizationId: string }> = (props) => {return false;}- return query.data && query.data.plan.id === 'free';+ return query.data && query.data.plan.id === PLAN_IDS.FREE;};return (diff --git a/apps/papra-server/package.json b/apps/papra-server/package.jsonindex 574bd8a3..c4bcfba6 100644--- a/apps/papra-server/package.json+++ b/apps/papra-server/package.json@@ -10,7 +10,8 @@"exports": {"./organizations/constants": "./src/modules/organizations/organizations.constants.ts","./customProperties/constants": "./src/modules/custom-properties/custom-properties.constants.ts",- "./apiKeys/constants": "./src/modules/api-keys/api-keys.constants.ts"+ "./apiKeys/constants": "./src/modules/api-keys/api-keys.constants.ts",+ "./plans/constants": "./src/modules/plans/plans.constants.ts"},"scripts": {"dev": "tsx watch --env-file-if-exists=.env src/index.ts | crowlog-pretty",diff --git a/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.ts b/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.tsindex 68d93648..dd8e164b 100644--- a/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.ts+++ b/apps/papra-server/src/modules/documents/e2e/upload-documents.e2e.test.ts@@ -5,7 +5,7 @@ import { createServer } from '../../app/server';import { createTestServerDependencies } from '../../app/server.test-utils';import { overrideConfig } from '../../config/config.test-utils';import { ORGANIZATION_ROLES } from '../../organizations/organizations.constants';-import { PLUS_PLAN_ID, PRO_PLAN_ID } from '../../plans/plans.constants';+import { PLAN_IDS } from '../../plans/plans.constants';import { documentsTable } from '../documents.table';import { createInMemoryDocumentStorageServices } from '../storage/documents.storage.services.test-utils';@@ -302,7 +302,7 @@ describe('documents e2e', () => {id: 'sub_plus123',customerId: 'cus_plus123',organizationId: 'org_222222222222222222222222',- planId: PLUS_PLAN_ID,+ planId: PLAN_IDS.PLUS,status: 'active',seatsCount: 5,currentPeriodStart: new Date('2024-01-01'),@@ -372,7 +372,7 @@ describe('documents e2e', () => {id: 'sub_pro123',customerId: 'cus_pro123',organizationId: 'org_333333333333333333333333',- planId: PRO_PLAN_ID,+ planId: PLAN_IDS.PRO,status: 'active',seatsCount: 20,currentPeriodStart: new Date('2024-01-01'),diff --git a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.tsindex 0cf9a067..7f78b43b 100644--- a/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts+++ b/apps/papra-server/src/modules/intake-emails/intake-emails.usecases.test.ts@@ -8,7 +8,7 @@ import { overrideConfig } from '../config/config.test-utils';import { documentsTable } from '../documents/documents.table';import { createDocumentCreationUsecase } from '../documents/documents.usecases';import { createInMemoryDocumentStorageServices } from '../documents/storage/documents.storage.services.test-utils';-import { PLUS_PLAN_ID } from '../plans/plans.constants';+import { PLAN_IDS } from '../plans/plans.constants';import { pick } from '../shared/objects';import { createSubscriptionsRepository } from '../subscriptions/subscriptions.repository';import { createInMemoryTaskServices } from '../tasks/tasks.test-utils';@@ -301,7 +301,7 @@ describe('intake-emails usecases', () => {currentPeriodStart: new Date('2025-03-18T00:00:00.000Z'),currentPeriodEnd: new Date('2025-04-18T00:00:00.000Z'),customerId: 'sc_123',- planId: PLUS_PLAN_ID,+ planId: PLAN_IDS.PLUS,},],});@@ -310,7 +310,7 @@ describe('intake-emails usecases', () => {const plansRepository = {getOrganizationPlanById: async () => ({organizationPlan: {- id: PLUS_PLAN_ID,+ id: PLAN_IDS.PLUS,name: 'Plus',limits: {maxIntakeEmailsCount: 2,diff --git a/apps/papra-server/src/modules/plans/plans.constants.ts b/apps/papra-server/src/modules/plans/plans.constants.tsindex ff46b52a..13b8982d 100644--- a/apps/papra-server/src/modules/plans/plans.constants.ts+++ b/apps/papra-server/src/modules/plans/plans.constants.ts@@ -1,3 +1,5 @@-export const FREE_PLAN_ID = 'free';-export const PLUS_PLAN_ID = 'plus';-export const PRO_PLAN_ID = 'pro';+export const PLAN_IDS = {+ FREE: 'free',+ PLUS: 'plus',+ PRO: 'pro',+} as const;diff --git a/apps/papra-server/src/modules/plans/plans.repository.test.ts b/apps/papra-server/src/modules/plans/plans.repository.test.tsindex c6be8316..64075326 100644--- a/apps/papra-server/src/modules/plans/plans.repository.test.ts+++ b/apps/papra-server/src/modules/plans/plans.repository.test.ts@@ -1,6 +1,6 @@import { describe, expect, test } from 'vitest';import { overrideConfig } from '../config/config.test-utils';-import { FREE_PLAN_ID } from './plans.constants';+import { PLAN_IDS } from './plans.constants';import { getOrganizationPlansRecords } from './plans.repository';describe('plans repository', () => {@@ -32,7 +32,7 @@ describe('plans repository', () => {const { organizationPlans } = getOrganizationPlansRecords({ config });- expect(organizationPlans[FREE_PLAN_ID]!.limits).to.deep.equal({+ expect(organizationPlans[PLAN_IDS.FREE]!.limits).to.deep.equal({maxDocumentStorageBytes: Number.POSITIVE_INFINITY,maxIntakeEmailsCount: Number.POSITIVE_INFINITY,maxOrganizationsMembersCount: Number.POSITIVE_INFINITY,diff --git a/apps/papra-server/src/modules/plans/plans.repository.ts b/apps/papra-server/src/modules/plans/plans.repository.tsindex e62f80c4..ebc23aeb 100644--- a/apps/papra-server/src/modules/plans/plans.repository.ts+++ b/apps/papra-server/src/modules/plans/plans.repository.ts@@ -3,7 +3,7 @@ import type { OrganizationPlanRecord } from './plans.types';import { injectArguments } from '@corentinth/chisels';import { isDocumentSizeLimitEnabled } from '../documents/documents.models';import { IN_BYTES } from '../shared/units';-import { FREE_PLAN_ID, PLUS_PLAN_ID, PRO_PLAN_ID } from './plans.constants';+import { PLAN_IDS } from './plans.constants';import { createPlanNotFoundError } from './plans.errors';export type PlansRepository = ReturnType<typeof createPlansRepository>;@@ -27,8 +27,8 @@ export function getOrganizationPlansRecords({ config }: { config: Config }) {const { maxUploadSize } = config.documentsStorage;const organizationPlans: Record<string, OrganizationPlanRecord> = {- [FREE_PLAN_ID]: {- id: FREE_PLAN_ID,+ [PLAN_IDS.FREE]: {+ id: PLAN_IDS.FREE,name: 'Free',limits: {maxDocumentStorageBytes: isFreePlanUnlimited@@ -41,8 +41,8 @@ export function getOrganizationPlansRecords({ config }: { config: Config }) {: Number.POSITIVE_INFINITY,},},- [PLUS_PLAN_ID]: {- id: PLUS_PLAN_ID,+ [PLAN_IDS.PLUS]: {+ id: PLAN_IDS.PLUS,name: 'Plus',monthlyPriceId: config.organizationPlans.plusPlanMonthlyPriceId,annualPriceId: config.organizationPlans.plusPlanAnnualPriceId,@@ -53,8 +53,8 @@ export function getOrganizationPlansRecords({ config }: { config: Config }) {maxFileSize: 100 * IN_BYTES.MEGABYTE, // 100 MiB},},- [PRO_PLAN_ID]: {- id: PRO_PLAN_ID,+ [PLAN_IDS.PRO]: {+ id: PLAN_IDS.PRO,name: 'Pro',monthlyPriceId: config.organizationPlans.proPlanMonthlyPriceId,annualPriceId: config.organizationPlans.proPlanAnnualPriceId,diff --git a/apps/papra-server/src/modules/plans/plans.usecases.test.ts b/apps/papra-server/src/modules/plans/plans.usecases.test.tsindex b05405b2..be387b60 100644--- a/apps/papra-server/src/modules/plans/plans.usecases.test.ts+++ b/apps/papra-server/src/modules/plans/plans.usecases.test.ts@@ -3,7 +3,7 @@ import { createInMemoryDatabase } from '../app/database/database.test-utils';import { overrideConfig } from '../config/config.test-utils';import { ORGANIZATION_ROLES } from '../organizations/organizations.constants';import { createSubscriptionsRepository } from '../subscriptions/subscriptions.repository';-import { FREE_PLAN_ID, PLUS_PLAN_ID } from './plans.constants';+import { PLAN_IDS } from './plans.constants';import { createPlansRepository } from './plans.repository';import { getOrganizationPlan } from './plans.usecases';@@ -20,7 +20,7 @@ describe('plans usecases', () => {{id: 'org_sub_1',organizationId: 'organization-1',- planId: PLUS_PLAN_ID,+ planId: PLAN_IDS.PLUS,customerId: 'cus_123',seatsCount: 10,status: 'active',@@ -47,7 +47,7 @@ describe('plans usecases', () => {plansRepository,});- expect(organizationPlan.id).to.equal(PLUS_PLAN_ID);+ expect(organizationPlan.id).to.equal(PLAN_IDS.PLUS);});test('an organization may not have any subscription, in this case the organization is considered to be on the free plan', async () => {@@ -75,7 +75,7 @@ describe('plans usecases', () => {plansRepository,});- expect(organizationPlan.id).to.equal(FREE_PLAN_ID);+ expect(organizationPlan.id).to.equal(PLAN_IDS.FREE);});});});diff --git a/apps/papra-server/src/modules/plans/plans.usecases.ts b/apps/papra-server/src/modules/plans/plans.usecases.tsindex 981d1f04..eb023eda 100644--- a/apps/papra-server/src/modules/plans/plans.usecases.ts+++ b/apps/papra-server/src/modules/plans/plans.usecases.ts@@ -1,6 +1,6 @@import type { SubscriptionsRepository } from '../subscriptions/subscriptions.repository';import type { PlansRepository } from './plans.repository';-import { FREE_PLAN_ID } from './plans.constants';+import { PLAN_IDS } from './plans.constants';export async function getOrganizationPlan({organizationId,@@ -15,7 +15,7 @@ export async function getOrganizationPlan({organizationId,});- const planId = subscription?.planId ?? FREE_PLAN_ID;+ const planId = subscription?.planId ?? PLAN_IDS.FREE;const { organizationPlan } = await plansRepository.getOrganizationPlanById({ planId });diff --git a/apps/papra-server/src/modules/subscriptions/subscriptions.routes.ts b/apps/papra-server/src/modules/subscriptions/subscriptions.routes.tsindex 9344b688..98de98ec 100644--- a/apps/papra-server/src/modules/subscriptions/subscriptions.routes.ts+++ b/apps/papra-server/src/modules/subscriptions/subscriptions.routes.ts@@ -12,7 +12,7 @@ import {ensureUserIsOwnerOfOrganization,getOrCreateOrganizationCustomerId,} from '../organizations/organizations.usecases';-import { FREE_PLAN_ID, PLUS_PLAN_ID, PRO_PLAN_ID } from '../plans/plans.constants';+import { PLAN_IDS } from '../plans/plans.constants';import { getPriceIdForBillingInterval } from '../plans/plans.models';import { createPlansRepository } from '../plans/plans.repository';import { getOrganizationPlan } from '../plans/plans.usecases';@@ -93,7 +93,7 @@ function setupCreateCheckoutSessionRoute({),validateJsonBody(v.strictObject({- planId: v.picklist([PLUS_PLAN_ID, PRO_PLAN_ID]),+ planId: v.picklist([PLAN_IDS.PLUS, PLAN_IDS.PRO]),billingInterval: v.optional(v.picklist(['monthly', 'annual']), 'monthly'),}),),@@ -127,7 +127,7 @@ function setupCreateCheckoutSessionRoute({plansRepository,});- if (organizationCurrentPlan.id !== FREE_PLAN_ID) {+ if (organizationCurrentPlan.id !== PLAN_IDS.FREE) {throw createOrganizationAlreadyHasSubscriptionError();}@@ -246,7 +246,7 @@ function setupGetOrganizationSubscriptionRoute({ app, db, config }: RouteDefinit});const { organizationPlan } = await plansRepository.getOrganizationPlanById({- planId: subscription?.planId ?? FREE_PLAN_ID,+ planId: subscription?.planId ?? PLAN_IDS.FREE,});return context.json({