mass refactor
This commit is contained in:
101
scripts/create_profile_fix.js
Normal file
101
scripts/create_profile_fix.js
Normal file
@@ -0,0 +1,101 @@
|
||||
// Исправленный код для создания профиля
|
||||
const { Client } = require('pg');
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
// Получаем аргументы из командной строки
|
||||
const args = process.argv.slice(2);
|
||||
const telegramId = args[0];
|
||||
const name = args[1];
|
||||
const age = parseInt(args[2]);
|
||||
const gender = args[3];
|
||||
const city = args[4];
|
||||
const bio = args[5];
|
||||
const photoFileId = args[6];
|
||||
|
||||
// Проверяем, что все необходимые аргументы предоставлены
|
||||
if (!telegramId || !name || !age || !gender || !city || !bio || !photoFileId) {
|
||||
console.error('Необходимо указать все параметры: telegramId, name, age, gender, city, bio, photoFileId');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Устанавливаем соединение с базой данных
|
||||
const client = new Client({
|
||||
host: '192.168.0.102',
|
||||
port: 5432,
|
||||
user: 'trevor',
|
||||
password: 'Cl0ud_1985!',
|
||||
database: 'telegram_tinder_bot'
|
||||
});
|
||||
|
||||
async function createProfile() {
|
||||
try {
|
||||
await client.connect();
|
||||
|
||||
// Шаг 1: Создаем или обновляем пользователя
|
||||
const userResult = await client.query(`
|
||||
INSERT INTO users (telegram_id, username, first_name, last_name)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (telegram_id) DO UPDATE SET
|
||||
username = EXCLUDED.username,
|
||||
first_name = EXCLUDED.first_name,
|
||||
last_name = EXCLUDED.last_name
|
||||
RETURNING id
|
||||
`, [parseInt(telegramId), null, name, null]);
|
||||
|
||||
const userId = userResult.rows[0].id;
|
||||
|
||||
// Шаг 2: Создаем профиль
|
||||
const profileId = uuidv4();
|
||||
const now = new Date();
|
||||
const interestedIn = gender === 'male' ? 'female' : 'male';
|
||||
|
||||
const columns = [
|
||||
'id', 'user_id', 'name', 'age', 'gender', 'interested_in',
|
||||
'bio', 'city', 'photos', 'is_verified',
|
||||
'is_visible', 'created_at', 'updated_at'
|
||||
].join(', ');
|
||||
|
||||
const values = [
|
||||
profileId, userId, name, age, gender, interestedIn,
|
||||
bio, city, JSON.stringify([photoFileId]),
|
||||
false, true, now, now
|
||||
];
|
||||
|
||||
const placeholders = values.map((_, i) => `$${i + 1}`).join(', ');
|
||||
|
||||
await client.query(`
|
||||
INSERT INTO profiles (${columns})
|
||||
VALUES (${placeholders})
|
||||
`, values);
|
||||
|
||||
console.log('Профиль успешно создан!');
|
||||
|
||||
// Возвращаем информацию о созданном профиле
|
||||
return {
|
||||
userId,
|
||||
profileId,
|
||||
name,
|
||||
age,
|
||||
gender,
|
||||
interestedIn,
|
||||
bio,
|
||||
city,
|
||||
photos: [photoFileId]
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Ошибка при создании профиля:', error);
|
||||
throw error;
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
}
|
||||
|
||||
createProfile()
|
||||
.then(profile => {
|
||||
console.log('Созданный профиль:', profile);
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Создание профиля не удалось:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user