103 lines
3.5 KiB
JavaScript
103 lines
3.5 KiB
JavaScript
// Исправленный код для создания профиля
|
||
require('dotenv').config();
|
||
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: process.env.DB_HOST || 'localhost',
|
||
port: parseInt(process.env.DB_PORT) || 5432,
|
||
user: process.env.DB_USERNAME || 'postgres',
|
||
password: process.env.DB_PASSWORD,
|
||
database: process.env.DB_NAME || '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);
|
||
});
|