Improve: NPS survey fetch (#21263)

pull/21199/head^2
Aaron Ogle 4 years ago committed by GitHub
parent 5f98a21cb0
commit 460ddc181d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/cloud/server/functions/buildRegistrationData.js
  2. 9
      app/cloud/server/functions/startRegisterWorkspace.js
  3. 12
      app/cloud/server/functions/syncWorkspace.js
  4. 11
      app/lib/server/startup/settings.js
  5. 2
      packages/rocketchat-i18n/i18n/en.i18n.json
  6. 67
      server/services/nps/getAndCreateNpsSurvey.ts
  7. 8
      server/services/nps/sendNpsResults.ts
  8. 4
      server/services/nps/service.ts

@ -31,6 +31,7 @@ export function buildWorkspaceRegistrationData() {
const agreePrivacyTerms = settings.get('Cloud_Service_Agree_PrivacyTerms');
const { organizationType, industry, size: orgSize, country, language, serverType: workspaceType } = stats.wizard;
const seats = Users.getActiveLocalUserCount();
return {
uniqueId: stats.uniqueId,
@ -38,6 +39,7 @@ export function buildWorkspaceRegistrationData() {
address,
contactName,
contactEmail,
seats,
allowMarketing,
accountName,
organizationType,

@ -10,18 +10,13 @@ import { buildWorkspaceRegistrationData } from './buildRegistrationData';
export function startRegisterWorkspace(resend = false) {
const { workspaceRegistered, connectToCloud } = retrieveRegistrationStatus();
if ((workspaceRegistered && connectToCloud) || process.env.TEST_MODE) {
syncWorkspace(true);
return true;
}
settings.updateById('Register_Server', true);
// If we still have client id lets see if they are still good before trying to register
if (workspaceRegistered) {
if (syncWorkspace(true)) {
return true;
}
}
const regInfo = buildWorkspaceRegistrationData();
const cloudUrl = settings.get('Cloud_Url');

@ -6,6 +6,7 @@ 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) {
@ -57,15 +58,22 @@ export function syncWorkspace(reconnectCheck = false) {
if (data.nps) {
const {
id: npsId,
startAt,
expireAt,
} = data.nps;
const startAt = new Date(data.nps.startAt);
Promise.await(NPS.create({
npsId,
startAt: new Date(startAt),
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

@ -2714,6 +2714,17 @@ settings.addGroup('Setup_Wizard', function() {
});
this.section('Cloud_Info', function() {
this.add('Nps_Url', 'https://nps.rocket.chat', {
type: 'string',
hidden: true,
readonly: true,
enableQuery: {
_id: 'Register_Server',
value: true,
},
secret: true,
});
this.add('Cloud_Url', 'https://cloud.rocket.chat', {
type: 'string',
hidden: true,

@ -2921,7 +2921,7 @@
"Notify_all_in_this_room": "Notify all in this room",
"NPS_survey_enabled": "Enable NPS Survey",
"NPS_survey_enabled_Description": "Allow NPS survey run for all users. Admins will receive an alert 2 months upfront the survey is launched",
"NPS_survey_is_scheduled_to-run-at__date__for_all_users": "NPS survey is scheduled to run at __date__ for all users. It's possible to turn off the survey on 'Admin > General > NPS'?",
"NPS_survey_is_scheduled_to-run-at__date__for_all_users": "NPS survey is scheduled to run at __date__ for all users. It's possible to turn off the survey on 'Admin > General > NPS'",
"Num_Agents": "# Agents",
"Number_in_seconds": "Number in seconds",
"Number_of_events": "Number of events",

@ -0,0 +1,67 @@
import { HTTP } from 'meteor/http';
import { Meteor } from 'meteor/meteor';
import { settings } from '../../../app/settings/server';
import { getWorkspaceAccessToken } from '../../../app/cloud/server';
import { UiKitBannerPayload } from '../../../definition/UIKit';
import { Banner } from '../../sdk';
import { IBanner, BannerPlatform } from '../../../definition/IBanner';
type NpsSurveyData = {
id: string;
platform: BannerPlatform[];
roles: string[];
survey: UiKitBannerPayload;
createdAt: Date;
startAt: Date;
expireAt: Date;
};
export const getAndCreateNpsSurvey = Meteor.bindEnvironment(async function getNpsSurvey(npsId: string) {
const token: string = getWorkspaceAccessToken();
if (!token) {
return false;
}
const npsEnabled = settings.get('NPS_survey_enabled');
if (!npsEnabled) {
return false;
}
const npsUrl = settings.get('Nps_Url');
try {
const result = HTTP.get(`${ npsUrl }/v1/surveys/${ npsId }`, {
headers: {
Authorization: `Bearer ${ token }`,
},
});
if (result.statusCode !== 200) {
console.log('invalid response from the nps service:', result);
return;
}
const surveyData = result.data as NpsSurveyData;
const banner: IBanner = {
_id: npsId,
platform: surveyData.platform,
createdAt: new Date(surveyData.createdAt),
expireAt: new Date(surveyData.expireAt),
startAt: new Date(surveyData.startAt),
_updatedAt: new Date(), // Needed by the IRocketChatRecord interface
roles: surveyData.roles,
createdBy: {
_id: 'rocket.cat',
username: 'rocket.cat',
},
view: surveyData.survey,
};
await Banner.create(banner);
} catch (e) {
throw new Error(e);
}
});

@ -10,16 +10,16 @@ type NPSResultPayload = {
votes: INpsVote[];
}
export const sendToCloud = Meteor.bindEnvironment(function sendToCloud(npsId: string, data: NPSResultPayload) {
const token: string = getWorkspaceAccessToken(true);
export const sendNpsResults = Meteor.bindEnvironment(function sendNpsResults(npsId: string, data: NPSResultPayload) {
const token: string = getWorkspaceAccessToken();
if (!token) {
return false;
}
const cloudUrl = settings.get('Cloud_Url');
const npsUrl = settings.get('Nps_Url');
try {
return HTTP.post(`${ cloudUrl }/v1/nps/surveys/${ npsId }/results`, {
return HTTP.post(`${ npsUrl }/v1/surveys/${ npsId }/results`, {
headers: {
Authorization: `Bearer ${ token }`,
},

@ -9,7 +9,7 @@ import { NPSStatus, INpsVoteStatus, INpsVote } from '../../../definition/INps';
import { INPSService, NPSVotePayload, NPSCreatePayload } from '../../sdk/types/INPSService';
import { ServiceClass } from '../../sdk/types/ServiceClass';
import { Banner, NPS } from '../../sdk';
import { sendToCloud } from './sendToCloud';
import { sendNpsResults } from './sendNpsResults';
import { getBannerForAdmins, notifyAdmins } from './notification';
export class NPSService extends ServiceClass implements INPSService {
@ -128,7 +128,7 @@ export class NPSService extends ServiceClass implements INPSService {
total,
votes,
};
sendToCloud(nps._id, payload);
sendNpsResults(nps._id, payload);
this.NpsVote.updateVotesToSent(voteIds);
}

Loading…
Cancel
Save