powr-sdk-api 2.1.1 → 2.2.2

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,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;
@@ -0,0 +1,189 @@
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
+ //add ratings
13
+ router.post("/", async (req, res) => {
14
+ const {
15
+ itemId,
16
+ rating,
17
+ review,
18
+ userId
19
+ } = req.body;
20
+ const projectId = req.projectId;
21
+ if (!rating || !review) {
22
+ return res.status(400).json({
23
+ success: false,
24
+ message: "All fields are required."
25
+ });
26
+ }
27
+ const ratingData = {
28
+ projectId,
29
+ itemId,
30
+ rating,
31
+ review,
32
+ userId,
33
+ createdAt: new Date()
34
+ };
35
+ try {
36
+ const db = await getDb();
37
+ const result = await db.collection("ratings").insertOne(ratingData);
38
+ if (result && result.insertedId) {
39
+ console.log("ratings added with ID:", result.insertedId);
40
+ return res.status(200).json({
41
+ success: true,
42
+ message: "rating added successfully.",
43
+ ratingId: result.insertedId
44
+ });
45
+ } else {
46
+ console.error("Insert operation failed. Result:", result);
47
+ return res.status(500).json({
48
+ success: false,
49
+ message: "ratings/reviews could not be added."
50
+ });
51
+ }
52
+ } catch (error) {
53
+ console.error("Error adding ratings:", error);
54
+ return res.status(500).json({
55
+ success: false,
56
+ message: "Failed to add ratings/reviews due to a server error."
57
+ });
58
+ }
59
+ });
60
+
61
+ //get ratings
62
+ router.get("/", async (req, res) => {
63
+ const {
64
+ itemId,
65
+ userId
66
+ } = req.query;
67
+ const projectId = req.projectId;
68
+ if (!projectId) {
69
+ return res.status(400).json({
70
+ success: false,
71
+ message: "projectId is required."
72
+ });
73
+ }
74
+ try {
75
+ const query = {
76
+ projectId
77
+ };
78
+ if (itemId) query.itemId = itemId;
79
+ if (userId) query.userId = userId;
80
+ console.log("Filter....:", query);
81
+ const db = await getDb();
82
+ const ratingData = await db.collection("ratings").find(query).toArray();
83
+ if (!ratingData || ratingData.length === 0) {
84
+ console.log("Data not found.");
85
+ return res.status(200).json({
86
+ success: true,
87
+ ratings: []
88
+ });
89
+ }
90
+ return res.status(200).json({
91
+ success: true,
92
+ ratings: ratingData
93
+ });
94
+ } catch (error) {
95
+ console.error("Error retrieving ratings:", error);
96
+ return res.status(500).json({
97
+ success: false,
98
+ message: "Failed to retrieve ratings."
99
+ });
100
+ }
101
+ });
102
+
103
+ // delete ratings
104
+ router.delete("/", async (req, res) => {
105
+ const {
106
+ ratingId
107
+ } = req.query;
108
+ const projectId = req.projectId;
109
+ if (!ratingId || !projectId) {
110
+ return res.status(400).json({
111
+ success: false,
112
+ message: "Both ratingId and projectId are required to delete a rating."
113
+ });
114
+ }
115
+ try {
116
+ const db = await getDb();
117
+ const result = await db.collection("ratings").deleteOne({
118
+ _id: new ObjectId(ratingId),
119
+ projectId: projectId
120
+ });
121
+ if (result.deletedCount === 0) {
122
+ return res.status(404).json({
123
+ success: false,
124
+ message: "Rating not found for the given ratingId and projectId."
125
+ });
126
+ }
127
+ return res.status(200).json({
128
+ success: true,
129
+ message: "Rating deleted successfully."
130
+ });
131
+ } catch (error) {
132
+ console.error("Error deleting rating:", error);
133
+ return res.status(500).json({
134
+ success: false,
135
+ message: "Failed to delete rating due to a server error."
136
+ });
137
+ }
138
+ });
139
+
140
+ // Calculate average rating
141
+ router.get("/average", async (req, res) => {
142
+ const {
143
+ itemId
144
+ } = req.query;
145
+ const projectId = req.projectId;
146
+ if (!projectId || !itemId) {
147
+ return res.status(400).json({
148
+ success: false,
149
+ message: "Both projectId and itemId are required."
150
+ });
151
+ }
152
+ try {
153
+ const db = await getDb();
154
+ const result = await db.collection("ratings").aggregate([{
155
+ $match: {
156
+ projectId,
157
+ itemId
158
+ }
159
+ }, {
160
+ $group: {
161
+ _id: null,
162
+ averageRating: {
163
+ $avg: "$rating"
164
+ },
165
+ totalRatings: {
166
+ $sum: 1
167
+ }
168
+ }
169
+ }]).toArray();
170
+ if (result.length === 0) {
171
+ return res.status(404).json({
172
+ success: false,
173
+ message: "No ratings found for the given projectId and itemId."
174
+ });
175
+ }
176
+ return res.status(200).json({
177
+ success: true,
178
+ averageRating: result[0].averageRating.toFixed(2),
179
+ totalRatings: result[0].totalRatings
180
+ });
181
+ } catch (error) {
182
+ console.error("Error calculating average rating:", error);
183
+ return res.status(500).json({
184
+ success: false,
185
+ message: "Failed to calculate average rating."
186
+ });
187
+ }
188
+ });
189
+ module.exports = router;
@@ -0,0 +1,132 @@
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 routes entries
10
+ router.get('/', async (req, res) => {
11
+ const {
12
+ projectId
13
+ } = req.query;
14
+ try {
15
+ const query = {
16
+ projectId
17
+ };
18
+ if (!projectId) {
19
+ return res.status(400).json({
20
+ success: false,
21
+ message: 'projectId is required.'
22
+ });
23
+ }
24
+ const routesData = await getDb().collection("routes").find(query).toArray();
25
+ return res.status(200).json({
26
+ success: true,
27
+ allroutes: routesData
28
+ });
29
+ } catch (error) {
30
+ console.error("Error retrieving routes entries:", error);
31
+ return res.status(500).json({
32
+ success: false,
33
+ message: "Failed to retrieve routes entries."
34
+ });
35
+ }
36
+ });
37
+
38
+ // Create a new route
39
+ router.post('/', async (req, res) => {
40
+ try {
41
+ const {
42
+ projectId
43
+ } = req.query;
44
+ if (!projectId) {
45
+ return res.status(400).json({
46
+ success: false,
47
+ message: 'projectId is required.'
48
+ });
49
+ }
50
+ const newRoute = req.body;
51
+ newRoute.projectId = projectId;
52
+ if (!newRoute || Object.keys(newRoute).length === 0) {
53
+ return res.status(400).json({
54
+ success: false,
55
+ message: 'Request body is empty or invalid.'
56
+ });
57
+ }
58
+ const result = await getDb().collection('routes').insertOne(newRoute);
59
+ return res.status(201).json({
60
+ success: true,
61
+ entry: result.ops ? result.ops[0] : newRoute
62
+ });
63
+ } catch (error) {
64
+ console.error('Error inserting route entry:', error);
65
+ return res.status(500).json({
66
+ success: false,
67
+ message: 'Failed to insert route entry.'
68
+ });
69
+ }
70
+ });
71
+
72
+ // Update route code
73
+ router.put('/:route', async (req, res) => {
74
+ try {
75
+ const {
76
+ route
77
+ } = req.params;
78
+ const {
79
+ code
80
+ } = req.body;
81
+ const {
82
+ projectId
83
+ } = req.query;
84
+ if (!projectId) {
85
+ return res.status(400).json({
86
+ success: false,
87
+ message: 'projectId is required.'
88
+ });
89
+ }
90
+ if (!code) {
91
+ return res.status(400).json({
92
+ success: false,
93
+ message: 'Code field is required.'
94
+ });
95
+ }
96
+ const query = {
97
+ route,
98
+ projectId
99
+ };
100
+ const existingRoute = await getDb().collection('routes').findOne(query);
101
+ if (!existingRoute) {
102
+ return res.status(404).json({
103
+ success: false,
104
+ message: 'Route not found.'
105
+ });
106
+ }
107
+ const result = await getDb().collection('routes').updateOne(query, {
108
+ $set: {
109
+ code
110
+ }
111
+ });
112
+ if (result.matchedCount === 0) {
113
+ return res.status(404).json({
114
+ success: false,
115
+ message: 'Route not found.'
116
+ });
117
+ }
118
+ const updatedRoute = await getDb().collection('routes').findOne(query);
119
+ return res.status(200).json({
120
+ success: true,
121
+ entry: updatedRoute
122
+ });
123
+ } catch (error) {
124
+ console.error('Error updating route code:', error);
125
+ return res.status(500).json({
126
+ success: false,
127
+ message: 'Failed to update route code.',
128
+ error: error.message
129
+ });
130
+ }
131
+ });
132
+ module.exports = router;
@@ -0,0 +1,101 @@
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 slides
10
+ router.get('/', async (req, res) => {
11
+ const {
12
+ projectId,
13
+ tag
14
+ } = req.query;
15
+ try {
16
+ const query = {
17
+ projectId
18
+ };
19
+ if (tag) {
20
+ query.tag = tag;
21
+ }
22
+ console.log("Filter.....:", query);
23
+ const slidesData = await getDb().collection('slides').find(query).toArray();
24
+ if (!slidesData || slidesData.length === 0) {
25
+ console.log("Data not found.");
26
+ return res.status(200).json({
27
+ success: true,
28
+ slides: []
29
+ });
30
+ }
31
+ const sortedData = slidesData.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));
32
+ console.log("Slides retrieved and sorted:", sortedData);
33
+ return res.status(200).json({
34
+ success: true,
35
+ slides: sortedData
36
+ });
37
+ } catch (error) {
38
+ console.error("Error retrieving slides:", error);
39
+ return res.status(500).json({
40
+ success: false,
41
+ message: "Failed to retrieve slides."
42
+ });
43
+ }
44
+ });
45
+
46
+ // Add slide
47
+ router.post('/', async (req, res) => {
48
+ const {
49
+ projectId,
50
+ tag,
51
+ city,
52
+ url,
53
+ title,
54
+ description,
55
+ expiry
56
+ } = req.body;
57
+ if (!url || !title || !description || !expiry) {
58
+ return res.status(400).json({
59
+ success: false,
60
+ message: "All fields are required."
61
+ });
62
+ }
63
+ const slideData = {
64
+ projectId,
65
+ url,
66
+ title,
67
+ description,
68
+ expiry,
69
+ createdAt: new Date()
70
+ };
71
+ if (city) {
72
+ slideData.city = city;
73
+ }
74
+ if (tag) {
75
+ slideData.tag = tag;
76
+ }
77
+ try {
78
+ const result = await getDb().collection('slides').insertOne(slideData);
79
+ if (result && result.insertedId) {
80
+ console.log("Slide added with ID:", result.insertedId);
81
+ return res.status(200).json({
82
+ success: true,
83
+ message: "Slide added successfully.",
84
+ slideId: result.insertedId
85
+ });
86
+ } else {
87
+ console.error("Insert operation failed. Result:", result);
88
+ return res.status(500).json({
89
+ success: false,
90
+ message: "Slide could not be added."
91
+ });
92
+ }
93
+ } catch (error) {
94
+ console.error("Error adding slide:", error);
95
+ return res.status(500).json({
96
+ success: false,
97
+ message: "Failed to add slide due to a server error."
98
+ });
99
+ }
100
+ });
101
+ module.exports = router;