update proj

This commit is contained in:
2026-01-26 17:53:36 +04:00
parent ce8eb49832
commit 8e34ec72c6
5 changed files with 138 additions and 11 deletions

53
user.repository.js Normal file
View File

@@ -0,0 +1,53 @@
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]
);
}
};