The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/cloud/server/functions/syncWorkspace.js

98 lines
2.3 KiB

import { HTTP } from 'meteor/http';
import { buildWorkspaceRegistrationData } from './buildRegistrationData';
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
import { getWorkspaceAccessToken } from './getWorkspaceAccessToken';
import { getWorkspaceLicense } from './getWorkspaceLicense';
import { Settings } from '../../../models';
import { settings } from '../../../settings';
import { getAndCreateNpsSurvey } from '../../../../server/services/nps/getAndCreateNpsSurvey';
import { NPS, Banner } from '../../../../server/sdk';
export function syncWorkspace(reconnectCheck = false) {
const { workspaceRegistered, connectToCloud } = retrieveRegistrationStatus();
if (!workspaceRegistered || (!connectToCloud && !reconnectCheck)) {
return false;
}
const info = buildWorkspaceRegistrationData();
const workspaceUrl = settings.get('Cloud_Workspace_Registration_Client_Uri');
let result;
try {
const headers = {};
const token = getWorkspaceAccessToken(true);
if (token) {
headers.Authorization = `Bearer ${ token }`;
} else {
return false;
}
result = HTTP.post(`${ workspaceUrl }/client`, {
data: info,
headers,
});
getWorkspaceLicense();
} catch (e) {
if (e.response && e.response.data && e.response.data.error) {
console.error(`Failed to sync with Rocket.Chat Cloud. Error: ${ e.response.data.error }`);
} else {
console.error(e);
}
return false;
}
const { data } = result;
if (!data) {
return true;
}
if (data.publicKey) {
Settings.updateValueById('Cloud_Workspace_PublicKey', data.publicKey);
}
if (data.nps) {
const {
id: npsId,
expireAt,
} = data.nps;
const startAt = new Date(data.nps.startAt);
Promise.await(NPS.create({
npsId,
startAt,
expireAt: new Date(expireAt),
}));
const now = new Date();
if (startAt.getFullYear() === now.getFullYear() && startAt.getMonth() === now.getMonth() && startAt.getDate() === now.getDate()) {
getAndCreateNpsSurvey(npsId);
}
}
// add banners
if (data.banners) {
for (const banner of data.banners) {
const {
createdAt,
expireAt,
startAt,
} = banner;
Promise.await(Banner.create({
...banner,
createdAt: new Date(createdAt),
expireAt: new Date(expireAt),
startAt: new Date(startAt),
}));
}
}
return true;
}