mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
add server to monorepo.
This commit is contained in:
60
packages/server/src/services/SMSClient/EasySmsClient.ts
Normal file
60
packages/server/src/services/SMSClient/EasySmsClient.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import axios from 'axios';
|
||||
import SMSClientInterface from '@/services/SMSClient/SMSClientInterfaces';
|
||||
import config from '@/config';
|
||||
|
||||
export default class EasySMSClient implements SMSClientInterface {
|
||||
token: string;
|
||||
clientName: string = 'easysms';
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} token
|
||||
*/
|
||||
constructor(token: string) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes the phone number string.
|
||||
* @param {string} phoneNumber
|
||||
* @returns {string}
|
||||
*/
|
||||
normlizePhoneNumber = (phoneNumber: string) => {
|
||||
let normalized = phoneNumber;
|
||||
|
||||
normalized = normalized.replace(/^00/, '');
|
||||
normalized = normalized.replace(/^0/, '');
|
||||
normalized = normalized.replace(/^218/, '');
|
||||
|
||||
return normalized;
|
||||
};
|
||||
|
||||
/**
|
||||
* Send message to given phone number via easy SMS client.
|
||||
* @param {string} to
|
||||
* @param {string} message
|
||||
*/
|
||||
send = (to: string, message: string) => {
|
||||
const API_KEY = this.token;
|
||||
const parsedTo = this.normlizePhoneNumber(to);
|
||||
const encodedMessage = encodeURIComponent(message);
|
||||
const encodeTo = encodeURIComponent(parsedTo);
|
||||
|
||||
const params = `action=send-sms&api_key=${API_KEY}&to=${encodeTo}&sms=${encodedMessage}&unicode=1`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
axios
|
||||
.get(`https://easysms.devs.ly/sms/api?${params}`)
|
||||
.then((response) => {
|
||||
if (response.data.code === 'ok') {
|
||||
resolve(response);
|
||||
} else {
|
||||
reject(response.data);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
39
packages/server/src/services/SMSClient/SMSAPI.ts
Normal file
39
packages/server/src/services/SMSClient/SMSAPI.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Container } from 'typedi';
|
||||
import SMSClientInterface from '@/services/SMSClient/SMSClientInterface';
|
||||
import { thomsonCrossSectionDependencies } from 'mathjs';
|
||||
|
||||
export default class SMSAPI {
|
||||
smsClient: SMSClientInterface;
|
||||
|
||||
constructor(smsClient: SMSClientInterface) {
|
||||
this.smsClient = smsClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the message to the target via the client.
|
||||
* @param {string} to
|
||||
* @param {string} message
|
||||
* @param {array} extraParams
|
||||
* @param {array} extraHeaders
|
||||
*/
|
||||
sendMessage(
|
||||
to: string,
|
||||
message: string,
|
||||
extraParams?: [],
|
||||
extraHeaders?: []
|
||||
) {
|
||||
return this.smsClient.send(to, message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param to
|
||||
* @param message
|
||||
* @returns
|
||||
*/
|
||||
sendMessageJob(to: string, message: string) {
|
||||
const agenda = Container.get('agenda');
|
||||
|
||||
return agenda.now('sms-notification', { to, message });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
export default interface SMSClientInterface {
|
||||
clientName: string;
|
||||
send(to: string, message: string): boolean;
|
||||
}
|
||||
3
packages/server/src/services/SMSClient/index.ts
Normal file
3
packages/server/src/services/SMSClient/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import SMSAPI from './SMSAPI';
|
||||
|
||||
export default SMSAPI;
|
||||
Reference in New Issue
Block a user