powr-sdk-api 4.18.0 → 4.19.0
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 +90 -2
- package/dist/routes/forms.test.js +96 -0
- package/dist/routes/posts.js +1 -0
- package/dist/utils/posts.js +7 -0
- package/dist/utils/posts.test.js +24 -1
- package/package.json +1 -1
package/dist/routes/forms.js
CHANGED
|
@@ -10,6 +10,76 @@ const {
|
|
|
10
10
|
const {
|
|
11
11
|
verifyToken
|
|
12
12
|
} = require("../middleware/jwtToken");
|
|
13
|
+
const {
|
|
14
|
+
emitActivity
|
|
15
|
+
} = require("../managers/activities");
|
|
16
|
+
function normalizeNotifyUserIds(value) {
|
|
17
|
+
if (!value) return [];
|
|
18
|
+
const raw = Array.isArray(value) ? value : String(value).split(/[,\s]+/);
|
|
19
|
+
return [...new Set(raw.map(id => String(id || "").trim()).filter(Boolean))].slice(0, 100);
|
|
20
|
+
}
|
|
21
|
+
async function resolveFormNotifyUserIds(db, projectId, formDoc, actorId) {
|
|
22
|
+
let userIds = normalizeNotifyUserIds(formDoc && formDoc.notifyUserIds);
|
|
23
|
+
if (userIds.length === 0 && projectId) {
|
|
24
|
+
const profiles = await db.collection("profiles").find({
|
|
25
|
+
projectId: String(projectId),
|
|
26
|
+
access: 100
|
|
27
|
+
}, {
|
|
28
|
+
projection: {
|
|
29
|
+
userId: 1
|
|
30
|
+
}
|
|
31
|
+
}).limit(100).toArray();
|
|
32
|
+
userIds = [...new Set((profiles || []).map(p => p && p.userId != null ? String(p.userId).trim() : "").filter(Boolean))];
|
|
33
|
+
}
|
|
34
|
+
const actor = actorId ? String(actorId).trim() : "";
|
|
35
|
+
const filtered = actor ? userIds.filter(id => id !== actor) : userIds;
|
|
36
|
+
return filtered.length > 0 ? filtered : userIds;
|
|
37
|
+
}
|
|
38
|
+
async function emitFormSubmittedActivity(db, {
|
|
39
|
+
projectId,
|
|
40
|
+
formName,
|
|
41
|
+
studentFormData,
|
|
42
|
+
submittedFormId
|
|
43
|
+
}) {
|
|
44
|
+
if (!projectId || !formName) return null;
|
|
45
|
+
const formDoc = await db.collection("powrForm").findOne({
|
|
46
|
+
formName,
|
|
47
|
+
projectId
|
|
48
|
+
}, {
|
|
49
|
+
projection: {
|
|
50
|
+
formTitle: 1,
|
|
51
|
+
formName: 1,
|
|
52
|
+
notifyUserIds: 1
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
const titleText = String(formDoc && formDoc.formTitle || studentFormData && studentFormData.formTitle || formName).trim();
|
|
56
|
+
const actorId = studentFormData && studentFormData.userId ? String(studentFormData.userId).trim() : null;
|
|
57
|
+
const respondent = String(studentFormData && (studentFormData.respondent_name || studentFormData.person_name || studentFormData.name || studentFormData.whatsapp || studentFormData.email) || "Someone").trim();
|
|
58
|
+
const userIds = await resolveFormNotifyUserIds(db, projectId, formDoc, actorId);
|
|
59
|
+
const notify = userIds.length > 0 ? {
|
|
60
|
+
userIds,
|
|
61
|
+
title: `New form response: ${titleText}`.slice(0, 140),
|
|
62
|
+
body: `${respondent} submitted ${titleText}.`.slice(0, 2000),
|
|
63
|
+
link: "/powr-admin",
|
|
64
|
+
projectId: String(projectId)
|
|
65
|
+
} : null;
|
|
66
|
+
return emitActivity({
|
|
67
|
+
projectId: String(projectId),
|
|
68
|
+
action: "form_submitted",
|
|
69
|
+
userId: actorId || null,
|
|
70
|
+
targetType: "form",
|
|
71
|
+
targetId: submittedFormId ? String(submittedFormId) : String(formName),
|
|
72
|
+
metadata: {
|
|
73
|
+
source: "powr-forms",
|
|
74
|
+
formName: String(formName),
|
|
75
|
+
formTitle: titleText,
|
|
76
|
+
respondent
|
|
77
|
+
},
|
|
78
|
+
notify
|
|
79
|
+
}, {
|
|
80
|
+
getDb: async () => db
|
|
81
|
+
});
|
|
82
|
+
}
|
|
13
83
|
const upload = multer({
|
|
14
84
|
storage: multer.memoryStorage(),
|
|
15
85
|
fileFilter: function (req, file, cb) {
|
|
@@ -116,6 +186,14 @@ router.post('/studentform', async (req, res) => {
|
|
|
116
186
|
submittedAt: new Date()
|
|
117
187
|
};
|
|
118
188
|
const formResult = await studentsFormCollection.insertOne(studentFormData);
|
|
189
|
+
emitFormSubmittedActivity(db, {
|
|
190
|
+
projectId,
|
|
191
|
+
formName,
|
|
192
|
+
studentFormData,
|
|
193
|
+
submittedFormId: formResult.insertedId
|
|
194
|
+
}).catch(err => {
|
|
195
|
+
console.error("form_submitted activity emit failed:", err && err.message ? err.message : err);
|
|
196
|
+
});
|
|
119
197
|
res.status(201).json({
|
|
120
198
|
success: true,
|
|
121
199
|
message: 'Student form submitted successfully',
|
|
@@ -185,6 +263,9 @@ async function createPowrForm(formData, projectId, res) {
|
|
|
185
263
|
}
|
|
186
264
|
const finalFormData = {
|
|
187
265
|
...formData,
|
|
266
|
+
...(formData.notifyUserIds !== undefined ? {
|
|
267
|
+
notifyUserIds: normalizeNotifyUserIds(formData.notifyUserIds)
|
|
268
|
+
} : {}),
|
|
188
269
|
projectId: projectId,
|
|
189
270
|
createdAt: new Date()
|
|
190
271
|
};
|
|
@@ -253,7 +334,8 @@ router.put('/update-form/:formName', verifyToken, async (req, res) => {
|
|
|
253
334
|
formId,
|
|
254
335
|
description,
|
|
255
336
|
imageUrl,
|
|
256
|
-
fields
|
|
337
|
+
fields,
|
|
338
|
+
notifyUserIds
|
|
257
339
|
} = req.body;
|
|
258
340
|
if (!(formTitle !== null && formTitle !== void 0 && formTitle.trim()) || !(formId !== null && formId !== void 0 && formId.trim())) {
|
|
259
341
|
return res.status(400).json({
|
|
@@ -285,6 +367,9 @@ router.put('/update-form/:formName', verifyToken, async (req, res) => {
|
|
|
285
367
|
description: (description === null || description === void 0 ? void 0 : description.trim()) || '',
|
|
286
368
|
imageUrl: (imageUrl === null || imageUrl === void 0 ? void 0 : imageUrl.trim()) || '',
|
|
287
369
|
fields,
|
|
370
|
+
...(notifyUserIds !== undefined ? {
|
|
371
|
+
notifyUserIds: normalizeNotifyUserIds(notifyUserIds)
|
|
372
|
+
} : {}),
|
|
288
373
|
updatedAt: new Date()
|
|
289
374
|
};
|
|
290
375
|
await powrFormCollection.updateOne({
|
|
@@ -360,4 +445,7 @@ router.get("/submissions/:formId", verifyToken, async (req, res) => {
|
|
|
360
445
|
});
|
|
361
446
|
}
|
|
362
447
|
});
|
|
363
|
-
module.exports = router;
|
|
448
|
+
module.exports = router;
|
|
449
|
+
module.exports.emitFormSubmittedActivity = emitFormSubmittedActivity;
|
|
450
|
+
module.exports.resolveFormNotifyUserIds = resolveFormNotifyUserIds;
|
|
451
|
+
module.exports.normalizeNotifyUserIds = normalizeNotifyUserIds;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
jest.mock("../services/mongo", () => ({
|
|
4
|
+
getDb: jest.fn()
|
|
5
|
+
}));
|
|
6
|
+
jest.mock("../middleware/jwtToken", () => ({
|
|
7
|
+
verifyToken: (req, res, next) => next()
|
|
8
|
+
}));
|
|
9
|
+
const {
|
|
10
|
+
emitFormSubmittedActivity,
|
|
11
|
+
resolveFormNotifyUserIds,
|
|
12
|
+
normalizeNotifyUserIds
|
|
13
|
+
} = require("./forms");
|
|
14
|
+
describe("forms Pulse notification helpers", () => {
|
|
15
|
+
const projectId = "6ab230fd53c20550171ac98b";
|
|
16
|
+
const admin1 = "6893ec11c19c093a5bf21720";
|
|
17
|
+
const admin2 = "6893ec11c19c093a5bf21721";
|
|
18
|
+
const submitter = "6893ec11c19c093a5bf21722";
|
|
19
|
+
it("normalizes notifyUserIds from array or comma string", () => {
|
|
20
|
+
expect(normalizeNotifyUserIds([admin1, " ", admin2, admin1])).toEqual([admin1, admin2]);
|
|
21
|
+
expect(normalizeNotifyUserIds(`${admin1}, ${admin2}`)).toEqual([admin1, admin2]);
|
|
22
|
+
});
|
|
23
|
+
it("falls back to project access=100 admins when notifyUserIds is empty", async () => {
|
|
24
|
+
const db = {
|
|
25
|
+
collection(name) {
|
|
26
|
+
expect(name).toBe("profiles");
|
|
27
|
+
return {
|
|
28
|
+
find(filter) {
|
|
29
|
+
expect(filter).toEqual({
|
|
30
|
+
projectId,
|
|
31
|
+
access: 100
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
limit: () => ({
|
|
35
|
+
toArray: async () => [{
|
|
36
|
+
userId: admin1
|
|
37
|
+
}, {
|
|
38
|
+
userId: admin2
|
|
39
|
+
}]
|
|
40
|
+
})
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const ids = await resolveFormNotifyUserIds(db, projectId, {}, submitter);
|
|
47
|
+
expect(ids).toEqual([admin1, admin2]);
|
|
48
|
+
});
|
|
49
|
+
it("emits form_submitted activity with notify payload", async () => {
|
|
50
|
+
const insertedActivities = [];
|
|
51
|
+
const db = {
|
|
52
|
+
collection(name) {
|
|
53
|
+
if (name === "powrForm") {
|
|
54
|
+
return {
|
|
55
|
+
findOne: async () => ({
|
|
56
|
+
formName: "kundli-test",
|
|
57
|
+
formTitle: "Kundli Test Data",
|
|
58
|
+
notifyUserIds: [admin1]
|
|
59
|
+
})
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (name === "activities") {
|
|
63
|
+
return {
|
|
64
|
+
insertOne: async doc => {
|
|
65
|
+
insertedActivities.push(doc);
|
|
66
|
+
return {
|
|
67
|
+
insertedId: "act123"
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
throw new Error(`Unexpected collection ${name}`);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const res = await emitFormSubmittedActivity(db, {
|
|
76
|
+
projectId,
|
|
77
|
+
formName: "kundli-test",
|
|
78
|
+
submittedFormId: "sub123",
|
|
79
|
+
studentFormData: {
|
|
80
|
+
userId: submitter,
|
|
81
|
+
respondent_name: "Vishwajeet",
|
|
82
|
+
whatsapp: "+919999999999"
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
expect(res.insertedId).toBe("act123");
|
|
86
|
+
expect(insertedActivities).toHaveLength(1);
|
|
87
|
+
expect(insertedActivities[0].action).toBe("form_submitted");
|
|
88
|
+
expect(insertedActivities[0].targetType).toBe("form");
|
|
89
|
+
expect(insertedActivities[0].metadata).toEqual({
|
|
90
|
+
source: "powr-forms",
|
|
91
|
+
formName: "kundli-test",
|
|
92
|
+
formTitle: "Kundli Test Data",
|
|
93
|
+
respondent: "Vishwajeet"
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
package/dist/routes/posts.js
CHANGED
|
@@ -606,6 +606,7 @@ router.post("/", verifyToken, async (req, res) => {
|
|
|
606
606
|
title: fields.title,
|
|
607
607
|
description: fields.description || "",
|
|
608
608
|
coverUrl: fields.coverUrl || "",
|
|
609
|
+
sourcePdfUrl: fields.sourcePdfUrl || "",
|
|
609
610
|
bodyMd: fields.bodyMd || "",
|
|
610
611
|
tags: fields.tags || [],
|
|
611
612
|
readMinutes: fields.readMinutes || 1,
|
package/dist/utils/posts.js
CHANGED
|
@@ -60,6 +60,12 @@ function parsePostWrite(body, partial) {
|
|
|
60
60
|
if (coverUrl && !/^https:\/\//i.test(coverUrl)) bad("Cover must be an https link.");
|
|
61
61
|
out.coverUrl = coverUrl;
|
|
62
62
|
}
|
|
63
|
+
if (has("sourcePdfUrl")) {
|
|
64
|
+
const sourcePdfUrl = String(source.sourcePdfUrl || "").trim();
|
|
65
|
+
if (sourcePdfUrl && !/^https:\/\//i.test(sourcePdfUrl)) bad("The PDF must be an https link.");
|
|
66
|
+
if (sourcePdfUrl && !/\.pdf(\?|#|$)/i.test(sourcePdfUrl)) bad("The file must be a PDF.");
|
|
67
|
+
out.sourcePdfUrl = sourcePdfUrl;
|
|
68
|
+
}
|
|
63
69
|
if (Object.prototype.hasOwnProperty.call(source, "slug")) {
|
|
64
70
|
out.slug = slugify(source.slug);
|
|
65
71
|
}
|
|
@@ -123,6 +129,7 @@ function staffPost(row, author) {
|
|
|
123
129
|
status: row.status || "draft",
|
|
124
130
|
authorId: row.authorId ? String(row.authorId) : "",
|
|
125
131
|
slugLocked: !!row.slugLocked,
|
|
132
|
+
sourcePdfUrl: row.sourcePdfUrl || "",
|
|
126
133
|
createdAt: row.createdAt || null,
|
|
127
134
|
updatedAt: row.updatedAt || null
|
|
128
135
|
});
|
package/dist/utils/posts.test.js
CHANGED
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
parsePostWrite,
|
|
6
6
|
parseAuthorWrite,
|
|
7
7
|
publicCard,
|
|
8
|
+
staffPost,
|
|
8
9
|
readMinutes
|
|
9
10
|
} = require("./posts");
|
|
10
11
|
describe("slugify", () => {
|
|
@@ -28,6 +29,16 @@ describe("parsePostWrite", () => {
|
|
|
28
29
|
expect(fields.tags).toEqual(["vibe", "ai"]);
|
|
29
30
|
expect(fields.readMinutes).toBe(1);
|
|
30
31
|
});
|
|
32
|
+
it("keeps a pdf link and rejects anything else", () => {
|
|
33
|
+
expect(parsePostWrite({
|
|
34
|
+
title: "Guide",
|
|
35
|
+
sourcePdfUrl: "https://cdn.example/guide.pdf"
|
|
36
|
+
}).sourcePdfUrl).toBe("https://cdn.example/guide.pdf");
|
|
37
|
+
expect(() => parsePostWrite({
|
|
38
|
+
title: "Guide",
|
|
39
|
+
sourcePdfUrl: "https://cdn.example/guide.png"
|
|
40
|
+
})).toThrow("The file must be a PDF.");
|
|
41
|
+
});
|
|
31
42
|
it("rejects a cover that is not https", () => {
|
|
32
43
|
expect(() => parsePostWrite({
|
|
33
44
|
title: "Hi",
|
|
@@ -66,13 +77,25 @@ describe("publicCard", () => {
|
|
|
66
77
|
slug: "hi",
|
|
67
78
|
title: "Hi",
|
|
68
79
|
bodyMd: "secret",
|
|
69
|
-
status: "published"
|
|
80
|
+
status: "published",
|
|
81
|
+
sourcePdfUrl: "https://cdn.example/a.pdf"
|
|
70
82
|
}, {
|
|
71
83
|
handle: "om",
|
|
72
84
|
displayName: "Om"
|
|
73
85
|
});
|
|
74
86
|
expect(card.bodyMd).toBeUndefined();
|
|
75
87
|
expect(card.status).toBeUndefined();
|
|
88
|
+
expect(card.sourcePdfUrl).toBeUndefined();
|
|
76
89
|
expect(card.author.displayName).toBe("Om");
|
|
77
90
|
});
|
|
91
|
+
it("shows the pdf only to staff", () => {
|
|
92
|
+
const row = {
|
|
93
|
+
_id: "a",
|
|
94
|
+
title: "Hi",
|
|
95
|
+
sourcePdfUrl: "https://cdn.example/a.pdf",
|
|
96
|
+
status: "in_review"
|
|
97
|
+
};
|
|
98
|
+
expect(staffPost(row, null).sourcePdfUrl).toBe("https://cdn.example/a.pdf");
|
|
99
|
+
expect(publicCard(row, null).sourcePdfUrl).toBeUndefined();
|
|
100
|
+
});
|
|
78
101
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.19.0",
|
|
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",
|