powr-sdk-api 2.0.2 → 2.0.4

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.
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+
3
+ const express = require("express");
4
+ const router = express.Router();
5
+ const multer = require('multer');
6
+ const path = require('path');
7
+ const upload = multer({
8
+ storage: multer.memoryStorage(),
9
+ fileFilter: function (req, file, cb) {
10
+ if (file.mimetype === 'application/json' || path.extname(file.originalname).toLowerCase() === '.json') {
11
+ cb(null, true);
12
+ } else {
13
+ cb(new Error('Only JSON files are allowed!'), false);
14
+ }
15
+ },
16
+ limits: {
17
+ fileSize: 5 * 1024 * 1024
18
+ }
19
+ });
20
+
21
+ // GET all powrForm data based on projectId
22
+ router.get("/powrform", async (req, res) => {
23
+ const projectId = req.query.projectId;
24
+ try {
25
+ const collection = req.db.collection("powrForm");
26
+ const forms = await collection.find({
27
+ projectId
28
+ }).toArray();
29
+ res.status(200).json({
30
+ success: true,
31
+ message: "Fetching powrForm data",
32
+ count: forms.length,
33
+ data: forms
34
+ });
35
+ } catch (error) {
36
+ res.status(500).json({
37
+ success: false,
38
+ message: "Internal server error",
39
+ error: error.message
40
+ });
41
+ }
42
+ });
43
+
44
+ // GET specific form by formName and projectId
45
+ router.get('/:formName', async (req, res) => {
46
+ try {
47
+ const {
48
+ formName
49
+ } = req.params;
50
+ const {
51
+ projectId
52
+ } = req.query;
53
+ if (!projectId) {
54
+ return res.status(400).json({
55
+ success: false,
56
+ message: 'projectId is required as query parameter'
57
+ });
58
+ }
59
+ const collection = req.db.collection("powrForm");
60
+ const formData = await collection.findOne({
61
+ formName,
62
+ projectId
63
+ });
64
+ if (!formData) {
65
+ return res.status(404).json({
66
+ success: false,
67
+ message: 'Form not found'
68
+ });
69
+ }
70
+ res.status(200).json({
71
+ success: true,
72
+ message: "Form fetched successfully",
73
+ data: formData
74
+ });
75
+ } catch (error) {
76
+ res.status(500).json({
77
+ success: false,
78
+ message: 'Error fetching form',
79
+ error: error.message
80
+ });
81
+ }
82
+ });
83
+
84
+ // POST /studentform
85
+ router.post('/studentform', async (req, res) => {
86
+ try {
87
+ const formData = req.body;
88
+ const {
89
+ projectId
90
+ } = req.query;
91
+ const {
92
+ whatsapp,
93
+ formName,
94
+ email,
95
+ ...otherFormFields
96
+ } = formData;
97
+ const studentsFormCollection = req.db.collection('studentsForm');
98
+ const existingForm = await studentsFormCollection.findOne({
99
+ whatsapp: whatsapp,
100
+ projectId: projectId,
101
+ email: email,
102
+ formName: formName
103
+ });
104
+ if (existingForm) {
105
+ return res.status(409).json({
106
+ message: 'You have already submitted this form. Only one submission per form type is allowed.',
107
+ existingFormId: existingForm._id,
108
+ submittedAt: existingForm.submittedAt
109
+ });
110
+ }
111
+ const studentFormData = {
112
+ whatsapp: whatsapp,
113
+ formName: formName,
114
+ projectId: projectId,
115
+ email: email,
116
+ ...otherFormFields,
117
+ submittedAt: new Date()
118
+ };
119
+ const formResult = await studentsFormCollection.insertOne(studentFormData);
120
+ res.status(201).json({
121
+ success: true,
122
+ message: 'Student form submitted successfully',
123
+ submittedFormId: formResult.insertedId,
124
+ data: studentFormData
125
+ });
126
+ } catch (error) {
127
+ res.status(500).json({
128
+ success: false,
129
+ message: 'Error submitting student form',
130
+ error: error.message
131
+ });
132
+ }
133
+ });
134
+
135
+ //get form
136
+ router.get('/getCount/:formName', async (req, res) => {
137
+ try {
138
+ const {
139
+ formName
140
+ } = req.params;
141
+ const {
142
+ projectId
143
+ } = req.query;
144
+ const studentsFormCollection = getDb().collection('studentsForm');
145
+
146
+ // Build query object
147
+ let query = {
148
+ formName
149
+ };
150
+ if (projectId) {
151
+ query.projectId = projectId;
152
+ }
153
+ const submissions = await studentsFormCollection.find(query).toArray();
154
+ res.status(200).json({
155
+ success: true,
156
+ message: 'Submission count retrieved successfully',
157
+ formName: formName,
158
+ projectId: projectId,
159
+ count: submissions.length,
160
+ data: submissions
161
+ });
162
+ } catch (error) {
163
+ res.status(500).json({
164
+ success: false,
165
+ message: 'Error counting form submissions',
166
+ error: error.message
167
+ });
168
+ }
169
+ });
170
+
171
+ // POST /create-form - Upload JSON file and store form data
172
+ router.post('/create-form', upload.single('jsonFile'), async (req, res) => {
173
+ try {
174
+ const {
175
+ projectId
176
+ } = req.query;
177
+ if (!projectId) {
178
+ return res.status(400).json({
179
+ success: false,
180
+ message: 'projectId is required as query parameter'
181
+ });
182
+ }
183
+ if (!req.file) {
184
+ return res.status(400).json({
185
+ success: false,
186
+ message: 'JSON file is required'
187
+ });
188
+ }
189
+ let formData;
190
+ try {
191
+ const fileContent = req.file.buffer.toString('utf8');
192
+ formData = JSON.parse(fileContent);
193
+ } catch (parseError) {
194
+ return res.status(400).json({
195
+ success: false,
196
+ message: 'Invalid JSON file format',
197
+ error: parseError.message
198
+ });
199
+ }
200
+ if (!formData.formName) {
201
+ return res.status(400).json({
202
+ success: false,
203
+ message: 'formName is required in JSON data'
204
+ });
205
+ }
206
+ const powrFormCollection = getDb().collection('powrForm');
207
+ const existingForm = await powrFormCollection.findOne({
208
+ formName: formData.formName,
209
+ projectId: projectId
210
+ });
211
+ if (existingForm) {
212
+ return res.status(409).json({
213
+ success: false,
214
+ message: 'Form with this name already exists for this project',
215
+ existingFormId: existingForm._id
216
+ });
217
+ }
218
+ const finalFormData = {
219
+ ...formData,
220
+ projectId: projectId,
221
+ createdAt: new Date()
222
+ };
223
+ const result = await powrFormCollection.insertOne(finalFormData);
224
+ res.status(201).json({
225
+ success: true,
226
+ message: 'Form created and stored successfully',
227
+ formId: result.insertedId,
228
+ formName: formData.formName,
229
+ projectId: projectId,
230
+ data: finalFormData
231
+ });
232
+ } catch (error) {
233
+ res.status(500).json({
234
+ success: false,
235
+ message: 'Error creating form',
236
+ error: error.message
237
+ });
238
+ }
239
+ });
240
+ module.exports = router;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ const express = require('express');
4
+
5
+ // Import all route modules
6
+ const commentsRoutes = require('./comments');
7
+ // const filesRoutes = require('./files'); // Commented out for now
8
+ const formsRoutes = require('./forms');
9
+ const invoiceRoutes = require('./invoice');
10
+ const likesRoutes = require('./likes');
11
+ const plexxRoutes = require('./plexx');
12
+ const ratingsRoutes = require('./ratings');
13
+ const usersRoutes = require('./users');
14
+ const waitlistsRoutes = require('./waitlists');
15
+ const activitiesRoutes = require('./activities');
16
+ const authRoutes = require('./auth');
17
+ const blogsRoutes = require('./blogs');
18
+ const slidesRoutes = require('./slides');
19
+ const notificationsRoutes = require('./notifications');
20
+ const createAdminRoutes = () => {
21
+ // Get config from environment variables
22
+
23
+ const router = express.Router();
24
+
25
+ // Mount all route modules
26
+ router.use('/comments', commentsRoutes);
27
+ // router.use('/files', filesRoutes); // Commented out for now
28
+ router.use('/forms', formsRoutes);
29
+ router.use('/invoice', invoiceRoutes);
30
+ router.use('/likes', likesRoutes);
31
+ router.use('/plexx', plexxRoutes);
32
+ router.use('/ratings', ratingsRoutes);
33
+ router.use('/users', usersRoutes);
34
+ router.use('/waitlists', waitlistsRoutes);
35
+ router.use('/activities', activitiesRoutes);
36
+ router.use('/auth', authRoutes);
37
+ router.use('/blogs', blogsRoutes);
38
+ router.use('/slides', slidesRoutes);
39
+ router.use('/notifications', notificationsRoutes);
40
+ return router;
41
+ };
42
+ module.exports = {
43
+ createAdminRoutes
44
+ };
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+
3
+ const express = require('express');
4
+ const router = express.Router();
5
+ const {
6
+ getDb
7
+ } = require("../services/mongo");
8
+ const {
9
+ ObjectId
10
+ } = require("mongodb");
11
+ router.get('/', async (req, res) => {
12
+ const {
13
+ projectId
14
+ } = req.query;
15
+ try {
16
+ const query = {
17
+ projectId
18
+ };
19
+ if (!projectId) {
20
+ return res.status(400).json({
21
+ success: false,
22
+ message: 'projectId is required.'
23
+ });
24
+ }
25
+ const invoiceData = await getDb().collection("invoices").find(query).toArray();
26
+ return res.status(200).json({
27
+ success: true,
28
+ invoices: invoiceData
29
+ });
30
+ } catch (error) {
31
+ console.error("Error retrieving invoices :", error);
32
+ return res.status(500).json({
33
+ success: false,
34
+ message: "Failed to retrieve invoices ."
35
+ });
36
+ }
37
+ });
38
+ router.get('/:invoiceId', async (req, res) => {
39
+ const {
40
+ invoiceId
41
+ } = req.params;
42
+ const {
43
+ projectId
44
+ } = req.query;
45
+ try {
46
+ if (!projectId) {
47
+ return res.status(400).json({
48
+ success: false,
49
+ message: 'projectId is required.'
50
+ });
51
+ }
52
+ const _id = new ObjectId(invoiceId);
53
+ const invoiceData = await getDb().collection("invoices").findOne({
54
+ _id,
55
+ projectId
56
+ });
57
+ return res.status(200).json({
58
+ success: true,
59
+ invoice: invoiceData
60
+ });
61
+ } catch (error) {
62
+ console.error("Error retrieving invoice :", error);
63
+ return res.status(500).json({
64
+ success: false,
65
+ message: "Failed to retrieve invoice."
66
+ });
67
+ }
68
+ });
69
+ router.post('/', async (req, res) => {
70
+ const {
71
+ projectId
72
+ } = req.query;
73
+ const {
74
+ invoiceNumber,
75
+ dateOfIssue,
76
+ paymentStatus,
77
+ customerInfo,
78
+ serviceSection,
79
+ paymentSection
80
+ } = req.body;
81
+ if (!projectId) {
82
+ return res.status(400).json({
83
+ success: false,
84
+ message: "projectId is required."
85
+ });
86
+ }
87
+ const invoice = {
88
+ invoiceNumber,
89
+ dateOfIssue,
90
+ paymentStatus,
91
+ customerInfo,
92
+ serviceSection,
93
+ paymentSection,
94
+ createdAt: new Date()
95
+ };
96
+ invoice.projectId = projectId;
97
+ try {
98
+ const result = await getDb().collection("invoices").insertOne(invoice);
99
+ if (result && result.insertedId) {
100
+ return res.status(201).json({
101
+ success: true,
102
+ message: "Invoice created successfully.",
103
+ invoiceId: result.insertedId,
104
+ invoice
105
+ });
106
+ } else {
107
+ return res.status(500).json({
108
+ success: false,
109
+ message: "Invoice could not be added."
110
+ });
111
+ }
112
+ } catch (error) {
113
+ console.error("Error adding invoice:", error);
114
+ return res.status(500).json({
115
+ success: false,
116
+ message: "Failed to add invoice due to a server error."
117
+ });
118
+ }
119
+ });
120
+ router.put('/:invoiceId', async (req, res) => {
121
+ const {
122
+ invoiceId
123
+ } = req.params;
124
+ const {
125
+ projectId
126
+ } = req.query;
127
+ if (!projectId) {
128
+ return res.status(400).json({
129
+ success: false,
130
+ message: "projectId is required."
131
+ });
132
+ }
133
+ let filter;
134
+ try {
135
+ filter = {
136
+ _id: new ObjectId(invoiceId),
137
+ projectId: projectId
138
+ };
139
+ } catch (e) {
140
+ return res.status(400).json({
141
+ success: false,
142
+ message: "Invalid invoiceId format."
143
+ });
144
+ }
145
+ const updateData = req.body;
146
+ try {
147
+ const result = await getDb().collection("invoices").updateOne(filter, {
148
+ $set: updateData
149
+ });
150
+ return res.status(200).json({
151
+ success: true,
152
+ message: "Invoice updated successfully.",
153
+ result: result
154
+ });
155
+ } catch (error) {
156
+ console.error("Error updating invoice:", error);
157
+ return res.status(500).json({
158
+ success: false,
159
+ message: "Failed to update invoice due to a server error."
160
+ });
161
+ }
162
+ });
163
+ module.exports = router;
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ const express = require('express');
4
+ const router = express.Router();
5
+ const {
6
+ getDb
7
+ } = require("../services/mongo");
8
+ const {
9
+ ObjectId
10
+ } = require("mongodb");
11
+
12
+ // Like/Unlike endpoint
13
+ router.post('/', async (req, res) => {
14
+ const {
15
+ liked
16
+ } = req.body;
17
+ const {
18
+ userId,
19
+ projectId,
20
+ contentId
21
+ } = req.query;
22
+ if (!userId || typeof liked !== 'boolean' || !projectId) {
23
+ return res.status(400).json({
24
+ success: false,
25
+ message: 'userId and projectId are required in query, liked (boolean) is required in body.'
26
+ });
27
+ }
28
+ try {
29
+ const query = {
30
+ userId: new ObjectId(userId),
31
+ projectId
32
+ };
33
+ const setObj = {
34
+ liked
35
+ };
36
+ if (contentId) {
37
+ query.contentId = contentId;
38
+ setObj.contentId = contentId;
39
+ }
40
+ const response = await getDb().collection('likes').updateOne(query, {
41
+ $set: setObj
42
+ }, {
43
+ upsert: true
44
+ });
45
+ return res.status(200).json({
46
+ success: true,
47
+ message: liked ? 'Liked.' : 'Unliked.',
48
+ response
49
+ });
50
+ } catch (error) {
51
+ console.error('Error updating like status:', error);
52
+ return res.status(500).json({
53
+ success: false,
54
+ message: 'Failed to update like status.'
55
+ });
56
+ }
57
+ });
58
+ router.get('/', async (req, res) => {
59
+ const {
60
+ projectId,
61
+ contentId
62
+ } = req.query;
63
+ if (!projectId) {
64
+ return res.status(400).json({
65
+ success: false,
66
+ message: 'projectId is required in query.'
67
+ });
68
+ }
69
+ try {
70
+ const query = {
71
+ projectId
72
+ };
73
+ if (contentId) {
74
+ query.contentId = contentId;
75
+ }
76
+ const likes = await getDb().collection('likes').find(query).toArray();
77
+ const likesCount = likes.filter(like => like.liked === true).length;
78
+ const unlikesCount = likes.filter(like => like.liked === false).length;
79
+ return res.status(200).json({
80
+ success: true,
81
+ likes,
82
+ likesCount,
83
+ unlikesCount
84
+ });
85
+ } catch (error) {
86
+ console.error('Error fetching likes:', error);
87
+ return res.status(500).json({
88
+ success: false,
89
+ message: 'Failed to fetch likes.'
90
+ });
91
+ }
92
+ });
93
+ router.get('/:likeId', async (req, res) => {
94
+ const {
95
+ likeId
96
+ } = req.params;
97
+ const {
98
+ projectId,
99
+ contentId
100
+ } = req.query;
101
+ try {
102
+ const filter = {
103
+ userId: new ObjectId(likeId)
104
+ };
105
+ if (projectId) filter.projectId = projectId;
106
+ if (contentId) filter.contentId = contentId;
107
+ const like = await getDb().collection('likes').findOne(filter);
108
+ if (!like) {
109
+ return res.status(404).json({
110
+ success: false,
111
+ message: 'Like not found.'
112
+ });
113
+ }
114
+ return res.status(200).json({
115
+ success: true,
116
+ like
117
+ });
118
+ } catch (error) {
119
+ console.error('Error fetching like by id:', error);
120
+ return res.status(500).json({
121
+ success: false,
122
+ message: 'Failed to fetch like.'
123
+ });
124
+ }
125
+ });
126
+ module.exports = router;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+
3
+ const express = require('express');
4
+ const router = express.Router();
5
+ const {
6
+ getDb
7
+ } = require('../services/mongo');
8
+ // const tokenService = require('../services/tokenService');
9
+
10
+ // Get notifications
11
+ router.get('/', async (req, res) => {
12
+ const {
13
+ projectId
14
+ } = req.query;
15
+ try {
16
+ const query = {
17
+ projectId
18
+ };
19
+ console.log("Filter.....:", query);
20
+ const notificationData = await getDb().collection('notifications').find(query).toArray();
21
+ if (!notificationData || notificationData.length === 0) {
22
+ console.log("Data not found.");
23
+ return res.status(200).json({
24
+ success: true,
25
+ notification: []
26
+ });
27
+ }
28
+ const sortedData = notificationData.sort((a, b) => new Date(b === null || b === void 0 ? void 0 : b.createdAt) - new Date(a === null || a === void 0 ? void 0 : a.createdAt));
29
+ console.log("Notifications retrieved and sorted:", sortedData);
30
+ return res.status(200).json({
31
+ success: true,
32
+ notification: sortedData
33
+ });
34
+ } catch (error) {
35
+ console.error("Error retrieving notifications:", error);
36
+ return res.status(500).json({
37
+ success: false,
38
+ message: "Failed to retrieve notifications."
39
+ });
40
+ }
41
+ });
42
+
43
+ // Send notification
44
+ router.post('/', async (req, res) => {
45
+ const {
46
+ projectId,
47
+ title,
48
+ description,
49
+ filter,
50
+ imgurl
51
+ } = req.body;
52
+ if (typeof filter !== 'object' || Array.isArray(filter) || filter === null) {
53
+ return res.status(400).json({
54
+ success: false,
55
+ message: "Filter should be a valid object."
56
+ });
57
+ }
58
+ const notificationData = {
59
+ projectId,
60
+ filter,
61
+ imgurl,
62
+ title,
63
+ description,
64
+ createdAt: new Date()
65
+ };
66
+ try {
67
+ const result = await getDb().collection('notifications').insertOne(notificationData);
68
+ if (result && result.insertedId) {
69
+ console.log("Notification added with ID:", result.insertedId);
70
+
71
+ // await tokenService.sendNotificationsToProjectUsers(projectId, notificationData);
72
+
73
+ return res.status(200).json({
74
+ success: true,
75
+ message: "Notification added and sent successfully.",
76
+ notificationId: result.insertedId
77
+ });
78
+ } else {
79
+ console.error("Insert operation failed. Result:", result);
80
+ return res.status(500).json({
81
+ success: false,
82
+ message: "Notification could not be added."
83
+ });
84
+ }
85
+ } catch (error) {
86
+ console.error("Error adding notification:", error);
87
+ return res.status(500).json({
88
+ success: false,
89
+ message: "Failed to add notification due to a server error."
90
+ });
91
+ }
92
+ });
93
+ module.exports = router;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ const express = require("express");
4
+ const router = express.Router();
5
+ const {
6
+ getDb
7
+ } = require("../services/mongo");
8
+ const data = [{
9
+ age: 12
10
+ }, {
11
+ age: 45
12
+ }];
13
+ router.get("/:route", async (req, res) => {
14
+ const {
15
+ route
16
+ } = req.params;
17
+ const {
18
+ name,
19
+ projectId
20
+ } = req.query;
21
+ if (!projectId) {
22
+ return res.status(400).json({
23
+ success: false,
24
+ message: 'projectId is required.'
25
+ });
26
+ }
27
+ const logicEntry = await getDb().collection("routes").findOne({
28
+ route,
29
+ projectId
30
+ });
31
+ if (!logicEntry) {
32
+ return res.status(404).json({
33
+ success: false,
34
+ message: "Logic not found"
35
+ });
36
+ }
37
+ const codeString = logicEntry.code;
38
+ try {
39
+ const logicFunction = new Function("age", codeString);
40
+ const status = logicFunction(name);
41
+ return res.status(200).json({
42
+ success: true,
43
+ data: status
44
+ });
45
+ } catch (err) {
46
+ console.error("Error executing logic:", err);
47
+ return res.status(500).json({
48
+ success: false,
49
+ message: "Error executing logic"
50
+ });
51
+ }
52
+ });
53
+ module.exports = router;