update
This commit is contained in:
@ -3,19 +3,29 @@ import * as paths from '../paths.js';
|
||||
import { logger } from '../logger.js';
|
||||
import type { SzPlatformService } from '../platformservice.js';
|
||||
|
||||
export interface ISmsConstructorOptions {
|
||||
apiGatewayApiToken: string;
|
||||
}
|
||||
import type { ISmsConfig } from '../config/sms.config.js';
|
||||
import { ConfigValidator, smsConfigSchema } from '../config/index.js';
|
||||
|
||||
export class SmsService {
|
||||
public platformServiceRef: SzPlatformService;
|
||||
public projectinfo: plugins.projectinfo.ProjectInfo;
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
public options: ISmsConstructorOptions;
|
||||
public config: ISmsConfig;
|
||||
|
||||
constructor(platformServiceRefArg: SzPlatformService, optionsArg: ISmsConstructorOptions) {
|
||||
constructor(platformServiceRefArg: SzPlatformService, options: ISmsConfig) {
|
||||
this.platformServiceRef = platformServiceRefArg;
|
||||
this.options = optionsArg;
|
||||
|
||||
// Validate and apply defaults to configuration
|
||||
const validationResult = ConfigValidator.validate(options, smsConfigSchema);
|
||||
|
||||
if (!validationResult.valid) {
|
||||
logger.warn(`SMS service configuration has validation errors: ${validationResult.errors.join(', ')}`);
|
||||
}
|
||||
|
||||
// Set configuration with defaults
|
||||
this.config = validationResult.config;
|
||||
|
||||
// Add router to platform service
|
||||
this.platformServiceRef.typedrouter.addTypedRouter(this.typedrouter);
|
||||
}
|
||||
|
||||
@ -53,8 +63,11 @@ export class SmsService {
|
||||
}
|
||||
|
||||
public async sendSms(toNumber: number, fromName: string, messageText: string) {
|
||||
// Use default sender if not specified
|
||||
const sender = fromName || this.config.defaultSender || 'PlatformService';
|
||||
|
||||
const payload = {
|
||||
sender: fromName,
|
||||
sender,
|
||||
message: messageText,
|
||||
recipients: [{ msisdn: toNumber }],
|
||||
};
|
||||
@ -63,7 +76,7 @@ export class SmsService {
|
||||
method: 'POST',
|
||||
requestBody: JSON.stringify(payload),
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(`${this.options.apiGatewayApiToken}:`).toString('base64')}`,
|
||||
Authorization: `Basic ${Buffer.from(`${this.config.apiGatewayApiToken}:`).toString('base64')}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
Reference in New Issue
Block a user