update UserService
This commit is contained in:
parent
bfb9a55e36
commit
b224b3f331
@ -2,6 +2,16 @@ import db from "../config/database.js";
|
|||||||
import Wallet from "../models/Wallet.js";
|
import Wallet from "../models/Wallet.js";
|
||||||
|
|
||||||
class UserService {
|
class UserService {
|
||||||
|
// Функция для нормализации telegram_id
|
||||||
|
static normalizeTelegramId(telegramId) {
|
||||||
|
if (typeof telegramId === 'number') {
|
||||||
|
// Если это число, преобразуем его в строку и удаляем ".0"
|
||||||
|
return telegramId.toString().replace(/\.0$/, '');
|
||||||
|
}
|
||||||
|
// Если это уже строка, возвращаем как есть
|
||||||
|
return telegramId.toString();
|
||||||
|
}
|
||||||
|
|
||||||
static async getUserByUserId(userId) {
|
static async getUserByUserId(userId) {
|
||||||
try {
|
try {
|
||||||
return await db.getAsync(
|
return await db.getAsync(
|
||||||
@ -16,9 +26,10 @@ class UserService {
|
|||||||
|
|
||||||
static async getUserByTelegramId(telegramId) {
|
static async getUserByTelegramId(telegramId) {
|
||||||
try {
|
try {
|
||||||
|
const normalizedTelegramId = this.normalizeTelegramId(telegramId);
|
||||||
return await db.getAsync(
|
return await db.getAsync(
|
||||||
'SELECT * FROM users WHERE telegram_id = ?',
|
'SELECT * FROM users WHERE telegram_id = ?',
|
||||||
[String(telegramId)]
|
[normalizedTelegramId]
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting user:', error);
|
console.error('Error getting user:', error);
|
||||||
@ -28,6 +39,7 @@ class UserService {
|
|||||||
|
|
||||||
static async getDetailedUserByTelegramId(telegramId) {
|
static async getDetailedUserByTelegramId(telegramId) {
|
||||||
try {
|
try {
|
||||||
|
const normalizedTelegramId = this.normalizeTelegramId(telegramId);
|
||||||
return await db.getAsync(`
|
return await db.getAsync(`
|
||||||
SELECT
|
SELECT
|
||||||
u.*,
|
u.*,
|
||||||
@ -41,7 +53,7 @@ class UserService {
|
|||||||
LEFT JOIN crypto_wallets cw2 ON u.id = cw2.user_id AND cw2.wallet_type LIKE '%#_%' ESCAPE '#'
|
LEFT JOIN crypto_wallets cw2 ON u.id = cw2.user_id AND cw2.wallet_type LIKE '%#_%' ESCAPE '#'
|
||||||
WHERE u.telegram_id = ?
|
WHERE u.telegram_id = ?
|
||||||
GROUP BY u.id
|
GROUP BY u.id
|
||||||
`, [telegramId.toString()]);
|
`, [normalizedTelegramId]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting user stats:', error);
|
console.error('Error getting user stats:', error);
|
||||||
throw error;
|
throw error;
|
||||||
@ -50,7 +62,8 @@ class UserService {
|
|||||||
|
|
||||||
static async createUser(userData) {
|
static async createUser(userData) {
|
||||||
try {
|
try {
|
||||||
const existingUser = await this.getUserByTelegramId(userData?.telegram_id);
|
const normalizedTelegramId = this.normalizeTelegramId(userData?.telegram_id);
|
||||||
|
const existingUser = await this.getUserByTelegramId(normalizedTelegramId);
|
||||||
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
return existingUser.id;
|
return existingUser.id;
|
||||||
@ -81,7 +94,8 @@ class UserService {
|
|||||||
static async deleteUser() {}
|
static async deleteUser() {}
|
||||||
|
|
||||||
static async recalculateUserBalanceByTelegramId(telegramId) {
|
static async recalculateUserBalanceByTelegramId(telegramId) {
|
||||||
const user = await this.getUserByTelegramId(telegramId);
|
const normalizedTelegramId = this.normalizeTelegramId(telegramId);
|
||||||
|
const user = await this.getUserByTelegramId(normalizedTelegramId);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return;
|
return;
|
||||||
@ -101,23 +115,20 @@ class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async updateUserLocation(telegramId, country, city, district) {
|
static async updateUserLocation(telegramId, country, city, district) {
|
||||||
|
const normalizedTelegramId = this.normalizeTelegramId(telegramId);
|
||||||
await db.runAsync(
|
await db.runAsync(
|
||||||
'UPDATE users SET country = ?, city = ?, district = ? WHERE telegram_id = ?',
|
'UPDATE users SET country = ?, city = ?, district = ? WHERE telegram_id = ?',
|
||||||
[country, city, district, telegramId.toString()]
|
[country, city, district, normalizedTelegramId]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async updateUserStatus(telegramId, status) {
|
static async updateUserStatus(telegramId, status) {
|
||||||
// statuses
|
const normalizedTelegramId = this.normalizeTelegramId(telegramId);
|
||||||
// 0 - active
|
|
||||||
// 1 - deleted
|
|
||||||
// 2 - blocked
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.runAsync('BEGIN TRANSACTION');
|
await db.runAsync('BEGIN TRANSACTION');
|
||||||
|
|
||||||
// Update user status
|
// Update user status
|
||||||
await db.runAsync('UPDATE users SET status = ? WHERE telegram_id = ?', [status, telegramId.toString()]);
|
await db.runAsync('UPDATE users SET status = ? WHERE telegram_id = ?', [status, normalizedTelegramId]);
|
||||||
|
|
||||||
// Commit transaction
|
// Commit transaction
|
||||||
await db.runAsync('COMMIT');
|
await db.runAsync('COMMIT');
|
||||||
|
Loading…
Reference in New Issue
Block a user