powr-sdk-api 4.8.5 → 4.8.7
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.
- package/dist/routes/forms.js +119 -35
- package/package.json +1 -1
package/dist/routes/forms.js
CHANGED
|
@@ -163,65 +163,149 @@ router.get('/getCount/:formName', verifyToken, async (req, res) => {
|
|
|
163
163
|
});
|
|
164
164
|
}
|
|
165
165
|
});
|
|
166
|
+
async function createPowrForm(formData, projectId, res) {
|
|
167
|
+
if (!(formData !== null && formData !== void 0 && formData.formName)) {
|
|
168
|
+
return res.status(400).json({
|
|
169
|
+
success: false,
|
|
170
|
+
message: 'formName is required'
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const db = await getDb();
|
|
174
|
+
const powrFormCollection = db.collection('powrForm');
|
|
175
|
+
const existingForm = await powrFormCollection.findOne({
|
|
176
|
+
formName: formData.formName,
|
|
177
|
+
projectId: projectId
|
|
178
|
+
});
|
|
179
|
+
if (existingForm) {
|
|
180
|
+
return res.status(409).json({
|
|
181
|
+
success: false,
|
|
182
|
+
message: 'Form with this name already exists for this project',
|
|
183
|
+
existingFormId: existingForm._id
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
const finalFormData = {
|
|
187
|
+
...formData,
|
|
188
|
+
projectId: projectId,
|
|
189
|
+
createdAt: new Date()
|
|
190
|
+
};
|
|
191
|
+
const result = await powrFormCollection.insertOne(finalFormData);
|
|
192
|
+
return res.status(201).json({
|
|
193
|
+
success: true,
|
|
194
|
+
message: 'Form created and stored successfully',
|
|
195
|
+
formId: result.insertedId,
|
|
196
|
+
formName: formData.formName,
|
|
197
|
+
projectId: projectId,
|
|
198
|
+
data: finalFormData
|
|
199
|
+
});
|
|
200
|
+
}
|
|
166
201
|
|
|
167
|
-
// POST /create-form -
|
|
168
|
-
router.post('/create-form', verifyToken,
|
|
202
|
+
// POST /create-form - JSON body or legacy JSON file upload
|
|
203
|
+
router.post('/create-form', verifyToken, async (req, res) => {
|
|
169
204
|
try {
|
|
170
205
|
const projectId = req.projectId;
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
message: 'JSON file is required'
|
|
175
|
-
});
|
|
206
|
+
const contentType = req.headers['content-type'] || '';
|
|
207
|
+
if (contentType.includes('application/json')) {
|
|
208
|
+
return createPowrForm(req.body, projectId, res);
|
|
176
209
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
210
|
+
upload.single('jsonFile')(req, res, async uploadError => {
|
|
211
|
+
if (uploadError) {
|
|
212
|
+
return res.status(400).json({
|
|
213
|
+
success: false,
|
|
214
|
+
message: uploadError.message
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
if (!req.file) {
|
|
218
|
+
return res.status(400).json({
|
|
219
|
+
success: false,
|
|
220
|
+
message: 'Form data is required. Send JSON body or upload a JSON file.'
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
const fileContent = req.file.buffer.toString('utf8');
|
|
225
|
+
const formData = JSON.parse(fileContent);
|
|
226
|
+
return createPowrForm(formData, projectId, res);
|
|
227
|
+
} catch (parseError) {
|
|
228
|
+
return res.status(400).json({
|
|
229
|
+
success: false,
|
|
230
|
+
message: 'Invalid JSON file format',
|
|
231
|
+
error: parseError.message
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
} catch (error) {
|
|
236
|
+
res.status(500).json({
|
|
237
|
+
success: false,
|
|
238
|
+
message: 'Error creating form',
|
|
239
|
+
error: error.message
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// PUT /update-form/:formName - Update existing form schema
|
|
245
|
+
router.put('/update-form/:formName', verifyToken, async (req, res) => {
|
|
246
|
+
try {
|
|
247
|
+
const {
|
|
248
|
+
formName
|
|
249
|
+
} = req.params;
|
|
250
|
+
const projectId = req.projectId;
|
|
251
|
+
const {
|
|
252
|
+
formTitle,
|
|
253
|
+
formId,
|
|
254
|
+
description,
|
|
255
|
+
fields
|
|
256
|
+
} = req.body;
|
|
257
|
+
if (!(formTitle !== null && formTitle !== void 0 && formTitle.trim()) || !(formId !== null && formId !== void 0 && formId.trim())) {
|
|
182
258
|
return res.status(400).json({
|
|
183
259
|
success: false,
|
|
184
|
-
message: '
|
|
185
|
-
error: parseError.message
|
|
260
|
+
message: 'formTitle and formId are required'
|
|
186
261
|
});
|
|
187
262
|
}
|
|
188
|
-
if (!
|
|
263
|
+
if (!Array.isArray(fields) || fields.length === 0) {
|
|
189
264
|
return res.status(400).json({
|
|
190
265
|
success: false,
|
|
191
|
-
message: '
|
|
266
|
+
message: 'At least one field is required'
|
|
192
267
|
});
|
|
193
268
|
}
|
|
194
269
|
const db = await getDb();
|
|
195
270
|
const powrFormCollection = db.collection('powrForm');
|
|
196
271
|
const existingForm = await powrFormCollection.findOne({
|
|
197
|
-
formName
|
|
198
|
-
projectId
|
|
272
|
+
formName,
|
|
273
|
+
projectId
|
|
199
274
|
});
|
|
200
|
-
if (existingForm) {
|
|
201
|
-
return res.status(
|
|
275
|
+
if (!existingForm) {
|
|
276
|
+
return res.status(404).json({
|
|
202
277
|
success: false,
|
|
203
|
-
message: 'Form
|
|
204
|
-
existingFormId: existingForm._id
|
|
278
|
+
message: 'Form not found'
|
|
205
279
|
});
|
|
206
280
|
}
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
281
|
+
const updateData = {
|
|
282
|
+
formTitle: formTitle.trim(),
|
|
283
|
+
formId: formId.trim(),
|
|
284
|
+
description: (description === null || description === void 0 ? void 0 : description.trim()) || '',
|
|
285
|
+
fields,
|
|
286
|
+
updatedAt: new Date()
|
|
211
287
|
};
|
|
212
|
-
|
|
213
|
-
|
|
288
|
+
await powrFormCollection.updateOne({
|
|
289
|
+
formName,
|
|
290
|
+
projectId
|
|
291
|
+
}, {
|
|
292
|
+
$set: updateData
|
|
293
|
+
});
|
|
294
|
+
return res.status(200).json({
|
|
214
295
|
success: true,
|
|
215
|
-
message: 'Form
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
296
|
+
message: 'Form updated successfully',
|
|
297
|
+
formName,
|
|
298
|
+
projectId,
|
|
299
|
+
data: {
|
|
300
|
+
...existingForm,
|
|
301
|
+
...updateData,
|
|
302
|
+
formName
|
|
303
|
+
}
|
|
220
304
|
});
|
|
221
305
|
} catch (error) {
|
|
222
|
-
res.status(500).json({
|
|
306
|
+
return res.status(500).json({
|
|
223
307
|
success: false,
|
|
224
|
-
message: 'Error
|
|
308
|
+
message: 'Error updating form',
|
|
225
309
|
error: error.message
|
|
226
310
|
});
|
|
227
311
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.8.
|
|
3
|
+
"version": "4.8.7",
|
|
4
4
|
"description": "Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|