fix: use singleton in license key service (#16131)
* use singleton * fix typo * Fix getInstance call --------- Co-authored-by: Alex van Andel <me@alexvanandel.com>
This commit is contained in:
1 parent
b9a1522abc
commit
fba3f46779
5 files changed
+26
-8
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import type { NextMiddleware } from "next-api-middleware";
|
||||
|
||||
import LicenseKeyService from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { hashAPIKey } from "@calcom/features/ee/api-keys/lib/apiKeys";
|
||||
import { IS_PRODUCTION } from "@calcom/lib/constants";
|
||||
import prisma from "@calcom/prisma";
|
||||
@@ -18,7 +18,7 @@ export const dateNotInPast = function (date: Date) {
|
||||
|
||||
// This verifies the apiKey and sets the user if it is valid.
|
||||
export const verifyApiKey: NextMiddleware = async (req, res, next) => {
|
||||
const licenseKeyService = await LicenseKeyService.create();
|
||||
const licenseKeyService = await LicenseKeySingleton.getInstance();
|
||||
const hasValidLicense = await licenseKeyService.checkLicense();
|
||||
|
||||
if (!hasValidLicense && IS_PRODUCTION) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from
|
||||
import type { AuthOptions, Session } from "next-auth";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
import LicenseKeyService from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
@@ -65,7 +65,7 @@ export async function getServerSession(options: {
|
||||
return null;
|
||||
}
|
||||
|
||||
const licenseKeyService = await LicenseKeyService.create();
|
||||
const licenseKeyService = await LicenseKeySingleton.getInstance();
|
||||
const hasValidLicense = await licenseKeyService.checkLicense();
|
||||
|
||||
let upId = token.upId;
|
||||
|
||||
@@ -7,7 +7,7 @@ import CredentialsProvider from "next-auth/providers/credentials";
|
||||
import EmailProvider from "next-auth/providers/email";
|
||||
import GoogleProvider from "next-auth/providers/google";
|
||||
|
||||
import LicenseKeyService from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import createUsersAndConnectToOrg from "@calcom/features/ee/dsync/lib/users/createUsersAndConnectToOrg";
|
||||
import ImpersonationProvider from "@calcom/features/ee/impersonation/lib/ImpersonationProvider";
|
||||
import { getOrgFullOrigin, subdomainSuffix } from "@calcom/features/ee/organizations/lib/orgDomains";
|
||||
@@ -619,7 +619,7 @@ export const AUTH_OPTIONS: AuthOptions = {
|
||||
},
|
||||
async session({ session, token, user }) {
|
||||
log.debug("callbacks:session - Session callback called", safeStringify({ session, token, user }));
|
||||
const licenseKeyService = await LicenseKeyService.create();
|
||||
const licenseKeyService = await LicenseKeySingleton.getInstance();
|
||||
const hasValidLicense = await licenseKeyService.checkLicense();
|
||||
|
||||
const profileId = token.profileId;
|
||||
|
||||
@@ -113,4 +113,22 @@ export class NoopLicenseKeyService implements ILicenseKeyService {
|
||||
}
|
||||
}
|
||||
|
||||
export class LicenseKeySingleton {
|
||||
private static instance: ILicenseKeyService | null = null;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function -- Private constructor to prevent direct instantiation
|
||||
private constructor() {}
|
||||
|
||||
public static async getInstance(): Promise<ILicenseKeyService> {
|
||||
if (!LicenseKeySingleton.instance) {
|
||||
const licenseKey = await getDeploymentKey(prisma);
|
||||
const useNoop = !licenseKey || process.env.NEXT_PUBLIC_IS_E2E === "1";
|
||||
LicenseKeySingleton.instance = !useNoop
|
||||
? await LicenseKeyService.create()
|
||||
: new NoopLicenseKeyService();
|
||||
}
|
||||
return LicenseKeySingleton.instance;
|
||||
}
|
||||
}
|
||||
|
||||
export default LicenseKeyService;
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { waitUntil } from "@vercel/functions";
|
||||
|
||||
import LicenseKeyService, { UsageEvent } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
import { UsageEvent, LicenseKeySingleton } from "@calcom/ee/common/server/LicenseKeyService";
|
||||
|
||||
async function incrementUsage(event?: UsageEvent) {
|
||||
try {
|
||||
const licenseKeyService = await LicenseKeyService.create();
|
||||
const licenseKeyService = await LicenseKeySingleton.getInstance();
|
||||
await licenseKeyService.incrementUsage(event);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
|
||||
Reference in new issue
Block a user