fix twilio config

This commit is contained in:
Simon Larsen 2023-09-07 16:29:36 +05:30
parent 10348f1c72
commit 7310c5ddeb
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
4 changed files with 63 additions and 45 deletions

View File

@ -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'
]

View File

@ -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<void> {
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;

View File

@ -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<void> {
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;

View File

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