54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
import { pool } from "./db.js";
|
|
|
|
export const UserRepository = {
|
|
|
|
async createOrUpdateUser({ telegramId, username, chatId }) {
|
|
const query = `
|
|
INSERT INTO users (telegram_id, telegram_username, chat_id, last_msg_context)
|
|
VALUES ($1, $2, $3, '')
|
|
ON CONFLICT (telegram_id)
|
|
DO UPDATE SET
|
|
telegram_username = EXCLUDED.telegram_username,
|
|
chat_id = EXCLUDED.chat_id,
|
|
last_msg_context = '',
|
|
updated_at = now()
|
|
RETURNING *;
|
|
`;
|
|
|
|
const values = [telegramId, username, chatId];
|
|
|
|
const { rows } = await pool.query(query, values);
|
|
return rows[0];
|
|
},
|
|
|
|
async getByTelegramId(telegramId) {
|
|
const { rows } = await pool.query(
|
|
`SELECT * FROM users WHERE telegram_id = $1`,
|
|
[telegramId]
|
|
);
|
|
return rows[0];
|
|
},
|
|
|
|
async updateContext(telegramId, context) {
|
|
await pool.query(
|
|
`UPDATE users SET last_msg_context = $1 WHERE telegram_id = $2`,
|
|
[context, telegramId]
|
|
);
|
|
},
|
|
|
|
async getContext(telegramId) {
|
|
const { rows } = await pool.query(
|
|
`SELECT last_msg_context FROM users WHERE telegram_id = $1`,
|
|
[telegramId]
|
|
);
|
|
return rows[0]?.last_msg_context;
|
|
},
|
|
|
|
async deleteUser(telegramId) {
|
|
await pool.query(
|
|
`DELETE FROM users WHERE telegram_id = $1`,
|
|
[telegramId]
|
|
);
|
|
}
|
|
};
|