-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstateManager.js
More file actions
104 lines (94 loc) · 4.73 KB
/
Copy pathstateManager.js
File metadata and controls
104 lines (94 loc) · 4.73 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// stateManager.js
//
// Projects are persisted in Postgres and mirrored in an in-memory cache for
// synchronous reads. Mutations must call `updateProject()` to persist.
//
// User states (the conversational step a user is on) are in-memory only —
// losing them on restart just means the user re-enters whatever multi-step
// flow they were in the middle of. Acceptable trade-off for simplicity.
const db = require('./db.js');
// ──────────────────────────────────────────────────────────────────────────
// In-memory caches
// ──────────────────────────────────────────────────────────────────────────
let projectsCache = new Map();
const userStates = new Map();
let initialized = false;
// ──────────────────────────────────────────────────────────────────────────
// Initialization — call once at startup
// ──────────────────────────────────────────────────────────────────────────
async function init() {
// Ensure schema exists BEFORE we try to query it (handles fresh databases).
await db.createTables();
projectsCache = await db.loadAllProjects();
initialized = true;
console.log(`📦 Loaded ${projectsCache.size} project(s) from database`);
}
function assertInitialized() {
if (!initialized) {
throw new Error('stateManager.init() must be called before use');
}
}
// ──────────────────────────────────────────────────────────────────────────
// Project API
// ──────────────────────────────────────────────────────────────────────────
async function createProject(projectName, adminChatId) {
assertInitialized();
if (projectsCache.has(projectName)) {
throw new Error(`Project "${projectName}" already exists.`);
}
const project = {
projectName,
adminChatId,
adminPassword: null,
websites: [],
};
await db.upsertProject(projectName, project);
projectsCache.set(projectName, project);
return project;
}
function getProject(projectName) {
return projectsCache.get(projectName);
}
async function updateProject(projectName, updatedProject) {
// Ensure the project carries its own name so callers can save-back without
// tracking the key separately.
updatedProject.projectName = updatedProject.projectName || projectName;
await db.upsertProject(projectName, updatedProject);
projectsCache.set(projectName, updatedProject);
}
async function deleteProject(projectName) {
await db.deleteProjectRow(projectName);
projectsCache.delete(projectName);
}
// ──────────────────────────────────────────────────────────────────────────
// User state API (in-memory only, no async needed)
// ──────────────────────────────────────────────────────────────────────────
function setUserState(chatId, state) {
userStates.set(chatId, state);
}
function getUserState(chatId) {
return userStates.get(chatId);
}
function deleteUserState(chatId) {
userStates.delete(chatId);
}
// ──────────────────────────────────────────────────────────────────────────
// Module exports
// ──────────────────────────────────────────────────────────────────────────
module.exports = {
init,
// project methods
createProject,
getProject,
updateProject,
deleteProject,
// user state methods
setUserState,
getUserState,
deleteUserState,
// direct cache access (for legacy callers that iterate / check existence)
get projectsByName() {
return projectsCache;
},
userStates,
};