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.

60 lines (54 loc) 1.5 kB
"use strict"; const express = require("express"); const router = express.Router(); const { getDb } = require("../services/mongo"); // Get all users for a project router.get("/", async (req, res) => { try { const projectId = req.projectId; // Use middleware-injected projectId const db = await getDb(); const profiles = await db.collection("profiles").find({ projectId: projectId }).toArray(); if (profiles.length === 0) { return res.status(200).json({ success: true, users: [] }); } // Get core user data for all profiles const userIds = profiles.map(profile => profile.userId); const users = await db.collection("users").find({ _id: { $in: userIds } }).toArray(); // Create a map for quick lookup const usersMap = users.reduce((acc, user) => { const { password, ...userWithoutPassword } = user; acc[user._id.toString()] = userWithoutPassword; return acc; }, {}); // Merge user and profile data const usersWithProfiles = profiles.map(profile => ({ ...usersMap[profile.userId.toString()], ...profile })); return res.status(200).json({ success: true, users: usersWithProfiles }); } catch (error) { console.error("Error fetching project users:", error); return res.status(500).json({ success: false, message: "Failed to fetch project users" }); } }); module.exports = router;