new user registration function
This commit is contained in:
@@ -12,6 +12,56 @@ class UserService {
|
|||||||
return telegramId.toString();
|
return telegramId.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Функция для валидации telegram_id
|
||||||
|
static validateTelegramId(telegramId) {
|
||||||
|
if (typeof telegramId !== 'string') {
|
||||||
|
throw new Error('telegram_id должен быть строкой');
|
||||||
|
}
|
||||||
|
if (telegramId.includes('.0')) {
|
||||||
|
throw new Error('telegram_id не должен содержать ".0"');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async createUser(userData) {
|
||||||
|
try {
|
||||||
|
// Нормализуем и валидируем telegram_id
|
||||||
|
const normalizedTelegramId = this.normalizeTelegramId(userData?.telegram_id);
|
||||||
|
// console.log("Normalized telegram_id:", normalizedTelegramId); // Отладочный вывод
|
||||||
|
this.validateTelegramId(normalizedTelegramId);
|
||||||
|
|
||||||
|
// Обновляем значение telegram_id в объекте userData
|
||||||
|
userData.telegram_id = normalizedTelegramId;
|
||||||
|
|
||||||
|
// Проверяем, существует ли пользователь с таким telegram_id
|
||||||
|
const existingUser = await this.getUserByTelegramId(normalizedTelegramId);
|
||||||
|
if (existingUser) {
|
||||||
|
console.log("User already exists with telegram_id:", normalizedTelegramId);
|
||||||
|
return existingUser.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Подготавливаем данные для вставки в базу данных
|
||||||
|
const fields = Object.keys(userData);
|
||||||
|
const values = Object.values(userData);
|
||||||
|
const marks = Array(fields.length).fill('?');
|
||||||
|
|
||||||
|
const query = `
|
||||||
|
INSERT INTO users (${fields.join(', ')})
|
||||||
|
VALUES (${marks.join(', ')})
|
||||||
|
`;
|
||||||
|
|
||||||
|
// Выполняем запрос к базе данных
|
||||||
|
await db.runAsync('BEGIN TRANSACTION');
|
||||||
|
const result = await db.runAsync(query, values);
|
||||||
|
await db.runAsync('COMMIT');
|
||||||
|
|
||||||
|
return result.lastID;
|
||||||
|
} catch (error) {
|
||||||
|
await db.runAsync('ROLLBACK');
|
||||||
|
console.error('Error creating user:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static async getUserByUserId(userId) {
|
static async getUserByUserId(userId) {
|
||||||
try {
|
try {
|
||||||
return await db.getAsync(
|
return await db.getAsync(
|
||||||
@@ -60,35 +110,6 @@ class UserService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async createUser(userData) {
|
|
||||||
try {
|
|
||||||
const normalizedTelegramId = this.normalizeTelegramId(userData?.telegram_id);
|
|
||||||
const existingUser = await this.getUserByTelegramId(normalizedTelegramId);
|
|
||||||
|
|
||||||
if (existingUser) {
|
|
||||||
return existingUser.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
const fields = Object.keys(userData);
|
|
||||||
const marks = Array(fields.length).fill('?');
|
|
||||||
|
|
||||||
const query = `
|
|
||||||
INSERT INTO users (${fields.join(', ')})
|
|
||||||
VALUES (${marks.join(', ')})
|
|
||||||
`;
|
|
||||||
|
|
||||||
await db.runAsync('BEGIN TRANSACTION');
|
|
||||||
const result = await db.runAsync(query, Object.values(userData));
|
|
||||||
await db.runAsync('COMMIT');
|
|
||||||
|
|
||||||
return result.lastID;
|
|
||||||
} catch (error) {
|
|
||||||
await db.runAsync('ROLLBACK');
|
|
||||||
console.error('Error creating user:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static async updateUser(userId, newUserData) {}
|
static async updateUser(userId, newUserData) {}
|
||||||
|
|
||||||
static async deleteUser() {}
|
static async deleteUser() {}
|
||||||
|
|||||||
Reference in New Issue
Block a user