powr-sdk-api 3.1.4 → 3.1.5

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.
@@ -6,13 +6,11 @@ const config = require('../config');
6
6
  * Middleware to inject and validate projectId into requests
7
7
  * @param {Object} options - Configuration options
8
8
  * @param {boolean} options.isCentralService - Whether this is a central service (like powr-base-cloud)
9
- * @param {boolean} options.requireProjectId - Whether to require projectId validation (default: true)
10
9
  * @param {Array} options.excludePaths - Array of paths to exclude from projectId validation
11
10
  * @returns {Function} Express middleware function
12
11
  */
13
12
  const injectProjectId = (options = {}) => {
14
13
  const isCentralService = options.isCentralService || false;
15
- const requireProjectId = options.requireProjectId !== false; // Default to true
16
14
  const excludePaths = options.excludePaths || ['/auth/login', '/auth/register', '/health'];
17
15
  return (req, res, next) => {
18
16
  try {
@@ -23,21 +21,14 @@ const injectProjectId = (options = {}) => {
23
21
  }
24
22
  if (isCentralService) {
25
23
  // For central services, get projectId from request
26
- const projectId = req.query.projectId || req.body.projectId;
27
- if (requireProjectId && !projectId) {
28
- return res.status(400).json({
29
- success: false,
30
- message: 'projectId is required'
31
- });
32
- }
33
- req.projectId = projectId;
24
+ req.projectId = req.query.projectId || req.body.projectId;
34
25
  } else {
35
26
  // For individual APIs, use config.projectId
36
27
  req.projectId = config.projectId;
37
28
  }
38
29
 
39
- // Validate projectId if required
40
- if (requireProjectId && !req.projectId) {
30
+ // Validate projectId (always required except for excluded paths)
31
+ if (!req.projectId) {
41
32
  return res.status(400).json({
42
33
  success: false,
43
34
  message: 'projectId is required'
@@ -52,12 +52,6 @@ router.get('/:formName', async (req, res) => {
52
52
  formName
53
53
  } = req.params;
54
54
  const projectId = req.projectId;
55
- if (!projectId) {
56
- return res.status(400).json({
57
- success: false,
58
- message: 'projectId is required'
59
- });
60
- }
61
55
  const db = await getDb();
62
56
  const collection = db.collection("powrForm");
63
57
  const formData = await collection.findOne({
@@ -170,15 +164,7 @@ router.get('/getCount/:formName', async (req, res) => {
170
164
  // POST /create-form - Upload JSON file and store form data
171
165
  router.post('/create-form', upload.single('jsonFile'), async (req, res) => {
172
166
  try {
173
- const {
174
- projectId
175
- } = req.query;
176
- if (!projectId) {
177
- return res.status(400).json({
178
- success: false,
179
- message: 'projectId is required as query parameter'
180
- });
181
- }
167
+ const projectId = req.projectId;
182
168
  if (!req.file) {
183
169
  return res.status(400).json({
184
170
  success: false,
@@ -34,12 +34,6 @@ router.get('/:invoiceId', async (req, res) => {
34
34
  } = req.params;
35
35
  const projectId = req.projectId;
36
36
  try {
37
- if (!projectId) {
38
- return res.status(400).json({
39
- success: false,
40
- message: 'projectId is required.'
41
- });
42
- }
43
37
  const _id = new ObjectId(invoiceId);
44
38
  const db = await getDb();
45
39
  const invoiceData = await db.collection("invoices").findOne({
@@ -68,12 +62,6 @@ router.post('/', async (req, res) => {
68
62
  serviceSection,
69
63
  paymentSection
70
64
  } = req.body;
71
- if (!projectId) {
72
- return res.status(400).json({
73
- success: false,
74
- message: "projectId is required."
75
- });
76
- }
77
65
  const invoice = {
78
66
  invoiceNumber,
79
67
  dateOfIssue,
@@ -113,12 +101,6 @@ router.put('/:invoiceId', async (req, res) => {
113
101
  invoiceId
114
102
  } = req.params;
115
103
  const projectId = req.projectId;
116
- if (!projectId) {
117
- return res.status(400).json({
118
- success: false,
119
- message: "projectId is required."
120
- });
121
- }
122
104
  let filter;
123
105
  try {
124
106
  filter = {
@@ -16,13 +16,13 @@ router.post('/', async (req, res) => {
16
16
  } = req.body;
17
17
  const {
18
18
  userId,
19
- projectId,
20
19
  contentId
21
20
  } = req.query;
22
- if (!userId || typeof liked !== 'boolean' || !projectId) {
21
+ const projectId = req.projectId;
22
+ if (!userId || typeof liked !== 'boolean') {
23
23
  return res.status(400).json({
24
24
  success: false,
25
- message: 'userId and projectId are required in query, liked (boolean) is required in body.'
25
+ message: 'userId is required in query, liked (boolean) is required in body.'
26
26
  });
27
27
  }
28
28
  try {
@@ -58,15 +58,9 @@ router.post('/', async (req, res) => {
58
58
  });
59
59
  router.get('/', async (req, res) => {
60
60
  const {
61
- projectId,
62
61
  contentId
63
62
  } = req.query;
64
- if (!projectId) {
65
- return res.status(400).json({
66
- success: false,
67
- message: 'projectId is required in query.'
68
- });
69
- }
63
+ const projectId = req.projectId;
70
64
  try {
71
65
  const query = {
72
66
  projectId
@@ -97,14 +91,14 @@ router.get('/:likeId', async (req, res) => {
97
91
  likeId
98
92
  } = req.params;
99
93
  const {
100
- projectId,
101
94
  contentId
102
95
  } = req.query;
96
+ const projectId = req.projectId;
103
97
  try {
104
98
  const filter = {
105
- userId: new ObjectId(likeId)
99
+ userId: new ObjectId(likeId),
100
+ projectId
106
101
  };
107
- if (projectId) filter.projectId = projectId;
108
102
  if (contentId) filter.contentId = contentId;
109
103
  const db = await getDb();
110
104
  const like = await db.collection('likes').findOne(filter);
@@ -65,12 +65,6 @@ router.get("/", async (req, res) => {
65
65
  userId
66
66
  } = req.query;
67
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
68
  try {
75
69
  const query = {
76
70
  projectId
@@ -106,10 +100,10 @@ router.delete("/", async (req, res) => {
106
100
  ratingId
107
101
  } = req.query;
108
102
  const projectId = req.projectId;
109
- if (!ratingId || !projectId) {
103
+ if (!ratingId) {
110
104
  return res.status(400).json({
111
105
  success: false,
112
- message: "Both ratingId and projectId are required to delete a rating."
106
+ message: "ratingId is required to delete a rating."
113
107
  });
114
108
  }
115
109
  try {
@@ -143,10 +137,10 @@ router.get("/average", async (req, res) => {
143
137
  itemId
144
138
  } = req.query;
145
139
  const projectId = req.projectId;
146
- if (!projectId || !itemId) {
140
+ if (!itemId) {
147
141
  return res.status(400).json({
148
142
  success: false,
149
- message: "Both projectId and itemId are required."
143
+ message: "itemId is required."
150
144
  });
151
145
  }
152
146
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "3.1.4",
3
+ "version": "3.1.5",
4
4
  "description": "Shared API core library for PowrStack projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",