powr-sdk-api 4.2.0 → 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 → managers}/tasks.js +123 -143
  5. package/dist/{services → managers}/tools.js +1 -1
  6. package/dist/routes/functions.js +3 -1
  7. package/dist/routes/index.js +2 -17
  8. package/dist/routes/tasks.js +184 -63
  9. package/dist/routes/tools.js +21 -8
  10. package/package.json +69 -71
  11. package/dist/admin/activities.js +0 -81
  12. package/dist/admin/auth.js +0 -234
  13. package/dist/admin/blogs.js +0 -94
  14. package/dist/admin/comments.js +0 -83
  15. package/dist/admin/files.js +0 -56
  16. package/dist/admin/forms.js +0 -242
  17. package/dist/admin/index.js +0 -53
  18. package/dist/admin/invoice.js +0 -163
  19. package/dist/admin/likes.js +0 -126
  20. package/dist/admin/notifications.js +0 -93
  21. package/dist/admin/plexx.js +0 -53
  22. package/dist/admin/ratings.js +0 -189
  23. package/dist/admin/routes.js +0 -132
  24. package/dist/admin/slides.js +0 -101
  25. package/dist/admin/users.js +0 -175
  26. package/dist/admin/waitlists.js +0 -94
  27. package/dist/logger/gcs.js +0 -78
  28. package/dist/logger/s3.js +0 -78
  29. package/dist/middleware/auth.js +0 -76
  30. package/dist/middleware/logger.js +0 -45
  31. package/dist/middleware/response.js +0 -53
  32. package/dist/routes/admin/index.js +0 -520
  33. package/dist/routes/blogs.js +0 -94
  34. package/dist/routes/invoice.js +0 -167
  35. package/dist/routes/plexx.js +0 -269
  36. package/dist/routes/routes.js +0 -143
  37. package/dist/routes/scheduledTasks.js +0 -240
  38. package/dist/scripts/update-tools-schema.js +0 -157
  39. package/dist/services/dbService.js +0 -42
  40. package/dist/services/logger.js +0 -35
  41. package/dist/services/plexx.js +0 -229
  42. package/dist/services/scheduledTasks.js +0 -459
  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
@@ -1,76 +0,0 @@
1
- "use strict";
2
-
3
- const jwt = require("jsonwebtoken");
4
- const generateToken = user => {
5
- return jwt.sign({
6
- userId: user.userId
7
- }, process.env.JWT_SECRET, {
8
- expiresIn: process.env.JWT_EXPIRES_IN || '24h'
9
- });
10
- };
11
- const validateToken = async (req, res, next) => {
12
- try {
13
- let token;
14
-
15
- // Check if token exists in headers
16
- if (req.headers.authorization && req.headers.authorization.startsWith("Bearer")) {
17
- token = req.headers.authorization.split(" ")[1];
18
- }
19
- if (!token) {
20
- return res.error("Not authorized to access this route", 401);
21
- }
22
- try {
23
- // Verify token
24
- const decoded = jwt.verify(token, process.env.JWT_SECRET);
25
-
26
- // // Get user from token
27
- // const user = await User.findByPk(decoded.userId);
28
- // if (!user) {
29
- // return res.error('User not found', 401);
30
- // }
31
-
32
- // // Add user and userId to request object
33
- // req.user = user;
34
- req.userId = decoded.userId;
35
- next();
36
- } catch (error) {
37
- return res.error("Not authorized to access this route", 401, error);
38
- }
39
- } catch (error) {
40
- return res.error("Error authenticating user", 500, error);
41
- }
42
- };
43
- const validateAuth = (options = {}) => {
44
- const {
45
- publicPaths = ["/auth", "/", "/status", "/swagger"],
46
- publicMethods = ["OPTIONS"]
47
- } = options;
48
- return async (req, res, next) => {
49
- // Skip auth validation for public paths
50
- if (publicPaths.some(path => req.path.startsWith(path))) {
51
- return next();
52
- }
53
-
54
- // Skip auth validation for public methods
55
- if (publicMethods.includes(req.method)) {
56
- return next();
57
- }
58
-
59
- // Use the original validateToken middleware for protected routes
60
- return validateToken(req, res, next);
61
- };
62
- };
63
-
64
- // exports.authorize = (...roles) => {
65
- // return (req, res, next) => {
66
- // if (!roles.includes(req.user.role)) {
67
- // return res.error(`User role ${req.user.role} is not authorized to access this route`, 403);
68
- // }
69
- // next();
70
- // };
71
- // };
72
-
73
- module.exports = {
74
- validateAuth,
75
- generateToken
76
- };
@@ -1,45 +0,0 @@
1
- "use strict";
2
-
3
- /**
4
- * Logging middleware for Express applications
5
- */
6
-
7
- const logger = (req, res, next) => {
8
- const startTime = Date.now();
9
-
10
- // Log request with redacted sensitive information
11
- try {
12
- const logBody = {
13
- ...req.body
14
- };
15
- if (logBody.password) logBody.password = "[REDACTED]";
16
- if (logBody.token) logBody.token = "[REDACTED]";
17
- console.log('Request:', {
18
- timestamp: new Date().toISOString(),
19
- method: req.method,
20
- url: req.originalUrl,
21
- ip: req.ip,
22
- userAgent: req.get('user-agent'),
23
- body: logBody,
24
- query: req.query
25
- });
26
- } catch (error) {
27
- console.error("Error logging request:", error);
28
- }
29
-
30
- // Log response with timing information
31
- res.on('finish', () => {
32
- const responseTime = Date.now() - startTime;
33
- console.log('Response:', {
34
- timestamp: new Date().toISOString(),
35
- method: req.method,
36
- url: req.originalUrl,
37
- statusCode: res.statusCode,
38
- responseTime: `${responseTime}ms`,
39
- userAgent: req.get('user-agent'),
40
- ip: req.ip
41
- });
42
- });
43
- next();
44
- };
45
- module.exports = logger;
@@ -1,53 +0,0 @@
1
- "use strict";
2
-
3
- const logger = require('../services/logger');
4
- const responseFormatter = (req, res, next) => {
5
- // Add success method to response object
6
- res.success = function (message = 'Success', statusCode = 200, data = null) {
7
- const response = {
8
- success: true,
9
- message
10
- };
11
-
12
- // Only include data if it's not null
13
- if (data !== null) {
14
- response.data = data;
15
- }
16
- return this.status(statusCode).json(response);
17
- };
18
-
19
- // Add error method to response object
20
- res.error = function (message = 'Error', statusCode = 500, error = null) {
21
- const response = {
22
- success: false,
23
- message
24
- };
25
-
26
- // Only include details if they're not null
27
- if (error !== null) {
28
- response.error = error;
29
- }
30
- return this.status(statusCode).json(response);
31
- };
32
- next();
33
- };
34
- const responseLogger = (req, res, next) => {
35
- const startTime = Date.now();
36
- res.on('finish', () => {
37
- const responseTime = Date.now() - startTime;
38
- logger.info('API Response', {
39
- method: req.method,
40
- url: req.originalUrl,
41
- statusCode: res.statusCode,
42
- responseTime: `${responseTime}ms`,
43
- userAgent: req.get('user-agent'),
44
- ip: req.ip,
45
- response: res.locals.responseBody || '[not captured]'
46
- });
47
- });
48
- next();
49
- };
50
- module.exports = {
51
- responseFormatter,
52
- responseLogger
53
- };
@@ -1,520 +0,0 @@
1
- "use strict";
2
-
3
- // Admin routes for powr-sdk-api
4
- // This file contains the admin route handlers for common admin operations
5
-
6
- const {
7
- MongoClient
8
- } = require('mongodb');
9
- const createAdminRoutes = (app, options = {}) => {
10
- const {
11
- projectId,
12
- apiUrl = '/admin',
13
- authMiddleware = null,
14
- logger = console,
15
- mongoUrl = process.env.MONGODB_URI || 'mongodb://localhost:27017/powrbase'
16
- } = options;
17
- let db;
18
-
19
- // Connect to MongoDB
20
- const connectDB = async () => {
21
- try {
22
- const client = new MongoClient(mongoUrl);
23
- await client.connect();
24
- db = client.db();
25
- logger.info('Connected to MongoDB');
26
- } catch (error) {
27
- logger.error('MongoDB connection error:', error);
28
- }
29
- };
30
-
31
- // Initialize database connection
32
- connectDB();
33
-
34
- // Forms Admin Routes
35
- app.get(`${apiUrl}/forms`, async (req, res) => {
36
- try {
37
- const forms = await db.collection('forms').find({
38
- projectId
39
- }).toArray();
40
- res.json({
41
- success: true,
42
- forms
43
- });
44
- } catch (error) {
45
- logger.error('Error fetching forms:', error);
46
- res.status(500).json({
47
- success: false,
48
- error: 'Failed to fetch forms'
49
- });
50
- }
51
- });
52
- app.post(`${apiUrl}/forms`, async (req, res) => {
53
- try {
54
- const {
55
- name,
56
- fields,
57
- settings
58
- } = req.body;
59
- const form = {
60
- projectId,
61
- name,
62
- fields,
63
- settings,
64
- createdAt: new Date(),
65
- updatedAt: new Date()
66
- };
67
- const result = await db.collection('forms').insertOne(form);
68
- res.json({
69
- success: true,
70
- form: {
71
- ...form,
72
- _id: result.insertedId
73
- }
74
- });
75
- } catch (error) {
76
- logger.error('Error creating form:', error);
77
- res.status(500).json({
78
- success: false,
79
- error: 'Failed to create form'
80
- });
81
- }
82
- });
83
- app.put(`${apiUrl}/forms/:id`, async (req, res) => {
84
- try {
85
- const {
86
- id
87
- } = req.params;
88
- const {
89
- name,
90
- fields,
91
- settings
92
- } = req.body;
93
- const result = await db.collection('forms').updateOne({
94
- _id: id,
95
- projectId
96
- }, {
97
- $set: {
98
- name,
99
- fields,
100
- settings,
101
- updatedAt: new Date()
102
- }
103
- });
104
- if (result.matchedCount === 0) {
105
- return res.status(404).json({
106
- success: false,
107
- error: 'Form not found'
108
- });
109
- }
110
- res.json({
111
- success: true,
112
- message: 'Form updated successfully'
113
- });
114
- } catch (error) {
115
- logger.error('Error updating form:', error);
116
- res.status(500).json({
117
- success: false,
118
- error: 'Failed to update form'
119
- });
120
- }
121
- });
122
- app.delete(`${apiUrl}/forms/:id`, async (req, res) => {
123
- try {
124
- const {
125
- id
126
- } = req.params;
127
- const result = await db.collection('forms').deleteOne({
128
- _id: id,
129
- projectId
130
- });
131
- if (result.deletedCount === 0) {
132
- return res.status(404).json({
133
- success: false,
134
- error: 'Form not found'
135
- });
136
- }
137
- res.json({
138
- success: true,
139
- message: 'Form deleted successfully'
140
- });
141
- } catch (error) {
142
- logger.error('Error deleting form:', error);
143
- res.status(500).json({
144
- success: false,
145
- error: 'Failed to delete form'
146
- });
147
- }
148
- });
149
-
150
- // Waitlists Admin Routes
151
- app.get(`${apiUrl}/waitlists`, async (req, res) => {
152
- try {
153
- const waitlists = await db.collection('waitlists').find({
154
- projectId
155
- }).toArray();
156
- res.json({
157
- success: true,
158
- waitlists
159
- });
160
- } catch (error) {
161
- logger.error('Error fetching waitlists:', error);
162
- res.status(500).json({
163
- success: false,
164
- error: 'Failed to fetch waitlists'
165
- });
166
- }
167
- });
168
- app.post(`${apiUrl}/waitlists`, async (req, res) => {
169
- try {
170
- const {
171
- email,
172
- name,
173
- phone,
174
- additionalInfo
175
- } = req.body;
176
- const entry = {
177
- projectId,
178
- email,
179
- name,
180
- phone,
181
- additionalInfo,
182
- createdAt: new Date()
183
- };
184
- const result = await db.collection('waitlists').insertOne(entry);
185
- res.json({
186
- success: true,
187
- entry: {
188
- ...entry,
189
- _id: result.insertedId
190
- }
191
- });
192
- } catch (error) {
193
- logger.error('Error adding waitlist entry:', error);
194
- res.status(500).json({
195
- success: false,
196
- error: 'Failed to add entry'
197
- });
198
- }
199
- });
200
-
201
- // Slides Admin Routes
202
- app.get(`${apiUrl}/slides`, async (req, res) => {
203
- try {
204
- const slides = await db.collection('slides').find({
205
- projectId
206
- }).toArray();
207
- res.json({
208
- success: true,
209
- slides
210
- });
211
- } catch (error) {
212
- logger.error('Error fetching slides:', error);
213
- res.status(500).json({
214
- success: false,
215
- error: 'Failed to fetch slides'
216
- });
217
- }
218
- });
219
- app.post(`${apiUrl}/slides`, async (req, res) => {
220
- try {
221
- const {
222
- title,
223
- description,
224
- imageUrl,
225
- link,
226
- order
227
- } = req.body;
228
- const slide = {
229
- projectId,
230
- title,
231
- description,
232
- imageUrl,
233
- link,
234
- order: order || 0,
235
- createdAt: new Date(),
236
- updatedAt: new Date()
237
- };
238
- const result = await db.collection('slides').insertOne(slide);
239
- res.json({
240
- success: true,
241
- slide: {
242
- ...slide,
243
- _id: result.insertedId
244
- }
245
- });
246
- } catch (error) {
247
- logger.error('Error creating slide:', error);
248
- res.status(500).json({
249
- success: false,
250
- error: 'Failed to create slide'
251
- });
252
- }
253
- });
254
- app.put(`${apiUrl}/slides/:id`, async (req, res) => {
255
- try {
256
- const {
257
- id
258
- } = req.params;
259
- const {
260
- title,
261
- description,
262
- imageUrl,
263
- link,
264
- order
265
- } = req.body;
266
- const result = await db.collection('slides').updateOne({
267
- _id: id,
268
- projectId
269
- }, {
270
- $set: {
271
- title,
272
- description,
273
- imageUrl,
274
- link,
275
- order,
276
- updatedAt: new Date()
277
- }
278
- });
279
- if (result.matchedCount === 0) {
280
- return res.status(404).json({
281
- success: false,
282
- error: 'Slide not found'
283
- });
284
- }
285
- res.json({
286
- success: true,
287
- message: 'Slide updated successfully'
288
- });
289
- } catch (error) {
290
- logger.error('Error updating slide:', error);
291
- res.status(500).json({
292
- success: false,
293
- error: 'Failed to update slide'
294
- });
295
- }
296
- });
297
- app.delete(`${apiUrl}/slides/:id`, async (req, res) => {
298
- try {
299
- const {
300
- id
301
- } = req.params;
302
- const result = await db.collection('slides').deleteOne({
303
- _id: id,
304
- projectId
305
- });
306
- if (result.deletedCount === 0) {
307
- return res.status(404).json({
308
- success: false,
309
- error: 'Slide not found'
310
- });
311
- }
312
- res.json({
313
- success: true,
314
- message: 'Slide deleted successfully'
315
- });
316
- } catch (error) {
317
- logger.error('Error deleting slide:', error);
318
- res.status(500).json({
319
- success: false,
320
- error: 'Failed to delete slide'
321
- });
322
- }
323
- });
324
-
325
- // Invoices Admin Routes
326
- app.get(`${apiUrl}/invoices`, async (req, res) => {
327
- try {
328
- const invoices = await db.collection('invoices').find({
329
- projectId
330
- }).toArray();
331
- res.json({
332
- success: true,
333
- invoices
334
- });
335
- } catch (error) {
336
- logger.error('Error fetching invoices:', error);
337
- res.status(500).json({
338
- success: false,
339
- error: 'Failed to fetch invoices'
340
- });
341
- }
342
- });
343
- app.post(`${apiUrl}/invoices`, async (req, res) => {
344
- try {
345
- const {
346
- invoiceNumber,
347
- clientName,
348
- clientEmail,
349
- amount,
350
- description,
351
- dueDate,
352
- status = 'pending'
353
- } = req.body;
354
- const invoice = {
355
- projectId,
356
- invoiceNumber,
357
- clientName,
358
- clientEmail,
359
- amount,
360
- description,
361
- dueDate: new Date(dueDate),
362
- status,
363
- createdAt: new Date(),
364
- updatedAt: new Date()
365
- };
366
- const result = await db.collection('invoices').insertOne(invoice);
367
- res.json({
368
- success: true,
369
- invoice: {
370
- ...invoice,
371
- _id: result.insertedId
372
- }
373
- });
374
- } catch (error) {
375
- logger.error('Error creating invoice:', error);
376
- res.status(500).json({
377
- success: false,
378
- error: 'Failed to create invoice'
379
- });
380
- }
381
- });
382
- app.put(`${apiUrl}/invoices/:id`, async (req, res) => {
383
- try {
384
- const {
385
- id
386
- } = req.params;
387
- const {
388
- invoiceNumber,
389
- clientName,
390
- clientEmail,
391
- amount,
392
- description,
393
- dueDate,
394
- status
395
- } = req.body;
396
- const result = await db.collection('invoices').updateOne({
397
- _id: id,
398
- projectId
399
- }, {
400
- $set: {
401
- invoiceNumber,
402
- clientName,
403
- clientEmail,
404
- amount,
405
- description,
406
- dueDate: new Date(dueDate),
407
- status,
408
- updatedAt: new Date()
409
- }
410
- });
411
- if (result.matchedCount === 0) {
412
- return res.status(404).json({
413
- success: false,
414
- error: 'Invoice not found'
415
- });
416
- }
417
- res.json({
418
- success: true,
419
- message: 'Invoice updated successfully'
420
- });
421
- } catch (error) {
422
- logger.error('Error updating invoice:', error);
423
- res.status(500).json({
424
- success: false,
425
- error: 'Failed to update invoice'
426
- });
427
- }
428
- });
429
-
430
- // Notifications Admin Routes
431
- app.get(`${apiUrl}/notifications`, async (req, res) => {
432
- try {
433
- const notifications = await db.collection('notifications').find({
434
- projectId
435
- }).toArray();
436
- res.json({
437
- success: true,
438
- notifications
439
- });
440
- } catch (error) {
441
- logger.error('Error fetching notifications:', error);
442
- res.status(500).json({
443
- success: false,
444
- error: 'Failed to fetch notifications'
445
- });
446
- }
447
- });
448
- app.post(`${apiUrl}/notifications`, async (req, res) => {
449
- try {
450
- const {
451
- title,
452
- message,
453
- type = 'info',
454
- priority = 'normal',
455
- targetUsers = 'all'
456
- } = req.body;
457
- const notification = {
458
- projectId,
459
- title,
460
- message,
461
- type,
462
- priority,
463
- targetUsers,
464
- isRead: false,
465
- createdAt: new Date()
466
- };
467
- const result = await db.collection('notifications').insertOne(notification);
468
- res.json({
469
- success: true,
470
- notification: {
471
- ...notification,
472
- _id: result.insertedId
473
- }
474
- });
475
- } catch (error) {
476
- logger.error('Error creating notification:', error);
477
- res.status(500).json({
478
- success: false,
479
- error: 'Failed to create notification'
480
- });
481
- }
482
- });
483
- app.put(`${apiUrl}/notifications/:id/read`, async (req, res) => {
484
- try {
485
- const {
486
- id
487
- } = req.params;
488
- const result = await db.collection('notifications').updateOne({
489
- _id: id,
490
- projectId
491
- }, {
492
- $set: {
493
- isRead: true,
494
- updatedAt: new Date()
495
- }
496
- });
497
- if (result.matchedCount === 0) {
498
- return res.status(404).json({
499
- success: false,
500
- error: 'Notification not found'
501
- });
502
- }
503
- res.json({
504
- success: true,
505
- message: 'Notification marked as read'
506
- });
507
- } catch (error) {
508
- logger.error('Error updating notification:', error);
509
- res.status(500).json({
510
- success: false,
511
- error: 'Failed to update notification'
512
- });
513
- }
514
- });
515
- logger.info('Admin routes initialized for project:', projectId);
516
- return app;
517
- };
518
- module.exports = {
519
- createAdminRoutes
520
- };