refactor: migrate database from SQLite to PostgreSQL

This commit is contained in:
AI Bot
2026-08-12 17:50:50 +05:30
parent 381a359cb2
commit ee891fdd9b
4 changed files with 213 additions and 1087 deletions
+5 -5
View File
@@ -64,15 +64,15 @@ app.post('/api/auth/register', async (req, res) => {
const id = uuidv4();
// First user becomes admin and is automatically active
const countRow = await get('SELECT COUNT(*) as count FROM profiles');
const role = countRow.count === 0 ? 'admin' : 'user';
const is_active = countRow.count === 0 ? 1 : 0;
const role = parseInt(countRow.count) === 0 ? 'admin' : 'user';
const is_active = parseInt(countRow.count) === 0 ? true : false;
await run(
'INSERT INTO profiles (id, email, password_hash, role, api_credits, storage_limit_mb, is_active) VALUES (?, ?, ?, ?, ?, ?, ?)',
[id, email, hash, role, 10, 500, is_active]
);
if (is_active === 0) {
if (is_active === false) {
return res.json({ status: 'pending', message: 'Account created. Please contact an administrator to activate your account.' });
}
@@ -94,7 +94,7 @@ app.post('/api/auth/login', async (req, res) => {
const valid = await bcrypt.compare(password, user.password_hash);
if (!valid) return res.status(400).json({ error: 'Invalid credentials' });
if (user.is_active === 0) {
if (user.is_active === false) {
return res.status(403).json({ error: 'Account pending admin approval. Please contact an administrator.' });
}
@@ -189,7 +189,7 @@ app.put('/api/users/:id', authenticate, async (req, res) => {
if (req.user.role !== 'admin') return res.status(403).json({ error: 'Forbidden' });
const { role, api_credits, storage_limit_mb, is_active } = req.body;
try {
await run('UPDATE profiles SET role = ?, api_credits = ?, storage_limit_mb = ?, is_active = ? WHERE id = ?', [role, api_credits, storage_limit_mb, is_active !== undefined ? is_active : 1, req.params.id]);
await run('UPDATE profiles SET role = ?, api_credits = ?, storage_limit_mb = ?, is_active = ? WHERE id = ?', [role, api_credits, storage_limit_mb, is_active !== undefined ? is_active : true, req.params.id]);
res.json({ success: true });
} catch (err) {
res.status(500).json({ error: err.message });