Skip to content

Commit

Permalink
syncWorkspace/syncWorkspace.back
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Sep 13, 2023
1 parent 24debfe commit 952fcde
Showing 1 changed file with 77 additions and 32 deletions.
109 changes: 77 additions & 32 deletions apps/meteor/app/cloud/server/functions/syncWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,43 @@ import { SystemLogger } from '../../../../server/lib/logger/system';
import { getAndCreateNpsSurvey } from '../../../../server/services/nps/getAndCreateNpsSurvey';
import { settings } from '../../../settings/server';
import { buildWorkspaceRegistrationData } from './buildRegistrationData';
import { generateWorkspaceBearerHttpHeader } from './getWorkspaceAccessToken';
import { getWorkspaceAccessToken } from './getWorkspaceAccessToken';
import { getWorkspaceLicense } from './getWorkspaceLicense';
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
import { getCachedSupportedVersionsToken, wrapPromise } from './supportedVersionsToken/supportedVersionsToken';
import { getCachedSupportedVersionsToken } from './supportedVersionsToken/supportedVersionsToken';

if (data.nps) {
const { id: npsId, expireAt } = data.nps;
export async function syncWorkspace(reconnectCheck = false) {
const { workspaceRegistered, connectToCloud } = await retrieveRegistrationStatus();
if (!workspaceRegistered || (!connectToCloud && !reconnectCheck)) {
return false;
}

const startAt = new Date(data.nps.startAt);
const info = await buildWorkspaceRegistrationData(undefined);

await NPS.create({
npsId,
startAt,
expireAt: new Date(expireAt),
createdBy: {
_id: 'rocket.cat',
username: 'rocket.cat',
},
});
const workspaceUrl = settings.get('Cloud_Workspace_Registration_Client_Uri');

const now = new Date();
let result;
try {
const headers: Record<string, string> = {};
const token = await getWorkspaceAccessToken(true);

if (startAt.getFullYear() === now.getFullYear() && startAt.getMonth() === now.getMonth() && startAt.getDate() === now.getDate()) {
await getAndCreateNpsSurvey(npsId);
}
if (token) {
headers.Authorization = `Bearer ${token}`;
} else {
return false;
}

// add banners
if (data.banners) {
for await (const banner of data.banners) {
const { createdAt, expireAt, startAt } = banner;

await Banner.create({
...banner,
createdAt: new Date(createdAt),
expireAt: new Date(expireAt),
startAt: new Date(startAt),
});
}
const request = await fetch(`${workspaceUrl}/client`, {
headers,
body: info,
method: 'POST',
});

if (!request.ok) {
throw new Error((await request.json()).error);
}

result = await request.json();
} catch (err: any) {
SystemLogger.error({
msg: 'Failed to sync with Rocket.Chat Cloud',
Expand All @@ -55,11 +52,59 @@ import { getCachedSupportedVersionsToken, wrapPromise } from './supportedVersion

return false;
} finally {
// always fetch the license
// aways fetch the license
await getWorkspaceLicense();
}

await getCachedSupportedVersionsToken.reset();
const data = result;
if (!data) {
return true;
}

if (data.publicKey) {
await Settings.updateValueById('Cloud_Workspace_PublicKey', data.publicKey);
}

if (data.trial?.trialId) {
await Settings.updateValueById('Cloud_Workspace_Had_Trial', true);
}

if (data.nps) {
const { id: npsId, expireAt } = data.nps;

const startAt = new Date(data.nps.startAt);

await NPS.create({
npsId,
startAt,
expireAt: new Date(expireAt),
createdBy: {
_id: 'rocket.cat',
username: 'rocket.cat',
},
});

const now = new Date();

if (startAt.getFullYear() === now.getFullYear() && startAt.getMonth() === now.getMonth() && startAt.getDate() === now.getDate()) {
await getAndCreateNpsSurvey(npsId);
}
}

// add banners
if (data.banners) {
for await (const banner of data.banners) {
const { createdAt, expireAt, startAt } = banner;

await Banner.create({
...banner,
createdAt: new Date(createdAt),
expireAt: new Date(expireAt),
startAt: new Date(startAt),
});
}
}

await getCachedSupportedVersionsToken.reset();
return true;
}

0 comments on commit 952fcde

Please sign in to comment.