UNPKG

powr-sdk-api

Version:

Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.

110 lines (102 loc) 2.64 kB
"use strict"; const express = require("express"); const router = express.Router(); const { getDb } = require("../services/mongo"); const { ObjectId } = require("mongodb"); // Get all waitlists router.get("/", async (req, res) => { const projectId = req.projectId; // Use middleware-injected projectId try { const db = await getDb(); const waitlists = await db.collection("waitlists").find({ projectId }).toArray(); return res.json({ success: true, data: waitlists }); } catch (error) { console.error("Error fetching waitlists:", error); return res.status(500).json({ success: false, message: "Failed to fetch waitlists." }); } }); // Add email to waitlist router.post("/", async (req, res) => { const { email } = req.body; const projectId = req.projectId; // Use middleware-injected projectId try { if (!email) { return res.status(400).json({ success: false, message: "Email is required" }); } // Check if email already exists in waitlist const existingEntry = await getDb().collection("waitlists").findOne({ projectId, email }); if (existingEntry) { return res.status(400).json({ success: false, message: "Email already exists in waitlist" }); } const newEntry = { projectId, email, createdAt: new Date() }; const result = await getDb().collection("waitlists").insertOne(newEntry); return res.status(201).json({ success: true, message: "Email added to waitlist successfully", id: result.insertedId }); } catch (error) { console.error("Error adding to waitlist:", error); return res.status(500).json({ success: false, message: "Failed to add to waitlist." }); } }); // Remove email from waitlist router.delete("/:id", async (req, res) => { const { id } = req.params; const projectId = req.projectId; // Use middleware-injected projectId try { const result = await getDb().collection("waitlists").deleteOne({ _id: new ObjectId(id), projectId }); if (result.deletedCount === 0) { return res.status(404).json({ success: false, message: "Waitlist entry not found" }); } return res.json({ success: true, message: "Email removed from waitlist successfully" }); } catch (error) { console.error("Error removing from waitlist:", error); return res.status(500).json({ success: false, message: "Failed to remove from waitlist." }); } }); module.exports = router;