powr-sdk-api 4.8.0 → 4.8.1

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/index.js CHANGED
@@ -22,8 +22,7 @@ const {
22
22
  initializeTools,
23
23
  executeTasks,
24
24
  executeTool,
25
- createTask,
26
- emitActivity
25
+ createTask
27
26
  } = require("./managers");
28
27
  const {
29
28
  verifyToken
@@ -45,6 +44,5 @@ module.exports = {
45
44
  executeTasks,
46
45
  executeTool,
47
46
  createTask,
48
- emitActivity,
49
47
  getPowrDb
50
48
  };
@@ -3,7 +3,6 @@
3
3
  const functionsManager = require('./functions');
4
4
  const toolsManager = require('./tools');
5
5
  const scheduledTasksManager = require('./tasks');
6
- const activitiesManager = require('./activities');
7
6
 
8
7
  // Async Functions initialization function
9
8
  const initializeFunctions = async (options = {}) => {
@@ -28,12 +27,11 @@ const createTask = async taskData => {
28
27
  // Create a new task/workflow
29
28
  return await scheduledTasksManager.createTask(taskData);
30
29
  };
31
- const emitActivity = activitiesManager.emitActivity;
32
30
  module.exports = {
31
+ // Initialization/Execution methods
33
32
  initializeFunctions,
34
33
  initializeTools,
35
34
  executeTasks,
36
35
  executeTool,
37
- createTask,
38
- emitActivity
36
+ createTask
39
37
  };
@@ -17,6 +17,9 @@ const {
17
17
  const {
18
18
  config
19
19
  } = require("../config");
20
+ const {
21
+ getClientIp
22
+ } = require("../utils/getClientIp");
20
23
 
21
24
  // Register User
22
25
  router.post("/register", async (req, res) => {
@@ -77,11 +80,13 @@ router.post("/register", async (req, res) => {
77
80
  };
78
81
  const result = await db.collection("users").insertOne(newUser);
79
82
  if (projectId) {
83
+ const now = new Date();
80
84
  const newProfile = {
81
85
  userId: result.insertedId,
82
86
  projectId: projectId,
83
- createdAt: new Date(),
84
- updatedAt: new Date()
87
+ createdAt: now,
88
+ lastActiveAt: now,
89
+ lastActiveFrom: getClientIp(req)
85
90
  };
86
91
  await db.collection("profiles").insertOne(newProfile);
87
92
  }
@@ -156,21 +161,23 @@ router.post("/login", async (req, res) => {
156
161
  projectId: projectId
157
162
  });
158
163
  if (profile) {
159
- // Update lastActiveAt
164
+ const now = new Date();
160
165
  await db.collection("profiles").updateOne({
161
166
  _id: profile._id
162
167
  }, {
163
168
  $set: {
164
- lastActiveAt: new Date()
169
+ lastActiveAt: now,
170
+ lastActiveFrom: getClientIp(req)
165
171
  }
166
172
  });
167
173
  } else {
168
- // Create profile if it doesn't exist - only essential fields
174
+ const now = new Date();
169
175
  const newProfile = {
170
176
  userId: user._id,
171
177
  projectId: projectId,
172
- createdAt: new Date(),
173
- updatedAt: new Date()
178
+ createdAt: now,
179
+ lastActiveAt: now,
180
+ lastActiveFrom: getClientIp(req)
174
181
  };
175
182
  const profileResult = await db.collection("profiles").insertOne(newProfile);
176
183
  profile = {
@@ -204,6 +211,8 @@ router.post("/login", async (req, res) => {
204
211
  projectId,
205
212
  createdAt,
206
213
  updatedAt,
214
+ lastActiveAt,
215
+ lastActiveFrom,
207
216
  ...profileData
208
217
  } = profile;
209
218
  mergedUser = {
@@ -11,6 +11,9 @@ const {
11
11
  const {
12
12
  verifyToken
13
13
  } = require("../middleware/jwtToken");
14
+ const {
15
+ getClientIp
16
+ } = require("../utils/getClientIp");
14
17
 
15
18
  // Create User
16
19
  router.post("/", verifyToken, async (req, res) => {
@@ -52,7 +55,8 @@ router.post("/", verifyToken, async (req, res) => {
52
55
  projectId: projectId,
53
56
  access: userData.access || 1,
54
57
  createdAt: new Date(),
55
- updatedAt: new Date()
58
+ lastActiveAt: new Date(),
59
+ lastActiveFrom: getClientIp(req)
56
60
  };
57
61
  await db.collection("profiles").insertOne(newProfile);
58
62
  return res.status(201).json({
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Best-effort client IP from the incoming HTTP request
5
+ * (uses X-Forwarded-For, X-Real-IP, req.ip, or socket address).
6
+ */
7
+ function getClientIp(req) {
8
+ const forwarded = req.headers["x-forwarded-for"];
9
+ const realIp = req.headers["x-real-ip"];
10
+ if (typeof forwarded === "string" && forwarded.length > 0) {
11
+ const first = forwarded.split(",")[0].trim();
12
+ if (first) {
13
+ console.log("[getClientIp] using x-forwarded-for:", first);
14
+ return first;
15
+ }
16
+ }
17
+ if (typeof realIp === "string" && realIp.trim()) {
18
+ const v = realIp.trim();
19
+ console.log("[getClientIp] using x-real-ip:", v);
20
+ return v;
21
+ }
22
+ if (req.ip) {
23
+ console.log("[getClientIp] using req.ip:", req.ip);
24
+ return req.ip;
25
+ }
26
+ if (req.socket && req.socket.remoteAddress) {
27
+ console.log("[getClientIp] using socket.remoteAddress:", req.socket.remoteAddress);
28
+ return req.socket.remoteAddress;
29
+ }
30
+ console.log("[getClientIp] no IP found", {
31
+ "x-forwarded-for": forwarded,
32
+ "x-real-ip": realIp,
33
+ reqIp: req.ip,
34
+ remoteAddress: req.socket && req.socket.remoteAddress
35
+ });
36
+ return null;
37
+ }
38
+ module.exports = {
39
+ getClientIp
40
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "4.8.0",
3
+ "version": "4.8.1",
4
4
  "description": "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.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",