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/server/services/nps/getAndCreateNpsSurvey.ts

67 lines
1.6 KiB

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);
}
});