powr-sdk-api 2.3.7 → 2.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.js CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  const config = {
4
4
  mongoUri: process.env.POWR_DB_URI,
5
- projectId: process.env.PROJECT_ID,
6
- jwtToken: process.env.JWT_TOKEN,
7
- storageBucket: process.env.STORAGE_BUCKET
5
+ jwtToken: process.env.JWT_TOKEN
8
6
  };
9
7
  module.exports = config;
@@ -29,6 +29,12 @@ const verifyToken = async (req, res, next) => {
29
29
 
30
30
  // Extract token (remove 'Bearer ' if present)
31
31
  const token = authHeader.startsWith("Bearer ") ? authHeader.slice(7) : authHeader;
32
+ if (!token) {
33
+ return res.status(401).json({
34
+ success: false,
35
+ message: 'No token provided'
36
+ });
37
+ }
32
38
 
33
39
  // Verify JWT token
34
40
  const decoded = jwt.verify(token, config.jwtToken);
@@ -185,13 +185,31 @@ router.post("/login", async (req, res) => {
185
185
  const token = generateJWTToken(user, profile);
186
186
  const {
187
187
  password: _,
188
+ _id,
188
189
  ...userWithoutPassword
189
190
  } = user;
191
+ let mergedUser = {
192
+ ...userWithoutPassword,
193
+ powrId: user._id
194
+ };
195
+ if (profile) {
196
+ const {
197
+ _id,
198
+ userId,
199
+ projectId,
200
+ createdAt,
201
+ updatedAt,
202
+ ...profileData
203
+ } = profile;
204
+ mergedUser = {
205
+ ...mergedUser,
206
+ ...profileData
207
+ };
208
+ }
190
209
  return res.status(200).json({
191
210
  success: true,
192
211
  message: "Login successful",
193
- user: userWithoutPassword,
194
- profile: profile,
212
+ user: mergedUser,
195
213
  token: token
196
214
  });
197
215
  } catch (error) {
@@ -3,11 +3,7 @@
3
3
  const express = require('express');
4
4
  const multer = require('multer');
5
5
  // const { uploadToGCS, bucket } = require('../uploadToGCS')
6
- const {
7
- bucket,
8
- uploadToGCS,
9
- bucketName
10
- } = require('../uploadToGCS.js');
6
+ // const { bucket, uploadToGCS, bucketName } = require('../uploadToGCS.js')
11
7
  // const uploadToGCS = require('../uploadToGCS.js')
12
8
 
13
9
  const router = express.Router();
@@ -20,11 +16,13 @@ router.post('/', upload.single('file'), async (req, res) => {
20
16
  message: 'No file uploaded'
21
17
  });
22
18
  }
23
- const url = await uploadToGCS(req.file);
19
+
20
+ // const url = await uploadToGCS(req.file);
21
+
24
22
  return res.status(200).json({
25
23
  status: 'success',
26
- message: 'File uploaded successfully',
27
- url
24
+ message: 'File uploaded successfully'
25
+ // url,
28
26
  });
29
27
  } catch (err) {
30
28
  console.error('Error uploading file:', err);
@@ -37,12 +35,13 @@ router.post('/', upload.single('file'), async (req, res) => {
37
35
  });
38
36
  router.get('/', async (req, res) => {
39
37
  try {
40
- const [files] = await bucket.getFiles();
41
- const bucketName = bucket.name;
42
- const imageUrls = files.map(file => `https://storage.googleapis.com/${bucketName}/${file.name}`);
38
+ // const [files] = await bucket.getFiles();
39
+ // const bucketName = bucket.name;
40
+ // const imageUrls = files.map(file => `https://storage.googleapis.com/${bucketName}/${file.name}`);
41
+
43
42
  return res.status(200).json({
44
- message: 'Images fetched successfully',
45
- images: imageUrls
43
+ message: 'Images fetched successfully'
44
+ // images: imageUrls,
46
45
  });
47
46
  } catch (err) {
48
47
  console.error('Error listing files from GCS:', err);
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const express = require('express');
4
+ const config = require('../config');
4
5
 
5
6
  // Import all route modules
6
7
  const commentsRoutes = require('./comments');
@@ -17,17 +18,30 @@ const authRoutes = require('./auth');
17
18
  const blogsRoutes = require('./blogs');
18
19
  const slidesRoutes = require('./slides');
19
20
  const notificationsRoutes = require('./notifications');
20
- const createPowrRoutes = () => {
21
+ const profilesRoutes = require('./profiles');
22
+ const createPowrRoutes = (options = {}) => {
21
23
  const router = express.Router();
22
24
 
25
+ // Check if this is a central service (like powr-base-cloud)
26
+ const isCentralService = options.isCentralService || false;
27
+
23
28
  // Middleware to inject projectId into all requests
24
29
  router.use((req, res, next) => {
25
30
  try {
26
- const {
27
- getConfig
28
- } = require('../config');
29
- const config = getConfig();
30
- req.projectId = config.projectId;
31
+ if (isCentralService) {
32
+ // For central services, get projectId from request
33
+ const projectId = req.query.projectId || req.body.projectId;
34
+ if (!projectId) {
35
+ return res.status(400).json({
36
+ success: false,
37
+ message: 'projectId is required for central services'
38
+ });
39
+ }
40
+ req.projectId = projectId;
41
+ } else {
42
+ // For individual APIs, use config.projectId
43
+ req.projectId = config.projectId;
44
+ }
31
45
  } catch (error) {
32
46
  console.error('❌ Config error:', error.message);
33
47
  return res.status(500).json({
@@ -38,8 +52,6 @@ const createPowrRoutes = () => {
38
52
  }
39
53
  next();
40
54
  });
41
-
42
- // Mount all route modules
43
55
  router.use('/comments', commentsRoutes);
44
56
  // router.use('/files', filesRoutes); // Commented out for now
45
57
  router.use('/forms', formsRoutes);
@@ -54,6 +66,7 @@ const createPowrRoutes = () => {
54
66
  router.use('/blogs', blogsRoutes);
55
67
  router.use('/slides', slidesRoutes);
56
68
  router.use('/notifications', notificationsRoutes);
69
+ router.use('/profiles', profilesRoutes);
57
70
  return router;
58
71
  };
59
72
  module.exports = {
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ const express = require("express");
4
+ const router = express.Router();
5
+ const {
6
+ getDb
7
+ } = require("../services/dbService");
8
+
9
+ // Get all users for a project
10
+ router.get("/", async (req, res) => {
11
+ try {
12
+ const {
13
+ projectId
14
+ } = req.query;
15
+ const profiles = await getDb().collection("profiles").find({
16
+ projectId: projectId
17
+ }).toArray();
18
+ if (profiles.length === 0) {
19
+ return res.status(200).json({
20
+ success: true,
21
+ users: []
22
+ });
23
+ }
24
+
25
+ // Get core user data for all profiles
26
+ const userIds = profiles.map(profile => profile.userId);
27
+ const users = await getDb().collection("users").find({
28
+ _id: {
29
+ $in: userIds
30
+ }
31
+ }).toArray();
32
+
33
+ // Create a map for quick lookup
34
+ const usersMap = users.reduce((acc, user) => {
35
+ const {
36
+ password,
37
+ ...userWithoutPassword
38
+ } = user;
39
+ acc[user._id.toString()] = userWithoutPassword;
40
+ return acc;
41
+ }, {});
42
+
43
+ // Merge user and profile data
44
+ const usersWithProfiles = profiles.map(profile => ({
45
+ ...usersMap[profile.userId.toString()],
46
+ ...profile
47
+ }));
48
+ return res.status(200).json({
49
+ success: true,
50
+ users: usersWithProfiles
51
+ });
52
+ } catch (error) {
53
+ console.error("Error fetching project users:", error);
54
+ return res.status(500).json({
55
+ success: false,
56
+ message: "Failed to fetch project users"
57
+ });
58
+ }
59
+ });
60
+ module.exports = router;
@@ -3,38 +3,40 @@
3
3
  const {
4
4
  MongoClient
5
5
  } = require('mongodb');
6
-
7
- // Database service with lazy connection
8
- let client = null;
9
- let db = null;
10
- const getDb = async () => {
11
- // Return existing connection if available
12
- if (db) {
13
- return db;
14
- }
15
-
16
- // Connect only when first needed
6
+ const {
7
+ config
8
+ } = require('../config');
9
+ const mongoClient = new MongoClient(config.mongoUri, {
10
+ maxPoolSize: 10,
11
+ serverSelectionTimeoutMS: 5000,
12
+ socketTimeoutMS: 45000
13
+ });
14
+ let db;
15
+ async function connect() {
17
16
  try {
18
- const config = require('../config').getConfig();
19
- client = new MongoClient(config.mongoUri);
20
- await client.connect();
21
- db = client.db();
22
- console.log('✅ Connected to MongoDB via SDK (lazy connection)');
23
- return db;
17
+ await mongoClient.connect();
18
+ db = mongoClient.db();
19
+ console.log("Connected to MongoDB via SDK");
24
20
  } catch (error) {
25
- console.error('❌ MongoDB connection error:', error);
26
- throw error;
21
+ console.error("Failed to connect to MongoDB:", error);
22
+ process.exit(1);
27
23
  }
28
- };
24
+ }
25
+ function getDb() {
26
+ if (!db) {
27
+ throw new Error("Database not initialized. Call connectToMongo() first.");
28
+ }
29
+ return db;
30
+ }
29
31
  const closeConnection = async () => {
30
- if (client) {
31
- await client.close();
32
- client = null;
32
+ if (mongoClient) {
33
+ await mongoClient.close();
33
34
  db = null;
34
35
  console.log('✅ MongoDB connection closed');
35
36
  }
36
37
  };
37
38
  module.exports = {
39
+ connect,
38
40
  getDb,
39
41
  closeConnection
40
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "2.3.7",
3
+ "version": "2.3.8",
4
4
  "description": "Shared API core library for PowrStack projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",