Mohammed Foud commited on
Commit ·
de0a64f
1
Parent(s): 54c580b
all
Browse files- .gitignore +0 -1
- src/bots/handlers/purchaseHandlers.ts +18 -7
.gitignore
CHANGED
|
@@ -37,4 +37,3 @@ coverage
|
|
| 37 |
trash
|
| 38 |
logs
|
| 39 |
.env
|
| 40 |
-
.git2222
|
|
|
|
| 37 |
trash
|
| 38 |
logs
|
| 39 |
.env
|
|
|
src/bots/handlers/purchaseHandlers.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
| 16 |
} from "../utils/messageUtils";
|
| 17 |
import { getLoggedInMenuKeyboard, getMainMenuKeyboard } from "../utils/keyboardUtils";
|
| 18 |
import { messageManager } from "../utils/messageManager";
|
|
|
|
| 19 |
|
| 20 |
const logger = createLogger('PurchaseHandlers');
|
| 21 |
const authService = AuthService.getInstance();
|
|
@@ -62,6 +63,7 @@ export const executePurchase = async (
|
|
| 62 |
return;
|
| 63 |
}
|
| 64 |
|
|
|
|
| 65 |
try {
|
| 66 |
const fiveSimService = serviceMapping[service];
|
| 67 |
const prices = await virtualNumberService.getPrices(fiveSimService, countryId);
|
|
@@ -78,12 +80,15 @@ export const executePurchase = async (
|
|
| 78 |
|
| 79 |
const cost = prices[countryId][fiveSimService][operator].cost;
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
// 💰 Balance check
|
| 82 |
const user = await authService.getUserByTelegramId(telegramId);
|
| 83 |
-
if (!user || user.balance <
|
| 84 |
await ctx.deleteMessage(loadingMsgId);
|
| 85 |
await ctx.reply(
|
| 86 |
-
getInsufficientBalanceMessage(
|
| 87 |
getLoggedInMenuKeyboard()
|
| 88 |
);
|
| 89 |
return;
|
|
@@ -95,7 +100,7 @@ export const executePurchase = async (
|
|
| 95 |
|
| 96 |
if (purchaseResult && purchaseResult.phone) {
|
| 97 |
// ➖ Deduct balance
|
| 98 |
-
await authService.updateUserBalance(telegramId, user.balance -
|
| 99 |
|
| 100 |
// 💾 Create purchase record in database with PENDING state
|
| 101 |
const purchaseRecord = await purchaseTrackingService.createPurchase({
|
|
@@ -106,13 +111,13 @@ export const executePurchase = async (
|
|
| 106 |
operator: operator,
|
| 107 |
phoneNumber: purchaseResult.phone,
|
| 108 |
orderId: purchaseResult.id,
|
| 109 |
-
cost:
|
| 110 |
state: PurchaseState.PENDING
|
| 111 |
});
|
| 112 |
|
| 113 |
// Send purchase confirmation
|
| 114 |
const confirmMsg = await ctx.reply(
|
| 115 |
-
getPurchaseSuccessMessage(purchaseResult, service, countryId, operator,
|
| 116 |
getLoggedInMenuKeyboard()
|
| 117 |
);
|
| 118 |
|
|
@@ -135,6 +140,9 @@ export const executePurchase = async (
|
|
| 135 |
PurchaseState.TIMEOUT
|
| 136 |
);
|
| 137 |
|
|
|
|
|
|
|
|
|
|
| 138 |
await ctx.reply(
|
| 139 |
getVerificationTimeoutMessage(),
|
| 140 |
getLoggedInMenuKeyboard()
|
|
@@ -197,7 +205,7 @@ export const executePurchase = async (
|
|
| 197 |
service: service,
|
| 198 |
countryId: countryId,
|
| 199 |
operator: operator,
|
| 200 |
-
cost:
|
| 201 |
state: PurchaseState.FAILED
|
| 202 |
});
|
| 203 |
}
|
|
@@ -212,13 +220,16 @@ export const executePurchase = async (
|
|
| 212 |
try {
|
| 213 |
const user = await authService.getUserByTelegramId(telegramId!);
|
| 214 |
if (user) {
|
|
|
|
|
|
|
|
|
|
| 215 |
await purchaseTrackingService.createPurchase({
|
| 216 |
userId: user.id,
|
| 217 |
telegramId: telegramId!,
|
| 218 |
service: service,
|
| 219 |
countryId: countryId,
|
| 220 |
operator: operator,
|
| 221 |
-
cost:
|
| 222 |
state: PurchaseState.FAILED
|
| 223 |
});
|
| 224 |
}
|
|
|
|
| 16 |
} from "../utils/messageUtils";
|
| 17 |
import { getLoggedInMenuKeyboard, getMainMenuKeyboard } from "../utils/keyboardUtils";
|
| 18 |
import { messageManager } from "../utils/messageManager";
|
| 19 |
+
import { calculatePriceWithProfit } from "../utils/priceUtils";
|
| 20 |
|
| 21 |
const logger = createLogger('PurchaseHandlers');
|
| 22 |
const authService = AuthService.getInstance();
|
|
|
|
| 63 |
return;
|
| 64 |
}
|
| 65 |
|
| 66 |
+
let costWithProfit = 0;
|
| 67 |
try {
|
| 68 |
const fiveSimService = serviceMapping[service];
|
| 69 |
const prices = await virtualNumberService.getPrices(fiveSimService, countryId);
|
|
|
|
| 80 |
|
| 81 |
const cost = prices[countryId][fiveSimService][operator].cost;
|
| 82 |
|
| 83 |
+
// Apply profit to the cost
|
| 84 |
+
costWithProfit = calculatePriceWithProfit(ctx, cost, 'RUB');
|
| 85 |
+
|
| 86 |
// 💰 Balance check
|
| 87 |
const user = await authService.getUserByTelegramId(telegramId);
|
| 88 |
+
if (!user || user.balance < costWithProfit) {
|
| 89 |
await ctx.deleteMessage(loadingMsgId);
|
| 90 |
await ctx.reply(
|
| 91 |
+
getInsufficientBalanceMessage(costWithProfit, user?.balance || 0),
|
| 92 |
getLoggedInMenuKeyboard()
|
| 93 |
);
|
| 94 |
return;
|
|
|
|
| 100 |
|
| 101 |
if (purchaseResult && purchaseResult.phone) {
|
| 102 |
// ➖ Deduct balance
|
| 103 |
+
await authService.updateUserBalance(telegramId, user.balance - costWithProfit);
|
| 104 |
|
| 105 |
// 💾 Create purchase record in database with PENDING state
|
| 106 |
const purchaseRecord = await purchaseTrackingService.createPurchase({
|
|
|
|
| 111 |
operator: operator,
|
| 112 |
phoneNumber: purchaseResult.phone,
|
| 113 |
orderId: purchaseResult.id,
|
| 114 |
+
cost: costWithProfit,
|
| 115 |
state: PurchaseState.PENDING
|
| 116 |
});
|
| 117 |
|
| 118 |
// Send purchase confirmation
|
| 119 |
const confirmMsg = await ctx.reply(
|
| 120 |
+
getPurchaseSuccessMessage(purchaseResult, service, countryId, operator, costWithProfit),
|
| 121 |
getLoggedInMenuKeyboard()
|
| 122 |
);
|
| 123 |
|
|
|
|
| 140 |
PurchaseState.TIMEOUT
|
| 141 |
);
|
| 142 |
|
| 143 |
+
// Return the cost to user's balance on timeout
|
| 144 |
+
await authService.updateUserBalance(telegramId, user.balance + costWithProfit);
|
| 145 |
+
|
| 146 |
await ctx.reply(
|
| 147 |
getVerificationTimeoutMessage(),
|
| 148 |
getLoggedInMenuKeyboard()
|
|
|
|
| 205 |
service: service,
|
| 206 |
countryId: countryId,
|
| 207 |
operator: operator,
|
| 208 |
+
cost: costWithProfit,
|
| 209 |
state: PurchaseState.FAILED
|
| 210 |
});
|
| 211 |
}
|
|
|
|
| 220 |
try {
|
| 221 |
const user = await authService.getUserByTelegramId(telegramId!);
|
| 222 |
if (user) {
|
| 223 |
+
// Return the cost to user's balance on error
|
| 224 |
+
await authService.updateUserBalance(telegramId!, user.balance + costWithProfit);
|
| 225 |
+
|
| 226 |
await purchaseTrackingService.createPurchase({
|
| 227 |
userId: user.id,
|
| 228 |
telegramId: telegramId!,
|
| 229 |
service: service,
|
| 230 |
countryId: countryId,
|
| 231 |
operator: operator,
|
| 232 |
+
cost: costWithProfit,
|
| 233 |
state: PurchaseState.FAILED
|
| 234 |
});
|
| 235 |
}
|