diff --git a/Notification/Config.ts b/Notification/Config.ts index ac09c1db3e..2e420d1287 100644 --- a/Notification/Config.ts +++ b/Notification/Config.ts @@ -1,6 +1,11 @@ +import GlobalConfig from 'Model/Models/GlobalConfig'; import Hostname from 'Common/Types/API/Hostname'; import Email from 'Common/Types/Email'; +import BadDataException from 'Common/Types/Exception/BadDataException'; +import ObjectID from 'Common/Types/ObjectID'; import Port from 'Common/Types/Port'; +import GlobalConfigService from 'CommonServer/Services/GlobalConfigService'; +import Phone from 'Common/Types/Phone'; export const ShouldUseInternalSmtp: boolean = process.env['USE_INTERNAL_SMTP'] === 'true'; @@ -29,12 +34,44 @@ export const InternalSmtpEmail: Email = new Email( export const InternalSmtpFromName: string = process.env['INTERNAL_SMTP_NAME'] || ''; -export const TwilioAccountSid: string = process.env['TWILIO_ACCOUNT_SID'] || ''; -export const TwilioAuthToken: string = process.env['TWILIO_AUTH_TOKEN'] || ''; -export const TwilioPhoneNumber: string = - process.env['TWILIO_PHONE_NUMBER'] || ''; + export interface TwilioConfig { + accountSid: string; + authToken: string; + phoneNumber: Phone; + } + +export const getTwilioConfig = async (): Promise< TwilioConfig | null > => { + const globalConfig: GlobalConfig | null = + await GlobalConfigService.findOneBy({ + query: { + _id: ObjectID.getZeroObjectID().toString(), + }, + props: { + isRoot: true, + }, + select: { + twilioAccountSID: true, + twilioAuthToken: true, + twilioPhoneNumber: true, + }, + }); + + if (!globalConfig) { + throw new BadDataException('Global Config not found'); + } + + if(!globalConfig.twilioAccountSID || !globalConfig.twilioAuthToken || !globalConfig.twilioPhoneNumber) { + return null; + } + + return { + accountSid: globalConfig.twilioAccountSID, + authToken: globalConfig.twilioAuthToken, + phoneNumber: globalConfig.twilioPhoneNumber, + } +} + - export const SMSDefaultCostInCents: number = process.env[ 'SMS_DEFAULT_COST_IN_CENTS' ] diff --git a/Notification/Services/CallService.ts b/Notification/Services/CallService.ts index ab3041beec..f345aab4e6 100644 --- a/Notification/Services/CallService.ts +++ b/Notification/Services/CallService.ts @@ -1,13 +1,9 @@ import ObjectID from 'Common/Types/ObjectID'; import Phone from 'Common/Types/Phone'; import { - CallDefaultCostInCentsPerMinute, - TwilioAccountSid, - TwilioAuthToken, - TwilioPhoneNumber, + CallDefaultCostInCentsPerMinute, TwilioConfig, getTwilioConfig, } from '../Config'; import Twilio from 'twilio'; -import TwilioUtil from '../Utils/Twilio'; import CallLog from 'Model/Models/CallLog'; import CallStatus from 'Common/Types/Call/CallStatus'; import CallRequest, { GatherInput, Say } from 'Common/Types/Call/CallRequest'; @@ -23,6 +19,7 @@ import OneUptimeDate from 'Common/Types/Date'; import JSONFunctions from 'Common/Types/JSONFunctions'; import UserOnCallLogTimelineService from 'CommonServer/Services/UserOnCallLogTimelineService'; import UserNotificationStatus from 'Common/Types/UserNotification/UserNotificationStatus'; +import BadDataException from 'Common/Types/Exception/BadDataException'; export default class CallService { public static async makeCall( @@ -34,13 +31,17 @@ export default class CallService { userOnCallLogTimelineId?: ObjectID | undefined; // user notification log timeline id } ): Promise { - TwilioUtil.checkEnvironmentVariables(); + const twilioConfig: TwilioConfig | null = await getTwilioConfig(); - const client: Twilio.Twilio = Twilio(TwilioAccountSid, TwilioAuthToken); + if(!twilioConfig){ + throw new BadDataException("Twilio Config not found"); + } + + const client: Twilio.Twilio = Twilio(twilioConfig.accountSid, twilioConfig.authToken); const callLog: CallLog = new CallLog(); callLog.toNumber = callRequest.to; - callLog.fromNumber = options.from || new Phone(TwilioPhoneNumber); + callLog.fromNumber = options.from || twilioConfig.phoneNumber; callLog.callData = options && options.isSensitive ? { message: 'This call is sensitive and is not logged' } @@ -216,7 +217,7 @@ export default class CallService { from: options && options.from ? options.from.toString() - : TwilioPhoneNumber.toString(), // From a valid Twilio number + : twilioConfig.phoneNumber.toString(), // From a valid Twilio number }); callLog.status = CallStatus.Success; diff --git a/Notification/Services/SmsService.ts b/Notification/Services/SmsService.ts index 7ff18d2227..0def1da838 100644 --- a/Notification/Services/SmsService.ts +++ b/Notification/Services/SmsService.ts @@ -1,10 +1,7 @@ import ObjectID from 'Common/Types/ObjectID'; import Phone from 'Common/Types/Phone'; import { - SMSDefaultCostInCents, - TwilioAccountSid, - TwilioAuthToken, - TwilioPhoneNumber, + SMSDefaultCostInCents, TwilioConfig, getTwilioConfig, } from '../Config'; import Twilio from 'twilio'; import SmsLog from 'Model/Models/SmsLog'; @@ -16,9 +13,9 @@ import Project from 'Model/Models/Project'; import { MessageInstance } from 'twilio/lib/rest/api/v2010/account/message'; import NotificationService from 'CommonServer/Services/NotificationService'; import logger from 'CommonServer/Utils/Logger'; -import TwilioUtil from '../Utils/Twilio'; import UserOnCallLogTimelineService from 'CommonServer/Services/UserOnCallLogTimelineService'; import UserNotificationStatus from 'Common/Types/UserNotification/UserNotificationStatus'; +import BadDataException from 'Common/Types/Exception/BadDataException'; export default class SmsService { public static async sendSms( @@ -31,13 +28,18 @@ export default class SmsService { userOnCallLogTimelineId?: ObjectID | undefined; } ): Promise { - TwilioUtil.checkEnvironmentVariables(); + const twilioConfig: TwilioConfig | null = await getTwilioConfig(); - const client: Twilio.Twilio = Twilio(TwilioAccountSid, TwilioAuthToken); + if(!twilioConfig){ + throw new BadDataException("Twilio Config not found"); + } + + + const client: Twilio.Twilio = Twilio(twilioConfig.accountSid, twilioConfig.authToken); const smsLog: SmsLog = new SmsLog(); smsLog.toNumber = to; - smsLog.fromNumber = options.from || new Phone(TwilioPhoneNumber); + smsLog.fromNumber = options.from || twilioConfig.phoneNumber; smsLog.smsText = options && options.isSensitive ? 'This message is sensitive and is not logged' @@ -213,7 +215,7 @@ export default class SmsService { from: options && options.from ? options.from.toString() - : TwilioPhoneNumber.toString(), // From a valid Twilio number + : twilioConfig.phoneNumber.toString(), // From a valid Twilio number }); smsLog.status = SmsStatus.Success; diff --git a/Notification/Utils/Twilio.ts b/Notification/Utils/Twilio.ts deleted file mode 100644 index 188853ee98..0000000000 --- a/Notification/Utils/Twilio.ts +++ /dev/null @@ -1,22 +0,0 @@ -import BadDataException from 'Common/Types/Exception/BadDataException'; -import { - TwilioAccountSid, - TwilioAuthToken, - TwilioPhoneNumber, -} from '../Config'; - -export default class TwilioUtil { - public static checkEnvironmentVariables(): void { - if (!TwilioAccountSid) { - throw new BadDataException('TwilioAccountSid is not configured'); - } - - if (!TwilioAuthToken) { - throw new BadDataException('TwilioAuthToken is not configured'); - } - - if (!TwilioPhoneNumber) { - throw new BadDataException('TwilioPhoneNumber is not configured'); - } - } -}