forked from BigMichi1/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserManager.ts
More file actions
70 lines (58 loc) · 1.98 KB
/
userManager.ts
File metadata and controls
70 lines (58 loc) · 1.98 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { db } from './db';
interface UserCredentials {
discordId: string;
userId: string;
userHash: string;
server?: string;
instanceId?: string;
}
interface StoredUser {
discord_id: string;
user_id: string;
user_hash: string;
server: string | null;
instance_id: string | null;
created_at: string;
updated_at: string;
}
class UserManager {
async saveCredentials(credentials: UserCredentials): Promise<void> {
const { discordId, userId, userHash, server, instanceId } = credentials;
await db.run(
`INSERT OR REPLACE INTO users (discord_id, user_id, user_hash, server, instance_id, updated_at)
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
[discordId, userId, userHash, server || null, instanceId || null]
);
}
async getCredentials(discordId: string): Promise<UserCredentials | null> {
const user = await db.get<StoredUser>('SELECT * FROM users WHERE discord_id = ?', [discordId]);
if (!user) return null;
return {
discordId: user.discord_id,
userId: user.user_id,
userHash: user.user_hash,
server: user.server || undefined,
instanceId: user.instance_id || undefined,
};
}
async deleteCredentials(discordId: string): Promise<void> {
await db.run('DELETE FROM users WHERE discord_id = ?', [discordId]);
}
async hasCredentials(discordId: string): Promise<boolean> {
const user = await db.get('SELECT discord_id FROM users WHERE discord_id = ?', [discordId]);
return user !== undefined;
}
async updateServer(discordId: string, server: string): Promise<void> {
await db.run(
'UPDATE users SET server = ?, updated_at = CURRENT_TIMESTAMP WHERE discord_id = ?',
[server, discordId]
);
}
async updateInstanceId(discordId: string, instanceId: string): Promise<void> {
await db.run(
'UPDATE users SET instance_id = ?, updated_at = CURRENT_TIMESTAMP WHERE discord_id = ?',
[instanceId, discordId]
);
}
}
export const userManager = new UserManager();