import express from "express"; import pool from "../db/pool.js"; const router = express.Router(); // 1. CREATE User router.post("/", async (req, res) => { const client = await pool.connect(); try { await client.query("BEGIN"); const { id, // Optional: if provided, use this UUID; otherwise generate one name, phone_number, avatar_url, language, timezone, country_code = "+91", } = req.body; // If id is provided, use it; otherwise let the database generate one const insertUserQuery = id ? ` INSERT INTO users (id, name, phone_number, avatar_url, language, timezone, country_code) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING * ` : ` INSERT INTO users (name, phone_number, avatar_url, language, timezone, country_code) VALUES ($1, $2, $3, $4, $5, $6) RETURNING * `; const userValues = id ? [id, name, phone_number, avatar_url, language, timezone, country_code] : [name, phone_number, avatar_url, language, timezone, country_code]; const userResult = await client.query(insertUserQuery, userValues); await client.query("COMMIT"); res.status(201).json(userResult.rows[0]); } catch (err) { await client.query("ROLLBACK"); console.error("Error creating user:", err); if (err.code === "23505") { // Unique constraint violation return res.status(400).json({ error: err.detail || "A user with this phone number or ID already exists", }); } if (err.code === "23503") { // Foreign key violation return res.status(400).json({ error: err.detail || "Foreign key constraint violation", }); } res.status(500).json({ error: err.message || "Internal server error" }); } finally { client.release(); } }); // 2. GET All Users router.get("/", async (req, res) => { try { const { limit = 100, offset = 0 } = req.query; const queryText = ` SELECT id, name, phone_number, avatar_url, language, timezone, country_code, is_active, created_at, updated_at FROM users WHERE deleted = FALSE ORDER BY created_at DESC LIMIT $1 OFFSET $2 `; const result = await pool.query(queryText, [limit, offset]); res.json(result.rows); } catch (err) { console.error("Error fetching users:", err); res.status(500).json({ error: "Internal server error" }); } }); // 3. GET Single User router.get("/:id", async (req, res) => { try { const { id } = req.params; const queryText = ` SELECT id, name, phone_number, avatar_url, language, timezone, country_code, is_active, created_at, updated_at FROM users WHERE id = $1 AND deleted = FALSE `; const result = await pool.query(queryText, [id]); if (result.rows.length === 0) { return res.status(404).json({ error: "User not found" }); } res.json(result.rows[0]); } catch (err) { console.error("Error fetching user:", err); res.status(500).json({ error: "Internal server error" }); } }); // 4. UPDATE User router.put("/:id", async (req, res) => { try { const { id } = req.params; const { name, phone_number, avatar_url, language, timezone, country_code, is_active } = req.body; const updateQuery = ` UPDATE users SET name = COALESCE($1, name), phone_number = COALESCE($2, phone_number), avatar_url = COALESCE($3, avatar_url), language = COALESCE($4, language), timezone = COALESCE($5, timezone), country_code = COALESCE($6, country_code), is_active = COALESCE($7, is_active) WHERE id = $8 AND deleted = FALSE RETURNING * `; const values = [name, phone_number, avatar_url, language, timezone, country_code, is_active, id]; const result = await pool.query(updateQuery, values); if (result.rows.length === 0) { return res.status(404).json({ error: "User not found" }); } res.json(result.rows[0]); } catch (err) { console.error("Error updating user:", err); res.status(500).json({ error: "Internal server error" }); } }); // 5. DELETE User (Soft Delete) router.delete("/:id", async (req, res) => { try { const { id } = req.params; const queryText = ` UPDATE users SET deleted = TRUE WHERE id = $1 RETURNING id `; const result = await pool.query(queryText, [id]); if (result.rows.length === 0) { return res.status(404).json({ error: "User not found" }); } res.json({ message: "User deleted successfully" }); } catch (err) { console.error("Error deleting user:", err); res.status(500).json({ error: "Internal server error" }); } }); export default router;