Skip to content

Commit

Permalink
feat: add license info in getAutomatischInfo query (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
farukaydin committed Aug 10, 2023
1 parent ec42446 commit 84b7017
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
11 changes: 11 additions & 0 deletions packages/backend/src/graphql/queries/get-automatisch-info.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};

Expand Down
8 changes: 8 additions & 0 deletions packages/backend/src/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,14 @@ type AppHealth {

type GetAutomatischInfo {
isCloud: Boolean
license: License
}

type License {
id: String
name: String
expireAt: String
verified: Boolean
}

type GetTrialStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 };
6 changes: 2 additions & 4 deletions packages/backend/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -289,9 +289,7 @@ class User extends Base {
}

async $afterFind(): Promise<any> {
const hasValidLicense = await checkLicense();

if (hasValidLicense) return this;
if (await hasValidLicense()) return this;

if (Array.isArray(this.permissions)) {
this.permissions = this.permissions.filter((permission) => {
Expand Down

0 comments on commit 84b7017

Please sign in to comment.