verdnatura-chat/app/lib/methods/enterpriseModules.ts

69 lines
2.3 KiB
TypeScript

import sdk from '../rocketchat/services/sdk';
import { compareServerVersion } from '../utils';
import { store as reduxStore } from '../auxStore';
import database from '../database';
import log from '../../utils/log';
import { clearEnterpriseModules, setEnterpriseModules as setEnterpriseModulesAction } from '../../actions/enterpriseModules';
const LICENSE_OMNICHANNEL_MOBILE_ENTERPRISE = 'omnichannel-mobile-enterprise';
const LICENSE_LIVECHAT_ENTERPRISE = 'livechat-enterprise';
export async function setEnterpriseModules() {
try {
const { server: serverId } = reduxStore.getState().server;
const serversDB = database.servers;
const serversCollection = serversDB.get('servers');
let server;
try {
server = await serversCollection.find(serverId);
} catch {
// Server not found
}
if (server?.enterpriseModules) {
reduxStore.dispatch(setEnterpriseModulesAction(server.enterpriseModules.split(',')));
return;
}
reduxStore.dispatch(clearEnterpriseModules());
} catch (e) {
log(e);
}
}
export function getEnterpriseModules() {
return new Promise<void>(async resolve => {
try {
const { version: serverVersion, server: serverId } = reduxStore.getState().server;
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.1.0')) {
// RC 3.1.0
const enterpriseModules = await sdk.methodCallWrapper('license:getModules');
if (enterpriseModules) {
const serversDB = database.servers;
const serversCollection = serversDB.get('servers');
const server = await serversCollection.find(serverId);
await serversDB.write(async () => {
await server.update(s => {
s.enterpriseModules = enterpriseModules.join(',');
});
});
reduxStore.dispatch(setEnterpriseModulesAction(enterpriseModules));
return resolve();
}
}
reduxStore.dispatch(clearEnterpriseModules());
} catch (e) {
log(e);
}
return resolve();
});
}
export function hasLicense(module: string) {
const { enterpriseModules } = reduxStore.getState();
return enterpriseModules.includes(module);
}
export function isOmnichannelModuleAvailable() {
const { enterpriseModules } = reduxStore.getState();
return [LICENSE_OMNICHANNEL_MOBILE_ENTERPRISE, LICENSE_LIVECHAT_ENTERPRISE].some(module => enterpriseModules.includes(module));
}