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,175 @@
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.post("/", async (req, res) => {
12
+ const {
13
+ projectId,
14
+ phoneNumber,
15
+ email,
16
+ ...userData
17
+ } = req.body;
18
+ if (!phoneNumber && !email) {
19
+ return res.status(400).json({
20
+ success: false,
21
+ message: "phoneNumber or email is required."
22
+ });
23
+ }
24
+ const user = {
25
+ projectId,
26
+ phoneNumber,
27
+ email,
28
+ ...userData,
29
+ createdAt: new Date()
30
+ };
31
+ try {
32
+ const usersCollection = getDb().collection("users");
33
+ if (phoneNumber) {
34
+ const existingByPhone = await usersCollection.findOne({
35
+ projectId,
36
+ phoneNumber
37
+ });
38
+ if (existingByPhone) {
39
+ return res.status(409).json({
40
+ success: false,
41
+ message: "Phone number already exists."
42
+ });
43
+ }
44
+ }
45
+ if (email) {
46
+ const existingByEmail = await usersCollection.findOne({
47
+ projectId,
48
+ email
49
+ });
50
+ if (existingByEmail) {
51
+ return res.status(409).json({
52
+ success: false,
53
+ message: "Email already exists."
54
+ });
55
+ }
56
+ }
57
+ const result = await getDb().collection("users").insertOne(user);
58
+ return res.status(200).json({
59
+ success: true,
60
+ message: "User created successfully.",
61
+ userId: result.insertedId
62
+ });
63
+ } catch (error) {
64
+ console.error("Error creating user:", error);
65
+ return res.status(500).json({
66
+ success: false,
67
+ message: "Internal server error."
68
+ });
69
+ }
70
+ });
71
+ router.get("/", async (req, res) => {
72
+ const {
73
+ projectId,
74
+ _id,
75
+ phoneNumber,
76
+ email
77
+ } = req.query;
78
+ if (!_id && !phoneNumber && !email) {
79
+ return res.status(400).json({
80
+ success: false,
81
+ message: "no filters provided.",
82
+ users: []
83
+ });
84
+ }
85
+ const filter = {
86
+ projectId
87
+ };
88
+ if (_id) {
89
+ try {
90
+ filter._id = new ObjectId(_id);
91
+ } catch (e) {
92
+ return res.status(400).json({
93
+ success: false,
94
+ message: "Invalid _id format."
95
+ });
96
+ }
97
+ }
98
+ if (phoneNumber) {
99
+ filter.phoneNumber = phoneNumber;
100
+ }
101
+ if (email) {
102
+ filter.email = email;
103
+ }
104
+ try {
105
+ const users = await getDb().collection("users").find(filter).toArray();
106
+ return res.status(200).json({
107
+ success: true,
108
+ message: "Users fetched successfully.",
109
+ users
110
+ });
111
+ } catch (error) {
112
+ console.error("Error fetching users:", error);
113
+ return res.status(500).json({
114
+ success: false,
115
+ message: "Internal server error."
116
+ });
117
+ }
118
+ });
119
+ router.post("/getOrCreate", async (req, res) => {
120
+ const {
121
+ projectId,
122
+ phoneNumber,
123
+ email,
124
+ ...userData
125
+ } = req.body;
126
+ if (!phoneNumber && !email) {
127
+ return res.status(400).json({
128
+ success: false,
129
+ message: "phoneNumber or email is required."
130
+ });
131
+ }
132
+ const user = {
133
+ projectId,
134
+ phoneNumber,
135
+ email,
136
+ ...userData,
137
+ createdAt: new Date()
138
+ };
139
+ try {
140
+ const usersCollection = getDb().collection("users");
141
+ let existingUser = null;
142
+ if (phoneNumber) {
143
+ existingUser = await usersCollection.findOne({
144
+ projectId,
145
+ phoneNumber
146
+ });
147
+ }
148
+ if (!existingUser && email) {
149
+ existingUser = await usersCollection.findOne({
150
+ projectId,
151
+ email
152
+ });
153
+ }
154
+ if (existingUser) {
155
+ return res.status(200).json({
156
+ success: true,
157
+ message: "User already exists.",
158
+ userId: existingUser._id
159
+ });
160
+ }
161
+ const result = await usersCollection.insertOne(user);
162
+ return res.status(200).json({
163
+ success: true,
164
+ message: "User created successfully.",
165
+ userId: result.insertedId
166
+ });
167
+ } catch (error) {
168
+ console.error("Error creating or fetching user:", error);
169
+ return res.status(500).json({
170
+ success: false,
171
+ message: "Internal server error."
172
+ });
173
+ }
174
+ });
175
+ module.exports = router;
@@ -0,0 +1,94 @@
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 waitlist entries
10
+ router.get('/', async (req, res) => {
11
+ const {
12
+ projectId
13
+ } = req.query;
14
+ try {
15
+ const query = {
16
+ projectId
17
+ };
18
+ console.log("Filter.....:", query);
19
+ const waitlistData = await getDb().collection("waitlists").find(query).toArray();
20
+ if (!waitlistData || waitlistData.length === 0) {
21
+ console.log("Data not found.");
22
+ return res.status(200).json({
23
+ success: true,
24
+ entries: []
25
+ });
26
+ }
27
+ const sortedData = waitlistData.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));
28
+ console.log("Waitlist entries retrieved and sorted:", sortedData);
29
+ return res.status(200).json({
30
+ success: true,
31
+ entries: sortedData
32
+ });
33
+ } catch (error) {
34
+ console.error("Error retrieving waitlist entries:", error);
35
+ return res.status(500).json({
36
+ success: false,
37
+ message: "Failed to retrieve waitlist entries."
38
+ });
39
+ }
40
+ });
41
+
42
+ // Original POST endpoint for adding to waitlist
43
+ router.post("/", async (req, res) => {
44
+ const {
45
+ projectId,
46
+ email
47
+ } = req.body;
48
+ try {
49
+ const existingEntry = await getDb().collection("waitlists").findOne({
50
+ projectId,
51
+ email
52
+ });
53
+ if (existingEntry) {
54
+ return res.status(400).json({
55
+ success: false,
56
+ message: "You are already in the waitlist "
57
+ });
58
+ }
59
+ } catch (error) {
60
+ console.error("Error checking waitlist:", error);
61
+ return res.status(500).json({
62
+ success: false,
63
+ message: "Failed to check waitlist due to a server error."
64
+ });
65
+ }
66
+ const waitListData = {
67
+ projectId,
68
+ email,
69
+ createdAt: new Date()
70
+ };
71
+ try {
72
+ const result = await getDb().collection("waitlists").insertOne(waitListData);
73
+ if (result && result.insertedId) {
74
+ return res.status(200).json({
75
+ success: true,
76
+ message: "you are added in the waitist",
77
+ userId: result.insertedId
78
+ });
79
+ } else {
80
+ console.error("Insert operation failed. Result:", result);
81
+ return res.status(500).json({
82
+ success: false,
83
+ message: "user could not be added."
84
+ });
85
+ }
86
+ } catch (error) {
87
+ console.error("Error adding user:", error);
88
+ return res.status(500).json({
89
+ success: false,
90
+ message: "Failed to add user due to a server error."
91
+ });
92
+ }
93
+ });
94
+ module.exports = router;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "2.1.1",
3
+ "version": "2.2.2",
4
4
  "description": "Shared API core library for PowrStack projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/README.md DELETED
@@ -1,93 +0,0 @@
1
- # Powr SDK API
2
-
3
- A shared API core library for PowrStack projects that provides common admin routes and middleware.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- npm install powr-sdk-api
9
- ```
10
-
11
- ## Usage
12
-
13
- ### Basic Setup
14
-
15
- ```javascript
16
- const express = require('express');
17
- const { createAdminRoutes } = require('powr-sdk-api');
18
-
19
- const app = express();
20
-
21
- // Mount admin routes (projectId from environment)
22
- app.use('/admin', createAdminRoutes());
23
-
24
- app.listen(3000);
25
- ```
26
-
27
- ### Environment Variables
28
-
29
- The SDK uses environment variables from the host project. Set these in your `.env` file:
30
-
31
- ```bash
32
- # Required
33
- MONGODB_URI=mongodb+srv://user:pass@cluster/database
34
- PROJECT_ID=your-project-id
35
-
36
- # Optional
37
- JWT_TOKEN=your-jwt-token
38
- STORAGE_BUCKET=your-storage-bucket
39
- ```
40
-
41
- ### Example Host Project Setup
42
-
43
- ```bash
44
- # spriny-api/.env
45
- MONGODB_URI=mongodb+srv://powrbase:powrbase123@cluster0.3jgjn.mongodb.net/powrbasedb
46
- PROJECT_ID=67feef40059e70e9cf0db96b
47
- JWT_TOKEN=5b69e0614a3d265ae4f7e01fb46205780208be81b6a646797d687448d730550e
48
- STORAGE_BUCKET=powrbase-files
49
- ```
50
-
51
- ```javascript
52
- // spriny-api/app.js
53
- const express = require('express');
54
- const { createAdminRoutes } = require('powr-sdk-api');
55
-
56
- const app = express();
57
-
58
- // One line integration!
59
- app.use('/admin', createAdminRoutes());
60
-
61
- app.listen(3000);
62
- ```
63
-
64
- ## Available Routes
65
-
66
- The SDK provides these admin routes:
67
-
68
- - `/admin/comments` - Comments management
69
- - `/admin/files` - File uploads
70
- - `/admin/forms` - Dynamic forms
71
- - `/admin/invoice` - Invoice management
72
- - `/admin/likes` - Like/unlike functionality
73
- - `/admin/ratings` - Rating system
74
- - `/admin/users` - User management
75
- - `/admin/waitlists` - Waitlist management
76
- - `/admin/activities` - Activity tracking
77
- - `/admin/auth` - Authentication
78
- - `/admin/blogs` - Blog management
79
- - `/admin/slides` - Banner slides
80
- - `/admin/notifications` - Notifications
81
-
82
- ## Error Handling
83
-
84
- The SDK will throw clear errors if required environment variables are missing:
85
-
86
- ```javascript
87
- // Error: MONGODB_URI environment variable is required
88
- // Error: PROJECT_ID environment variable is required
89
- ```
90
-
91
- ## Version
92
-
93
- Current version: 2.0.3