2024-02-16 13:28:40 +01:00
|
|
|
import * as plugins from '../plugins.js';
|
|
|
|
import * as paths from '../paths.js';
|
|
|
|
import { logger } from '../logger.js';
|
2025-03-15 16:21:37 +00:00
|
|
|
import type { SzPlatformService } from '../platformservice.js';
|
2024-02-16 13:28:40 +01:00
|
|
|
|
2025-05-08 12:46:10 +00:00
|
|
|
import type { ISmsConfig } from '../config/sms.config.js';
|
|
|
|
import { ConfigValidator, smsConfigSchema } from '../config/index.js';
|
2024-02-16 13:28:40 +01:00
|
|
|
|
|
|
|
export class SmsService {
|
|
|
|
public platformServiceRef: SzPlatformService;
|
|
|
|
public projectinfo: plugins.projectinfo.ProjectInfo;
|
|
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
2025-05-08 12:46:10 +00:00
|
|
|
public config: ISmsConfig;
|
2024-02-16 13:28:40 +01:00
|
|
|
|
2025-05-08 12:46:10 +00:00
|
|
|
constructor(platformServiceRefArg: SzPlatformService, options: ISmsConfig) {
|
2024-02-16 13:28:40 +01:00
|
|
|
this.platformServiceRef = platformServiceRefArg;
|
2025-05-08 12:46:10 +00:00
|
|
|
|
|
|
|
// 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
|
2024-02-16 13:28:40 +01:00
|
|
|
this.platformServiceRef.typedrouter.addTypedRouter(this.typedrouter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* starts the financeflow instance
|
|
|
|
*/
|
|
|
|
public async start() {
|
|
|
|
logger.log('info', `starting sms service`);
|
|
|
|
this.projectinfo = new plugins.projectinfo.ProjectInfo(paths.packageDir);
|
|
|
|
this.typedrouter.addTypedHandler(
|
|
|
|
new plugins.typedrequest.TypedHandler<plugins.servezoneInterfaces.platformservice.sms.IRequest_SendSms>(
|
|
|
|
'sendSms',
|
|
|
|
async (reqData) => {
|
|
|
|
await this.sendSms(reqData.toNumber, reqData.fromName, reqData.messageText);
|
|
|
|
return {
|
|
|
|
status: 'ok',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.typedrouter.addTypedHandler(
|
|
|
|
new plugins.typedrequest.TypedHandler<plugins.servezoneInterfaces.platformservice.sms.IRequest_SendVerificationCode>(
|
|
|
|
'sendVerificationCode',
|
|
|
|
async (reqData) => {
|
|
|
|
const verificationCode = (
|
|
|
|
await this.sendVerificationCode(reqData.toNumber, reqData.fromName)
|
|
|
|
).toString();
|
|
|
|
return {
|
|
|
|
status: 'ok',
|
|
|
|
verificationCode,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendSms(toNumber: number, fromName: string, messageText: string) {
|
2025-05-08 12:46:10 +00:00
|
|
|
// Use default sender if not specified
|
|
|
|
const sender = fromName || this.config.defaultSender || 'PlatformService';
|
|
|
|
|
2024-02-16 13:28:40 +01:00
|
|
|
const payload = {
|
2025-05-08 12:46:10 +00:00
|
|
|
sender,
|
2024-02-16 13:28:40 +01:00
|
|
|
message: messageText,
|
|
|
|
recipients: [{ msisdn: toNumber }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const resp = await plugins.smartrequest.request('https://gatewayapi.com/rest/mtsms', {
|
|
|
|
method: 'POST',
|
|
|
|
requestBody: JSON.stringify(payload),
|
|
|
|
headers: {
|
2025-05-08 12:46:10 +00:00
|
|
|
Authorization: `Basic ${Buffer.from(`${this.config.apiGatewayApiToken}:`).toString('base64')}`,
|
2024-02-16 13:28:40 +01:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const json = await resp.body;
|
|
|
|
logger.log('info', `sent an sms to ${toNumber} with text '${messageText}'`, {
|
|
|
|
eventType: 'sentSms',
|
|
|
|
sms: {
|
|
|
|
fromName: fromName,
|
|
|
|
toNumber: toNumber.toString(),
|
|
|
|
messageText: messageText,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
console.log(JSON.stringify(json, null, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendVerificationCode(toNumber: number, fromName: string) {
|
|
|
|
let verificationCode = Math.floor(100000 + Math.random() * 900000);
|
|
|
|
await this.sendSms(toNumber, fromName, `Your verification code: ${verificationCode}`);
|
|
|
|
return verificationCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async stop() {}
|
|
|
|
}
|