powr-sdk-api 4.8.5 → 4.8.6
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 +64 -50
- package/package.json +1 -1
package/dist/routes/forms.js
CHANGED
|
@@ -163,60 +163,74 @@ 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
|
-
});
|
|
176
|
-
}
|
|
177
|
-
let formData;
|
|
178
|
-
try {
|
|
179
|
-
const fileContent = req.file.buffer.toString('utf8');
|
|
180
|
-
formData = JSON.parse(fileContent);
|
|
181
|
-
} catch (parseError) {
|
|
182
|
-
return res.status(400).json({
|
|
183
|
-
success: false,
|
|
184
|
-
message: 'Invalid JSON file format',
|
|
185
|
-
error: parseError.message
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
if (!formData.formName) {
|
|
189
|
-
return res.status(400).json({
|
|
190
|
-
success: false,
|
|
191
|
-
message: 'formName is required in JSON data'
|
|
192
|
-
});
|
|
206
|
+
const contentType = req.headers['content-type'] || '';
|
|
207
|
+
if (contentType.includes('application/json')) {
|
|
208
|
+
return createPowrForm(req.body, projectId, res);
|
|
193
209
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
projectId: projectId,
|
|
219
|
-
data: finalFormData
|
|
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
|
+
}
|
|
220
234
|
});
|
|
221
235
|
} catch (error) {
|
|
222
236
|
res.status(500).json({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.8.
|
|
3
|
+
"version": "4.8.6",
|
|
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",
|