-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
2,210 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
enum SmsStatus { | ||
Success = 'Success', | ||
Error = 'Error', | ||
LowBalance = 'Low Balance', | ||
} | ||
|
||
export default SmsStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { IsBillingEnabled } from '../Config'; | ||
import ObjectID from 'Common/Types/ObjectID'; | ||
import Project from 'Model/Models/Project'; | ||
import ProjectService from './ProjectService'; | ||
import BillingService from './BillingService'; | ||
import logger from '../Utils/Logger'; | ||
import BadDataException from 'Common/Types/Exception/BadDataException'; | ||
|
||
export default class NotificationService { | ||
public static async rechargeIfBalanceIsLow( | ||
projectId: ObjectID, | ||
options?: { | ||
autoRechargeSmsOrCallByBalanceInUSD: number; | ||
autoRechargeSmsOrCallWhenCurrentBalanceFallsInUSD: number; | ||
enableAutoRechargeSmsOrCallBalance: boolean; | ||
} | ||
): Promise<number> { | ||
let project: Project | null = null; | ||
if (projectId && IsBillingEnabled) { | ||
// check payment methods. | ||
|
||
project = await ProjectService.findOneById({ | ||
id: projectId, | ||
select: { | ||
smsOrCallCurrentBalanceInUSDCents: true, | ||
enableAutoRechargeSmsOrCallBalance: true, | ||
enableSmsNotifications: true, | ||
autoRechargeSmsOrCallByBalanceInUSD: true, | ||
autoRechargeSmsOrCallWhenCurrentBalanceFallsInUSD: true, | ||
paymentProviderCustomerId: true, | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
|
||
const autoRechargeSmsOrCallWhenCurrentBalanceFallsInUSD: number = | ||
options?.autoRechargeSmsOrCallWhenCurrentBalanceFallsInUSD || | ||
project?.autoRechargeSmsOrCallWhenCurrentBalanceFallsInUSD || | ||
0; | ||
const autoRechargeSmsOrCallByBalanceInUSD: number = | ||
options?.autoRechargeSmsOrCallByBalanceInUSD || | ||
project?.autoRechargeSmsOrCallByBalanceInUSD || | ||
0; | ||
|
||
const enableAutoRechargeSmsOrCallBalance: boolean = options | ||
? options.enableAutoRechargeSmsOrCallBalance | ||
: project?.enableAutoRechargeSmsOrCallBalance || false; | ||
|
||
if (!project) { | ||
return 0; | ||
} | ||
|
||
if ( | ||
!(await BillingService.hasPaymentMethods( | ||
project.paymentProviderCustomerId! | ||
)) | ||
) { | ||
throw new BadDataException( | ||
'No payment methods found for the project. Please add a payment method in project settings to continue.' | ||
); | ||
} | ||
|
||
if ( | ||
enableAutoRechargeSmsOrCallBalance && | ||
autoRechargeSmsOrCallByBalanceInUSD && | ||
autoRechargeSmsOrCallWhenCurrentBalanceFallsInUSD | ||
) { | ||
if ( | ||
(project.smsOrCallCurrentBalanceInUSDCents || 0) / 100 < | ||
autoRechargeSmsOrCallWhenCurrentBalanceFallsInUSD | ||
) { | ||
try { | ||
// recharge balance | ||
const updatedAmount: number = Math.floor( | ||
(project.smsOrCallCurrentBalanceInUSDCents || 0) + | ||
autoRechargeSmsOrCallByBalanceInUSD * 100 | ||
); | ||
|
||
// If the recharge is succcessful, then update the project balance. | ||
await BillingService.genrateInvoiceAndChargeCustomer( | ||
project.paymentProviderCustomerId!, | ||
'SMS or Call Balance Recharge', | ||
autoRechargeSmsOrCallByBalanceInUSD | ||
); | ||
|
||
await ProjectService.updateOneById({ | ||
data: { | ||
smsOrCallCurrentBalanceInUSDCents: | ||
updatedAmount, | ||
}, | ||
id: project.id!, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
|
||
project.smsOrCallCurrentBalanceInUSDCents = | ||
updatedAmount; | ||
|
||
// TODO: Send an email on successful recharge. | ||
} catch (err) { | ||
// TODO: if the recharge fails, then send email to the user. | ||
logger.error(err); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return project?.smsOrCallCurrentBalanceInUSDCents || 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import PostgresDatabase from '../Infrastructure/PostgresDatabase'; | ||
import Model from 'Model/Models/SmsLog'; | ||
import DatabaseService from './DatabaseService'; | ||
|
||
export class Service extends DatabaseService<Model> { | ||
public constructor(postgresDatabase?: PostgresDatabase) { | ||
super(Model, postgresDatabase); | ||
this.hardDeleteItemsOlderThanInDays('createdAt', 30); | ||
} | ||
} | ||
|
||
export default new Service(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import EmptyResponseData from 'Common/Types/API/EmptyResponse'; | ||
import HTTPResponse from 'Common/Types/API/HTTPResponse'; | ||
import Route from 'Common/Types/API/Route'; | ||
import URL from 'Common/Types/API/URL'; | ||
import { JSONObject } from 'Common/Types/JSON'; | ||
import API from 'Common/Utils/API'; | ||
import { NotificationHostname } from '../Config'; | ||
import Protocol from 'Common/Types/API/Protocol'; | ||
import ClusterKeyAuthorization from '../Middleware/ClusterKeyAuthorization'; | ||
import Phone from 'Common/Types/Phone'; | ||
import ObjectID from 'Common/Types/ObjectID'; | ||
|
||
export default class SmsService { | ||
public static async sendSms( | ||
to: Phone, | ||
message: string, | ||
options: { | ||
projectId?: ObjectID | undefined; // project id for sms log | ||
from?: Phone; // from phone number | ||
} | ||
): Promise<HTTPResponse<EmptyResponseData>> { | ||
const body: JSONObject = { | ||
to: to.toString(), | ||
message, | ||
from: options.from?.toString(), | ||
projectId: options.projectId?.toString(), | ||
}; | ||
|
||
return await API.post<EmptyResponseData>( | ||
new URL( | ||
Protocol.HTTP, | ||
NotificationHostname, | ||
new Route('/sms/send') | ||
), | ||
body, | ||
{ | ||
...ClusterKeyAuthorization.getClusterKeyHeaders(), | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.