From 84b701747fb61f92f8221e2c7b26f7b151d2212f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20Ayd=C4=B1n?= Date: Thu, 10 Aug 2023 23:18:10 +0200 Subject: [PATCH] feat: add license info in getAutomatischInfo query (#1202) --- .../src/graphql/queries/get-automatisch-info.ts | 11 +++++++++++ packages/backend/src/graphql/schema.graphql | 8 ++++++++ .../helpers/{check-license.ee.ts => license.ee.ts} | 14 ++++++++++---- packages/backend/src/models/user.ts | 6 ++---- 4 files changed, 31 insertions(+), 8 deletions(-) rename packages/backend/src/helpers/{check-license.ee.ts => license.ee.ts} (70%) diff --git a/packages/backend/src/graphql/queries/get-automatisch-info.ts b/packages/backend/src/graphql/queries/get-automatisch-info.ts index 43937e5f..0886d563 100644 --- a/packages/backend/src/graphql/queries/get-automatisch-info.ts +++ b/packages/backend/src/graphql/queries/get-automatisch-info.ts @@ -1,8 +1,19 @@ import appConfig from '../../config/app'; +import { getLicense } from '../../helpers/license.ee'; const getAutomatischInfo = async () => { + const license = await getLicense(); + + const computedLicense = { + id: license ? license.id : null, + name: license ? license.name : null, + expireAt: license ? license.expireAt : null, + verified: license ? true : false, + }; + return { isCloud: appConfig.isCloud, + license: computedLicense, }; }; diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 87f803cf..9eeadbbe 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -593,6 +593,14 @@ type AppHealth { type GetAutomatischInfo { isCloud: Boolean + license: License +} + +type License { + id: String + name: String + expireAt: String + verified: Boolean } type GetTrialStatus { diff --git a/packages/backend/src/helpers/check-license.ee.ts b/packages/backend/src/helpers/license.ee.ts similarity index 70% rename from packages/backend/src/helpers/check-license.ee.ts rename to packages/backend/src/helpers/license.ee.ts index 4e2143f6..6426912f 100644 --- a/packages/backend/src/helpers/check-license.ee.ts +++ b/packages/backend/src/helpers/license.ee.ts @@ -5,7 +5,13 @@ import memoryCache from 'memory-cache'; const CACHE_DURATION = 1000 * 60 * 60 * 24; // 24 hours in milliseconds -const checkLicense = async () => { +const hasValidLicense = async () => { + const license = await getLicense(); + + return license ? true : false; +}; + +const getLicense = async () => { const licenseKey = appConfig.licenseKey; if (!licenseKey) { @@ -20,13 +26,13 @@ const checkLicense = async () => { } else { try { const { data } = await axios.post(url, { licenseKey }); - memoryCache.put(url, data.verified, CACHE_DURATION); + memoryCache.put(url, data, CACHE_DURATION); - return data.verified; + return data; } catch (error) { return false; } } }; -export default checkLicense; +export { getLicense, hasValidLicense }; diff --git a/packages/backend/src/models/user.ts b/packages/backend/src/models/user.ts index 7afee61b..74612ce5 100644 --- a/packages/backend/src/models/user.ts +++ b/packages/backend/src/models/user.ts @@ -4,7 +4,7 @@ import crypto from 'node:crypto'; import { ModelOptions, QueryContext } from 'objection'; import appConfig from '../config/app'; -import checkLicense from '../helpers/check-license.ee'; +import { hasValidLicense } from '../helpers/license.ee'; import userAbility from '../helpers/user-ability'; import Base from './base'; import Connection from './connection'; @@ -289,9 +289,7 @@ class User extends Base { } async $afterFind(): Promise { - const hasValidLicense = await checkLicense(); - - if (hasValidLicense) return this; + if (await hasValidLicense()) return this; if (Array.isArray(this.permissions)) { this.permissions = this.permissions.filter((permission) => {