-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 0 additions & 110 deletions
110
apps/meteor/app/cloud/server/functions/syncWorkspace.ts
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
apps/meteor/app/cloud/server/functions/syncWorkspace/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { CloudWorkspaceAccessTokenError } from '../getWorkspaceAccessToken'; | ||
import { getWorkspaceLicense } from '../getWorkspaceLicense'; | ||
import { getCachedSupportedVersionsToken } from '../supportedVersionsToken/supportedVersionsToken'; | ||
import { syncCloudData } from './syncCloudData'; | ||
|
||
export async function syncWorkspace() { | ||
try { | ||
await syncCloudData(); | ||
await getWorkspaceLicense(); | ||
} catch (error) { | ||
if (error instanceof CloudWorkspaceAccessTokenError) { | ||
// TODO: Remove License if there is no access token | ||
} | ||
} | ||
|
||
await getCachedSupportedVersionsToken.reset(); | ||
} |
85 changes: 85 additions & 0 deletions
85
apps/meteor/app/cloud/server/functions/syncWorkspace/syncCloudData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { NPS, Banner } from '@rocket.chat/core-services'; | ||
import { Settings } from '@rocket.chat/models'; | ||
import { serverFetch as fetch } from '@rocket.chat/server-fetch'; | ||
|
||
import { SystemLogger } from '../../../../../server/lib/logger/system'; | ||
import { getAndCreateNpsSurvey } from '../../../../../server/services/nps/getAndCreateNpsSurvey'; | ||
import { settings } from '../../../../settings/server'; | ||
import { buildWorkspaceRegistrationData } from '../buildRegistrationData'; | ||
import { generateWorkspaceBearerHttpHeaderOrThrow } from '../getWorkspaceAccessToken'; | ||
import { handleResponse } from '../supportedVersionsToken/supportedVersionsToken'; | ||
|
||
export async function syncCloudData() { | ||
const info = await buildWorkspaceRegistrationData(undefined); | ||
|
||
const token = await generateWorkspaceBearerHttpHeaderOrThrow(true); | ||
|
||
const request = await handleResponse( | ||
fetch(`${settings.get('Cloud_Workspace_Registration_Client_Uri')}/client`, { | ||
headers: { | ||
...token, | ||
}, | ||
body: info, | ||
method: 'POST', | ||
}), | ||
); | ||
|
||
if (!request.success) { | ||
return SystemLogger.error({ | ||
msg: 'Failed to sync with Rocket.Chat Cloud', | ||
url: '/client', | ||
err: request.error, | ||
}); | ||
} | ||
|
||
const data = request.result as any; | ||
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), | ||
}); | ||
} | ||
} | ||
|
||
return true; | ||
} |