Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

πŸ› Fix internal server error when re-activating an installed license key (#2883)

Luke Vellad10973ae0b5a
diff --git a/apps/web/src/features/licensing/actions.ts b/apps/web/src/features/licensing/actions.ts
index 24d909b2e..8e07a6266 100644
--- a/apps/web/src/features/licensing/actions.ts
+++ b/apps/web/src/features/licensing/actions.ts
@@ -7,7 +7,7 @@ import { INSTANCE_LICENSE_TAG } from "@/features/licensing/constants";
import { validateLicenseKeyInputSchema } from "@/features/licensing/schema";
import { AppError } from "@/lib/errors/app-error";
import { adminActionClient } from "@/lib/safe-action/server";
-import { licenseManager } from "./mutations";
+import { licenseManager, setInstanceLicense } from "./mutations";
const logger = createLogger("licensing/actions");
@@ -107,18 +107,7 @@ export const validateLicenseKeyAction = adminActionClient
});
}
- await prisma.instanceLicense.create({
- data: {
- licenseKey: data.key,
- licenseeName: data.licenseeName,
- licenseeEmail: data.licenseeEmail,
- issuedAt: data.issuedAt,
- expiresAt: data.expiresAt,
- seats: data.seats,
- type: data.type,
- whiteLabelAddon: data.whiteLabelAddon,
- },
- });
+ await setInstanceLicense(data);
updateTag(INSTANCE_LICENSE_TAG);
diff --git a/apps/web/src/features/licensing/mutations.test.ts b/apps/web/src/features/licensing/mutations.test.ts
new file mode 100644
index 000000000..e23f909b5
--- /dev/null
+++ b/apps/web/src/features/licensing/mutations.test.ts
@@ -0,0 +1,77 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const { mockTransaction, mockDeleteMany, mockCreate } = vi.hoisted(() => ({
+ mockTransaction: vi.fn(),
+ mockDeleteMany: vi.fn(),
+ mockCreate: vi.fn(),
+}));
+
+vi.mock("@rallly/database", () => ({
+ prisma: {
+ $transaction: mockTransaction,
+ instanceLicense: {
+ deleteMany: mockDeleteMany,
+ create: mockCreate,
+ },
+ },
+}));
+// setInstanceLicense doesn't touch either, but importing the module pulls in
+// LicenseManager (env) and createLicenseCheckoutSession (billing).
+vi.mock("@/env", () => ({ env: {} }));
+vi.mock("@rallly/billing", () => ({}));
+
+const license = {
+ key: "RLYV4-AAAA-BBBB-CCCC-DDDD-00199",
+ licenseeName: "Probe Licensee",
+ licenseeEmail: "[email protected]",
+ issuedAt: new Date("2026-01-01T00:00:00Z"),
+ expiresAt: null,
+ seats: 5,
+ type: "PLUS" as const,
+ whiteLabelAddon: false,
+};
+
+describe("setInstanceLicense", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ /**
+ * An instance holds exactly one license. Activating a key used to call
+ * create() directly, so re-entering the key already installed hit the
+ * license_key unique index. Prisma's P2002 surfaced through safe-action's
+ * handleServerError as a bare "INTERNAL_SERVER_ERROR" β€” the control panel
+ * showed an internal-error toast even though the licensing API returned 200.
+ */
+ it("clears the existing license before inserting, so re-activating the same key cannot collide", async () => {
+ const { setInstanceLicense } = await import("./mutations");
+ await setInstanceLicense(license);
+
+ expect(mockDeleteMany).toHaveBeenCalled();
+ expect(mockCreate).toHaveBeenCalledWith({
+ data: expect.objectContaining({ licenseKey: license.key }),
+ });
+
+ // Both operations must be in one transaction, or a failed insert would
+ // leave the instance with no license at all.
+ expect(mockTransaction).toHaveBeenCalledTimes(1);
+ expect(mockTransaction.mock.calls[0][0]).toHaveLength(2);
+ });
+
+ it("replaces a different installed key rather than accumulating rows", async () => {
+ const { setInstanceLicense } = await import("./mutations");
+ await setInstanceLicense({
+ ...license,
+ key: "RLYV4-ZZZZ-YYYY-XXXX-WWWW-99999",
+ });
+
+ // loadInstanceLicense reads the first row by id, so a stale row left
+ // behind here would keep winning over the key just activated.
+ expect(mockDeleteMany).toHaveBeenCalled();
+ expect(mockCreate).toHaveBeenCalledWith({
+ data: expect.objectContaining({
+ licenseKey: "RLYV4-ZZZZ-YYYY-XXXX-WWWW-99999",
+ }),
+ });
+ });
+});
diff --git a/apps/web/src/features/licensing/mutations.ts b/apps/web/src/features/licensing/mutations.ts
index ae383053f..26f789e27 100644
--- a/apps/web/src/features/licensing/mutations.ts
+++ b/apps/web/src/features/licensing/mutations.ts
@@ -214,6 +214,43 @@ export async function createLicenseCheckoutSession({
}
}
+/**
+ * Installs a validated license as the instance's license.
+ *
+ * An instance holds exactly one license, so this replaces whatever is
+ * currently installed rather than inserting alongside it. A plain create
+ * fails with a unique-constraint violation when the same key is re-entered,
+ * and silently accumulates rows when a different one is β€” loadInstanceLicense
+ * only ever reads the first by id, so the extras would be invisible but
+ * authoritative.
+ */
+export async function setInstanceLicense(license: {
+ key: string;
+ licenseeName: string | null;
+ licenseeEmail: string | null;
+ issuedAt: Date;
+ expiresAt: Date | null;
+ seats: number | null;
+ type: LicenseType;
+ whiteLabelAddon: boolean;
+}) {
+ await prisma.$transaction([
+ prisma.instanceLicense.deleteMany(),
+ prisma.instanceLicense.create({
+ data: {
+ licenseKey: license.key,
+ licenseeName: license.licenseeName,
+ licenseeEmail: license.licenseeEmail,
+ issuedAt: license.issuedAt,
+ expiresAt: license.expiresAt,
+ seats: license.seats,
+ type: license.type,
+ whiteLabelAddon: license.whiteLabelAddon,
+ },
+ }),
+ ]);
+}
+
export async function validateLicenseKey({
key,
fingerprint,