-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathusers.ts
More file actions
46 lines (41 loc) · 1.4 KB
/
users.ts
File metadata and controls
46 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* User helpers for the users table. Shared by API routes and bot.
* Import from ../database/users.js (e.g. from api/, telegram/, bot/).
*/
import { sql } from './start.js';
export function normalizeUsername(raw: unknown): string {
if (typeof raw !== 'string') return '';
let s = raw.trim();
if (s.startsWith('@')) s = s.slice(1);
return s.toLowerCase();
}
export async function upsertUserFromTma(opts: {
telegramUsername: string;
locale: string | null;
}): Promise<void> {
const { telegramUsername, locale } = opts;
if (!telegramUsername) return;
await sql`
INSERT INTO users (telegram_username, locale, created_at, updated_at, last_tma_seen_at)
VALUES (${telegramUsername}, ${locale}, NOW(), NOW(), NOW())
ON CONFLICT (telegram_username) DO UPDATE
SET locale = EXCLUDED.locale,
last_tma_seen_at = NOW(),
updated_at = NOW();
`;
}
export async function upsertUserFromBot(opts: {
telegramUsername: string;
locale: string | null;
}): Promise<void> {
const { telegramUsername, locale } = opts;
if (!telegramUsername) return;
await sql`
INSERT INTO users (telegram_username, locale, created_at, updated_at, last_login_at)
VALUES (${telegramUsername}, ${locale}, NOW(), NOW(), NOW())
ON CONFLICT (telegram_username) DO UPDATE
SET locale = EXCLUDED.locale,
last_login_at = NOW(),
updated_at = NOW();
`;
}