update UserService

This commit is contained in:
NW 2024-12-18 16:16:41 +00:00
parent bfb9a55e36
commit b224b3f331

View File

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