powr-sdk-api 4.1.10 → 4.3.0

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.
Files changed (46) hide show
  1. package/dist/index.js +7 -5
  2. package/dist/{services → managers}/functions.js +1 -1
  3. package/dist/managers/index.js +26 -0
  4. package/dist/{services/scheduledTasks.js → managers/tasks.js} +123 -143
  5. package/dist/{services → managers}/tools.js +1 -1
  6. package/dist/middleware/projectId.js +1 -1
  7. package/dist/routes/functions.js +3 -1
  8. package/dist/routes/index.js +3 -27
  9. package/dist/routes/tasks.js +304 -0
  10. package/dist/routes/tools.js +21 -8
  11. package/package.json +69 -71
  12. package/dist/admin/activities.js +0 -81
  13. package/dist/admin/auth.js +0 -234
  14. package/dist/admin/blogs.js +0 -94
  15. package/dist/admin/comments.js +0 -83
  16. package/dist/admin/files.js +0 -56
  17. package/dist/admin/forms.js +0 -242
  18. package/dist/admin/index.js +0 -53
  19. package/dist/admin/invoice.js +0 -163
  20. package/dist/admin/likes.js +0 -126
  21. package/dist/admin/notifications.js +0 -93
  22. package/dist/admin/plexx.js +0 -53
  23. package/dist/admin/ratings.js +0 -189
  24. package/dist/admin/routes.js +0 -132
  25. package/dist/admin/slides.js +0 -101
  26. package/dist/admin/users.js +0 -175
  27. package/dist/admin/waitlists.js +0 -94
  28. package/dist/logger/gcs.js +0 -78
  29. package/dist/logger/s3.js +0 -78
  30. package/dist/middleware/auth.js +0 -76
  31. package/dist/middleware/logger.js +0 -45
  32. package/dist/middleware/response.js +0 -53
  33. package/dist/routes/admin/index.js +0 -520
  34. package/dist/routes/blogs.js +0 -94
  35. package/dist/routes/invoice.js +0 -167
  36. package/dist/routes/plexx.js +0 -269
  37. package/dist/routes/routes.js +0 -143
  38. package/dist/routes/scheduledTasks.js +0 -240
  39. package/dist/scripts/update-tools-schema.js +0 -157
  40. package/dist/services/dbService.js +0 -42
  41. package/dist/services/logger.js +0 -35
  42. package/dist/services/plexx.js +0 -229
  43. package/dist/utils/auth.js +0 -19
  44. package/dist/utils/logger.js +0 -57
  45. package/dist/utils/s3-transport.js +0 -61
  46. package/dist/utils/s3.js +0 -78
@@ -0,0 +1,304 @@
1
+ "use strict";
2
+
3
+ const express = require("express");
4
+ const router = express.Router();
5
+ const {
6
+ toolsManager,
7
+ scheduledTasksManager
8
+ } = require("../managers");
9
+
10
+ // GET /tasks - Get all tasks
11
+ router.get("/", async (req, res) => {
12
+ try {
13
+ const isAdmin = req.user.access === 100;
14
+ const result = await scheduledTasksManager.getUserTasks(req.user.powrId, req.projectId, isAdmin);
15
+ if (result.success) {
16
+ return res.status(200).json({
17
+ success: true,
18
+ tasks: result.tasks
19
+ });
20
+ } else {
21
+ return res.status(400).json({
22
+ success: false,
23
+ message: result.message
24
+ });
25
+ }
26
+ } catch (error) {
27
+ console.error("Error retrieving tasks:", error);
28
+ return res.status(500).json({
29
+ success: false,
30
+ message: "Failed to retrieve tasks."
31
+ });
32
+ }
33
+ });
34
+
35
+ // GET /tasks/:taskId - Get specific task
36
+ router.get("/:taskId", async (req, res) => {
37
+ try {
38
+ const isAdmin = req.user.access === 100;
39
+ const result = await scheduledTasksManager.getTask(req.params.taskId, req.user.powrId, req.projectId, isAdmin);
40
+ if (result.success) {
41
+ return res.status(200).json({
42
+ success: true,
43
+ task: result.task
44
+ });
45
+ } else {
46
+ return res.status(404).json({
47
+ success: false,
48
+ message: result.message
49
+ });
50
+ }
51
+ } catch (error) {
52
+ console.error("Error retrieving task:", error);
53
+ return res.status(500).json({
54
+ success: false,
55
+ message: "Failed to retrieve task."
56
+ });
57
+ }
58
+ });
59
+
60
+ // POST /tasks - Create new task
61
+ router.post("/", async (req, res) => {
62
+ try {
63
+ var _tool$actions;
64
+ const {
65
+ name,
66
+ description,
67
+ scheduledFor,
68
+ toolId,
69
+ actionId,
70
+ params,
71
+ isActive
72
+ } = req.body;
73
+
74
+ // Validate required fields
75
+ if (!name || !toolId || !actionId) {
76
+ return res.status(400).json({
77
+ success: false,
78
+ message: "Missing required fields: name, toolId, actionId"
79
+ });
80
+ }
81
+
82
+ // Validate tool exists
83
+ const tool = toolsManager.getTool(toolId);
84
+ if (!tool) {
85
+ return res.status(400).json({
86
+ success: false,
87
+ message: `Tool not found: ${toolId}`
88
+ });
89
+ }
90
+
91
+ // Validate action exists
92
+ const action = (_tool$actions = tool.actions) === null || _tool$actions === void 0 ? void 0 : _tool$actions.find(a => a.id === actionId);
93
+ if (!action) {
94
+ return res.status(400).json({
95
+ success: false,
96
+ message: `Action not found: ${actionId} for tool ${toolId}`
97
+ });
98
+ }
99
+ const taskData = {
100
+ name,
101
+ description,
102
+ scheduledFor,
103
+ // null, timestamp, or cron expression
104
+ toolId,
105
+ actionId,
106
+ params: params || {},
107
+ userId: req.user.powrId,
108
+ projectId: req.projectId,
109
+ isActive: isActive !== false // Default to true
110
+ };
111
+ const result = await scheduledTasksManager.createTask(taskData);
112
+ if (result.success) {
113
+ res.status(201).json({
114
+ success: true,
115
+ message: "Task created successfully",
116
+ task: result.task
117
+ });
118
+ } else {
119
+ res.status(400).json({
120
+ success: false,
121
+ message: result.message
122
+ });
123
+ }
124
+ } catch (error) {
125
+ console.error("❌ Error creating scheduled task:", error);
126
+ res.status(500).json({
127
+ success: false,
128
+ message: "Failed to create scheduled task"
129
+ });
130
+ }
131
+ });
132
+
133
+ // PUT /tasks/:taskId - Update task
134
+ router.put("/:taskId", async (req, res) => {
135
+ try {
136
+ const {
137
+ name,
138
+ description,
139
+ scheduledFor,
140
+ toolId,
141
+ actionId,
142
+ params,
143
+ isActive
144
+ } = req.body;
145
+
146
+ // Validate tool exists if toolId is being updated
147
+ if (toolId) {
148
+ const tool = toolsManager.getTool(toolId);
149
+ if (!tool) {
150
+ return res.status(400).json({
151
+ success: false,
152
+ message: `Tool not found: ${toolId}`
153
+ });
154
+ }
155
+
156
+ // Validate action exists if actionId is being updated
157
+ if (actionId) {
158
+ var _tool$actions2;
159
+ const action = (_tool$actions2 = tool.actions) === null || _tool$actions2 === void 0 ? void 0 : _tool$actions2.find(a => a.id === actionId);
160
+ if (!action) {
161
+ return res.status(400).json({
162
+ success: false,
163
+ message: `Action not found: ${actionId} for tool ${toolId}`
164
+ });
165
+ }
166
+ }
167
+ }
168
+ const updateData = {};
169
+ if (name !== undefined) updateData.name = name;
170
+ if (description !== undefined) updateData.description = description;
171
+ if (scheduledFor !== undefined) updateData.scheduledFor = scheduledFor;
172
+ if (toolId !== undefined) updateData.toolId = toolId;
173
+ if (actionId !== undefined) updateData.actionId = actionId;
174
+ if (params !== undefined) updateData.params = params;
175
+ if (isActive !== undefined) updateData.isActive = isActive;
176
+ const isAdmin = req.user.access === 100;
177
+ const result = await scheduledTasksManager.updateTask(req.params.taskId, req.user.powrId, req.projectId, updateData, isAdmin);
178
+ if (result.success) {
179
+ res.status(200).json({
180
+ success: true,
181
+ message: "Task updated successfully"
182
+ });
183
+ } else {
184
+ res.status(400).json({
185
+ success: false,
186
+ message: result.message
187
+ });
188
+ }
189
+ } catch (error) {
190
+ console.error("❌ Error updating scheduled task:", error);
191
+ res.status(500).json({
192
+ success: false,
193
+ message: "Failed to update scheduled task"
194
+ });
195
+ }
196
+ });
197
+
198
+ // POST /tasks/:taskId/execute - Execute task manually
199
+ router.post("/:taskId/execute", async (req, res) => {
200
+ try {
201
+ const taskId = req.params.taskId;
202
+ const isAdmin = req.user.access === 100;
203
+ const result = await scheduledTasksManager.executeTask(taskId, req.user.powrId, req.projectId, isAdmin);
204
+ if (result.success) {
205
+ res.json({
206
+ success: true,
207
+ message: "Task executed successfully",
208
+ result: result.result
209
+ });
210
+ } else {
211
+ res.status(400).json({
212
+ success: false,
213
+ message: result.message
214
+ });
215
+ }
216
+ } catch (error) {
217
+ console.error("❌ Error executing scheduled task:", error);
218
+ res.status(500).json({
219
+ success: false,
220
+ message: "Failed to execute scheduled task"
221
+ });
222
+ }
223
+ });
224
+
225
+ // POST /tasks/:taskId/toggle - Toggle task active status
226
+ router.post("/:taskId/toggle", async (req, res) => {
227
+ try {
228
+ const taskId = req.params.taskId;
229
+ const {
230
+ isActive
231
+ } = req.body;
232
+ const isAdmin = req.user.access === 100;
233
+ const result = await scheduledTasksManager.toggleTask(taskId, req.user.powrId, req.projectId, isActive, isAdmin);
234
+ if (result.success) {
235
+ res.json({
236
+ success: true,
237
+ message: `Task ${isActive ? 'activated' : 'deactivated'} successfully`
238
+ });
239
+ } else {
240
+ res.status(400).json({
241
+ success: false,
242
+ message: result.message
243
+ });
244
+ }
245
+ } catch (error) {
246
+ console.error("❌ Error toggling scheduled task:", error);
247
+ res.status(500).json({
248
+ success: false,
249
+ message: "Failed to toggle scheduled task"
250
+ });
251
+ }
252
+ });
253
+
254
+ // DELETE /tasks/:taskId - Delete task
255
+ router.delete("/:taskId", async (req, res) => {
256
+ try {
257
+ const taskId = req.params.taskId;
258
+ const isAdmin = req.user.access === 100;
259
+ const result = await scheduledTasksManager.deleteTask(taskId, req.user.powrId, req.projectId, isAdmin);
260
+ if (result.success) {
261
+ res.json({
262
+ success: true,
263
+ message: "Task deleted successfully"
264
+ });
265
+ } else {
266
+ res.status(400).json({
267
+ success: false,
268
+ message: result.message
269
+ });
270
+ }
271
+ } catch (error) {
272
+ console.error("❌ Error deleting scheduled task:", error);
273
+ res.status(500).json({
274
+ success: false,
275
+ message: "Failed to delete scheduled task"
276
+ });
277
+ }
278
+ });
279
+
280
+ // POST /tasks/execute-scheduled - Execute all scheduled tasks (called by Atlas)
281
+ router.post("/execute-scheduled", async (req, res) => {
282
+ try {
283
+ const projectId = req.projectId;
284
+ console.log(`🔍 API: Executing scheduled tasks for project: ${projectId}`);
285
+
286
+ // Execute all scheduled tasks for this project
287
+ const result = await scheduledTasksManager.executeScheduledTasks(projectId);
288
+ console.log(`✅ API: Scheduled tasks execution completed for project: ${projectId}`);
289
+ res.json({
290
+ success: true,
291
+ message: "Scheduled tasks executed successfully",
292
+ projectId: projectId,
293
+ result: result
294
+ });
295
+ } catch (error) {
296
+ console.error("❌ API: Error executing scheduled tasks:", error);
297
+ res.status(500).json({
298
+ success: false,
299
+ message: "Failed to execute scheduled tasks",
300
+ error: error.message
301
+ });
302
+ }
303
+ });
304
+ module.exports = router;
@@ -2,7 +2,9 @@
2
2
 
3
3
  const express = require("express");
4
4
  const router = express.Router();
5
- const toolsManager = require("../services/tools");
5
+ const {
6
+ toolsManager
7
+ } = require("../managers");
6
8
  const {
7
9
  getDb
8
10
  } = require("../services/mongo");
@@ -192,7 +194,7 @@ router.post('/:toolId/configure', async (req, res) => {
192
194
  // Execute tool action
193
195
  router.post('/:toolId/execute', async (req, res) => {
194
196
  try {
195
- var _req$user2;
197
+ var _req$headers$authoriz;
196
198
  const {
197
199
  toolId
198
200
  } = req.params;
@@ -200,13 +202,24 @@ router.post('/:toolId/execute', async (req, res) => {
200
202
  actionId,
201
203
  params
202
204
  } = req.body;
203
- const userId = (_req$user2 = req.user) === null || _req$user2 === void 0 ? void 0 : _req$user2.powrId; // From JWT token
204
205
 
205
- if (!userId) {
206
- return res.status(401).json({
207
- success: false,
208
- message: "User not authenticated"
209
- });
206
+ // Check for service-to-service authentication
207
+ const apiKey = req.headers['x-api-key'] || ((_req$headers$authoriz = req.headers['authorization']) === null || _req$headers$authoriz === void 0 ? void 0 : _req$headers$authoriz.replace('Bearer ', ''));
208
+ const isServiceCall = apiKey === process.env.POWR_SERVICE_API_KEY;
209
+ let userId;
210
+ if (isServiceCall) {
211
+ // For service calls, use the userId from the task
212
+ userId = req.headers['x-user-id'] || 'system';
213
+ } else {
214
+ var _req$user2;
215
+ // For regular calls, use JWT token
216
+ userId = (_req$user2 = req.user) === null || _req$user2 === void 0 ? void 0 : _req$user2.powrId;
217
+ if (!userId) {
218
+ return res.status(401).json({
219
+ success: false,
220
+ message: "User not authenticated"
221
+ });
222
+ }
210
223
  }
211
224
  if (!actionId) {
212
225
  return res.status(400).json({
package/package.json CHANGED
@@ -1,71 +1,69 @@
1
- {
2
- "name": "powr-sdk-api",
3
- "version": "4.1.10",
4
- "description": "Shared API core library for PowrStack projects",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "files": [
8
- "dist",
9
- "README.md"
10
- ],
11
- "scripts": {
12
- "test": "jest --passWithNoTests",
13
- "lint": "eslint .",
14
- "build": "babel src -d dist",
15
- "prepare": "npm run build",
16
- "prepublishOnly": "npm run test"
17
- },
18
- "keywords": [
19
- "api",
20
- "express",
21
- "middleware",
22
- "error-handling",
23
- "response-formatting",
24
- "logging",
25
- "swagger",
26
- "documentation"
27
- ],
28
- "author": "Lawazia Tech",
29
- "license": "ISC",
30
- "repository": {
31
- "type": "git",
32
- "url": "git+https://github.com/powrstack/powr-sdk-api.git"
33
- },
34
- "bugs": {
35
- "url": "https://github.com/powrstack/powr-sdk-api/issues"
36
- },
37
- "homepage": "https://github.com/powrstack/powr-sdk-api#readme",
38
- "dependencies": {
39
- "@aws-sdk/client-s3": "^3.787.0",
40
- "@google-cloud/storage": "^7.16.0",
41
- "axios": "^1.6.0",
42
- "bcrypt": "^5.1.1",
43
- "cron-parser": "^4.9.0",
44
- "express": "^4.18.2",
45
- "jsonwebtoken": "^9.0.2",
46
- "mongodb": "^6.3.0",
47
- "multer": "^1.4.5-lts.1",
48
- "node-cron": "^3.0.3",
49
- "nodemailer": "^6.10.0",
50
- "swagger-jsdoc": "^6.2.8",
51
- "swagger-ui-express": "^5.0.0",
52
- "winston": "^3.17.0"
53
- },
54
- "devDependencies": {
55
- "@babel/cli": "^7.23.9",
56
- "@babel/core": "^7.24.0",
57
- "@babel/preset-env": "^7.24.0",
58
- "@types/express": "^4.17.21",
59
- "@types/swagger-jsdoc": "^6.0.4",
60
- "@types/swagger-ui-express": "^4.1.6",
61
- "eslint": "^8.57.0",
62
- "jest": "^29.7.0",
63
- "typescript": "^5.3.3"
64
- },
65
- "peerDependencies": {
66
- "express": "^4.18.2"
67
- },
68
- "engines": {
69
- "node": ">=14.0.0"
70
- }
71
- }
1
+ {
2
+ "name": "powr-sdk-api",
3
+ "version": "4.3.0",
4
+ "description": "Shared API core library for PowrStack projects",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
11
+ "scripts": {
12
+ "test": "jest --passWithNoTests",
13
+ "lint": "eslint .",
14
+ "build": "babel src -d dist",
15
+ "prepare": "npm run build",
16
+ "prepublishOnly": "npm run test"
17
+ },
18
+ "keywords": [
19
+ "api",
20
+ "express",
21
+ "middleware",
22
+ "error-handling",
23
+ "response-formatting",
24
+ "logging",
25
+ "swagger",
26
+ "documentation"
27
+ ],
28
+ "author": "Lawazia Tech",
29
+ "license": "ISC",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/powrstack/powr-sdk-api.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/powrstack/powr-sdk-api/issues"
36
+ },
37
+ "homepage": "https://github.com/powrstack/powr-sdk-api#readme",
38
+ "dependencies": {
39
+ "@aws-sdk/client-s3": "^3.787.0",
40
+ "@google-cloud/storage": "^7.16.0",
41
+ "axios": "^1.6.0",
42
+ "bcrypt": "^5.1.1",
43
+ "cron-parser": "^4.9.0",
44
+ "express": "^4.18.2",
45
+ "jsonwebtoken": "^9.0.2",
46
+ "mongodb": "^6.3.0",
47
+ "multer": "^1.4.5-lts.1",
48
+ "nodemailer": "^6.10.0",
49
+ "swagger-jsdoc": "^6.2.8",
50
+ "winston": "^3.17.0"
51
+ },
52
+ "devDependencies": {
53
+ "@babel/cli": "^7.23.9",
54
+ "@babel/core": "^7.24.0",
55
+ "@babel/preset-env": "^7.24.0",
56
+ "@types/express": "^4.17.21",
57
+ "@types/swagger-jsdoc": "^6.0.4",
58
+ "@types/swagger-ui-express": "^4.1.6",
59
+ "eslint": "^8.57.0",
60
+ "jest": "^29.7.0",
61
+ "typescript": "^5.3.3"
62
+ },
63
+ "peerDependencies": {
64
+ "express": "^4.18.2"
65
+ },
66
+ "engines": {
67
+ "node": ">=14.0.0"
68
+ }
69
+ }
@@ -1,81 +0,0 @@
1
- "use strict";
2
-
3
- const express = require("express");
4
- const router = express.Router();
5
- const {
6
- getDb
7
- } = require("../services/mongo");
8
-
9
- // Get activities
10
- router.get("/", async (req, res) => {
11
- const {
12
- projectId,
13
- contentId
14
- } = req.query;
15
- try {
16
- const query = {
17
- projectId
18
- };
19
- if (contentId) {
20
- query.contentId = contentId;
21
- }
22
- console.log("Filter.....:", query);
23
- const activities = await getDb().collection("activities").find(query).toArray();
24
- if (!activities || activities.length === 0) {
25
- console.log("Data not found.");
26
- return res.status(200).json({
27
- success: true,
28
- activities: []
29
- });
30
- }
31
- return res.status(200).json({
32
- success: true,
33
- activities: activities
34
- });
35
- } catch (error) {
36
- console.error("Error retrieving activities:", error);
37
- return res.status(500).json({
38
- success: false,
39
- message: "Failed to retrieve activities."
40
- });
41
- }
42
- });
43
-
44
- // Add activity
45
- router.post("/", async (req, res) => {
46
- const {
47
- projectId,
48
- contentId,
49
- activity
50
- } = req.body;
51
- const activityData = {
52
- projectId,
53
- contentId,
54
- activity,
55
- createdAt: new Date()
56
- };
57
- try {
58
- const result = await getDb().collection("activities").insertOne(activityData);
59
- if (result && result.insertedId) {
60
- console.log("Activity added with ID:", result.insertedId);
61
- return res.status(200).json({
62
- success: true,
63
- message: "Activity added successfully.",
64
- activityId: result.insertedId
65
- });
66
- } else {
67
- console.error("Insert operation failed. Result:", result);
68
- return res.status(500).json({
69
- success: false,
70
- message: "Activity could not be added."
71
- });
72
- }
73
- } catch (error) {
74
- console.error("Error adding activity:", error);
75
- return res.status(500).json({
76
- success: false,
77
- message: "Failed to add activity due to a server error."
78
- });
79
- }
80
- });
81
- module.exports = router;