diff --git a/apps/meteor/app/cloud/server/functions/buildRegistrationData.ts b/apps/meteor/app/cloud/server/functions/buildRegistrationData.ts index c2bd91e82dd8..ea94db8d17a1 100644 --- a/apps/meteor/app/cloud/server/functions/buildRegistrationData.ts +++ b/apps/meteor/app/cloud/server/functions/buildRegistrationData.ts @@ -38,6 +38,7 @@ export type WorkspaceRegistrationData = { MAC: number; // activeContactsBillingMonth: number; // activeContactsYesterday: number; + statsToken?: string; }; export async function buildWorkspaceRegistrationData(contactEmail: T): Promise> { @@ -92,5 +93,6 @@ export async function buildWorkspaceRegistrationData { const rcStatistics = await statistics.get(); rcStatistics.createdAt = new Date(); - await Statistics.insertOne(rcStatistics); + const { insertedId } = await Statistics.insertOne(rcStatistics); + rcStatistics._id = insertedId; + return rcStatistics; }, }; diff --git a/apps/meteor/server/cron/statistics.ts b/apps/meteor/server/cron/statistics.ts index 7e7dea6adbc7..44dcb554824c 100644 --- a/apps/meteor/server/cron/statistics.ts +++ b/apps/meteor/server/cron/statistics.ts @@ -1,5 +1,6 @@ import { cronJobs } from '@rocket.chat/cron'; import type { Logger } from '@rocket.chat/logger'; +import { Statistics } from '@rocket.chat/models'; import { serverFetch as fetch } from '@rocket.chat/server-fetch'; import { Meteor } from 'meteor/meteor'; @@ -8,9 +9,7 @@ import { settings } from '../../app/settings/server'; import { statistics } from '../../app/statistics/server'; async function generateStatistics(logger: Logger): Promise { - const cronStatistics: Record = await statistics.save(); - - cronStatistics.host = Meteor.absoluteUrl(); + const cronStatistics = await statistics.save(); if (!settings.get('Statistics_reporting')) { return; @@ -20,11 +19,20 @@ async function generateStatistics(logger: Logger): Promise { const token = await getWorkspaceAccessToken(); const headers = { ...(token && { Authorization: `Bearer ${token}` }) }; - await fetch('https://collector.rocket.chat/', { + const response = await fetch('https://collector.rocket.chat/', { method: 'POST', - body: cronStatistics, + body: { + ...cronStatistics, + host: Meteor.absoluteUrl(), + }, headers, }); + + const { statsToken } = await response.json(); + + if (statsToken != null) { + await Statistics.updateOne({ _id: cronStatistics._id }, { $set: { statsToken } }); + } } catch (error) { /* error*/ logger.warn('Failed to send usage report'); diff --git a/packages/core-typings/src/IStats.ts b/packages/core-typings/src/IStats.ts index 7fd5cd8218bc..0df389f2dd86 100644 --- a/packages/core-typings/src/IStats.ts +++ b/packages/core-typings/src/IStats.ts @@ -227,4 +227,5 @@ export interface IStats { webRTCEnabled: boolean; webRTCEnabledForOmnichannel: boolean; omnichannelWebRTCCalls: number; + statsToken?: string; }