powr-sdk-api 4.19.0 → 4.19.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.
- package/dist/routes/feedback.js +8 -5
- package/dist/routes/posts.js +4 -0
- package/dist/utils/feedback.js +95 -1
- package/dist/utils/feedback.test.js +84 -1
- package/dist/utils/posts.js +8 -0
- package/dist/utils/posts.test.js +11 -0
- package/package.json +1 -1
package/dist/routes/feedback.js
CHANGED
|
@@ -14,7 +14,8 @@ const {
|
|
|
14
14
|
const {
|
|
15
15
|
parseFeedbackCreate,
|
|
16
16
|
parseFeedbackStatus,
|
|
17
|
-
feedbackSummary
|
|
17
|
+
feedbackSummary,
|
|
18
|
+
ringFeedback
|
|
18
19
|
} = require("../utils/feedback");
|
|
19
20
|
async function callerIsAdmin(db, req) {
|
|
20
21
|
var _req$user, _req$user2;
|
|
@@ -121,12 +122,14 @@ router.post("/", async (req, res) => {
|
|
|
121
122
|
};
|
|
122
123
|
const db = await getDb();
|
|
123
124
|
const result = await db.collection("powrFeedback").insertOne(doc);
|
|
125
|
+
const saved = {
|
|
126
|
+
...doc,
|
|
127
|
+
_id: result.insertedId
|
|
128
|
+
};
|
|
129
|
+
await ringFeedback(db, saved);
|
|
124
130
|
return res.status(201).json({
|
|
125
131
|
success: true,
|
|
126
|
-
data: feedbackSummary(
|
|
127
|
-
...doc,
|
|
128
|
-
_id: result.insertedId
|
|
129
|
-
})
|
|
132
|
+
data: feedbackSummary(saved)
|
|
130
133
|
});
|
|
131
134
|
} catch (error) {
|
|
132
135
|
console.error("POST /feedback", error);
|
package/dist/routes/posts.js
CHANGED
|
@@ -19,6 +19,7 @@ const {
|
|
|
19
19
|
const {
|
|
20
20
|
slugify,
|
|
21
21
|
parsePostWrite,
|
|
22
|
+
parsePublishedAt,
|
|
22
23
|
parseAuthorWrite,
|
|
23
24
|
publicAuthor,
|
|
24
25
|
publicCard,
|
|
@@ -667,6 +668,9 @@ router.patch("/:id", verifyToken, async (req, res) => {
|
|
|
667
668
|
});
|
|
668
669
|
}
|
|
669
670
|
const fields = parsePostWrite(req.body || {}, true);
|
|
671
|
+
if (access.editor && Object.prototype.hasOwnProperty.call(req.body || {}, "publishedAt")) {
|
|
672
|
+
fields.publishedAt = parsePublishedAt(req.body.publishedAt);
|
|
673
|
+
}
|
|
670
674
|
if (fields.slug && (current.slugLocked || current.status === "published")) delete fields.slug;
|
|
671
675
|
if (fields.slug && fields.slug !== current.slug) {
|
|
672
676
|
fields.slug = await uniqueSlug(db, access.projectId, fields.slug, id);
|
package/dist/utils/feedback.js
CHANGED
|
@@ -41,6 +41,95 @@ function parseFeedbackStatus(body) {
|
|
|
41
41
|
}
|
|
42
42
|
return status;
|
|
43
43
|
}
|
|
44
|
+
const ADMIN_ORIGINS = {
|
|
45
|
+
"678db163f70e70f6056b2c38": "https://urbanstall.app",
|
|
46
|
+
"678db178f70e70f6056b2c3d": "https://excelrs.dev",
|
|
47
|
+
"6a266d9ed1945234e550241d": "https://rasoi.lawazia.tech",
|
|
48
|
+
"6921a93a3442a87a29e343a3": "https://marketiq.lawazia.tech",
|
|
49
|
+
"67feef40059e70e9cf0db96b": "https://spriny.lawazia.tech",
|
|
50
|
+
"688ff878af20e61d802124e7": "https://ampy.lawazia.tech",
|
|
51
|
+
"6a4796c08fd806f0476f87c0": "https://earn.urbanstall.app",
|
|
52
|
+
"69144e04dff7d83475e3ab71": "https://win.urbanstall.app",
|
|
53
|
+
"6a9c4e083a27d813e2d80ec9": "https://employee.lawazia.tech",
|
|
54
|
+
"6a438583e5075411f5dd15e0": "https://office.lawazia.tech",
|
|
55
|
+
"6ac697da9a9acc3bc53d66fa": "https://testflight.lawazia.tech",
|
|
56
|
+
"6ab5e57fde1ce4f47861257f": "https://data.lawazia.tech",
|
|
57
|
+
"6ac36a6c24c482d7d4644948": "https://mobility.lawazia.tech"
|
|
58
|
+
};
|
|
59
|
+
function feedbackAdminLink(projectId) {
|
|
60
|
+
const origin = ADMIN_ORIGINS[String(projectId || "").trim()];
|
|
61
|
+
if (!origin) return "/powr-admin";
|
|
62
|
+
return `${origin}/powr-admin`;
|
|
63
|
+
}
|
|
64
|
+
function adminIdsFromProfiles(profiles, actorId) {
|
|
65
|
+
const actor = String(actorId || "").trim();
|
|
66
|
+
const ids = [...new Set((profiles || []).map(row => row && row.userId != null ? String(row.userId).trim() : "").filter(Boolean))];
|
|
67
|
+
const others = actor ? ids.filter(id => id !== actor) : ids;
|
|
68
|
+
return others.length > 0 ? others : ids;
|
|
69
|
+
}
|
|
70
|
+
async function loadFeedbackAdminIds(db, projectId, actorId) {
|
|
71
|
+
const project = String(projectId || "").trim();
|
|
72
|
+
if (!project) return [];
|
|
73
|
+
const profiles = await db.collection("profiles").find({
|
|
74
|
+
projectId: project,
|
|
75
|
+
access: {
|
|
76
|
+
$in: [100, "100"]
|
|
77
|
+
}
|
|
78
|
+
}, {
|
|
79
|
+
projection: {
|
|
80
|
+
userId: 1
|
|
81
|
+
}
|
|
82
|
+
}).limit(100).toArray();
|
|
83
|
+
return adminIdsFromProfiles(profiles, actorId);
|
|
84
|
+
}
|
|
85
|
+
function feedbackNotify({
|
|
86
|
+
projectId,
|
|
87
|
+
title,
|
|
88
|
+
kind,
|
|
89
|
+
createdByName,
|
|
90
|
+
userIds
|
|
91
|
+
}) {
|
|
92
|
+
const who = String(createdByName || "").trim() || "Someone";
|
|
93
|
+
const label = kind === "other" ? "Domain" : "Tech";
|
|
94
|
+
return {
|
|
95
|
+
userIds,
|
|
96
|
+
title: "New feedback",
|
|
97
|
+
body: `${who} · ${label} · ${String(title || "").trim()}`.slice(0, 2000),
|
|
98
|
+
link: feedbackAdminLink(projectId),
|
|
99
|
+
projectId: String(projectId)
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
async function ringFeedback(db, doc, deps = {}) {
|
|
103
|
+
try {
|
|
104
|
+
const projectId = String(doc.projectId || "").trim();
|
|
105
|
+
const noteId = doc._id != null ? String(doc._id) : "";
|
|
106
|
+
if (!projectId || !noteId) return null;
|
|
107
|
+
const userIds = await loadFeedbackAdminIds(db, projectId, doc.createdBy);
|
|
108
|
+
if (!userIds.length) return null;
|
|
109
|
+
const emitActivity = deps.emitActivity || require("../managers/activities").emitActivity;
|
|
110
|
+
return await emitActivity({
|
|
111
|
+
projectId,
|
|
112
|
+
action: "feedback_filed",
|
|
113
|
+
userId: doc.createdBy ? String(doc.createdBy) : null,
|
|
114
|
+
targetType: "feedback",
|
|
115
|
+
targetId: noteId,
|
|
116
|
+
notify: feedbackNotify({
|
|
117
|
+
projectId,
|
|
118
|
+
title: doc.title,
|
|
119
|
+
kind: doc.kind,
|
|
120
|
+
createdByName: doc.createdByName,
|
|
121
|
+
userIds
|
|
122
|
+
})
|
|
123
|
+
}, {
|
|
124
|
+
getDb: async () => db,
|
|
125
|
+
fetchImpl: deps.fetchImpl,
|
|
126
|
+
env: deps.env
|
|
127
|
+
});
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.error("feedback pulse", error instanceof Error ? error.message : "failed");
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
44
133
|
function feedbackSummary(row) {
|
|
45
134
|
return {
|
|
46
135
|
_id: String(row._id),
|
|
@@ -60,5 +149,10 @@ module.exports = {
|
|
|
60
149
|
STATUSES,
|
|
61
150
|
parseFeedbackCreate,
|
|
62
151
|
parseFeedbackStatus,
|
|
63
|
-
feedbackSummary
|
|
152
|
+
feedbackSummary,
|
|
153
|
+
feedbackAdminLink,
|
|
154
|
+
adminIdsFromProfiles,
|
|
155
|
+
loadFeedbackAdminIds,
|
|
156
|
+
feedbackNotify,
|
|
157
|
+
ringFeedback
|
|
64
158
|
};
|
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
const {
|
|
4
4
|
parseFeedbackCreate,
|
|
5
5
|
parseFeedbackStatus,
|
|
6
|
-
feedbackSummary
|
|
6
|
+
feedbackSummary,
|
|
7
|
+
feedbackAdminLink,
|
|
8
|
+
adminIdsFromProfiles,
|
|
9
|
+
ringFeedback
|
|
7
10
|
} = require("./feedback");
|
|
8
11
|
describe("parseFeedbackCreate", () => {
|
|
9
12
|
it("keeps title, note, and kind", () => {
|
|
@@ -52,4 +55,84 @@ describe("feedbackSummary", () => {
|
|
|
52
55
|
expect(row.kind).toBe("other");
|
|
53
56
|
expect(row.createdByName).toBe("Ravi");
|
|
54
57
|
});
|
|
58
|
+
});
|
|
59
|
+
describe("feedback pulse", () => {
|
|
60
|
+
const projectId = "6a4796c08fd806f0476f87c0";
|
|
61
|
+
const admin = "6893ec11c19c093a5bf21720";
|
|
62
|
+
const sender = "6893ec11c19c093a5bf21722";
|
|
63
|
+
it("opens that app's admin page", () => {
|
|
64
|
+
expect(feedbackAdminLink(projectId)).toBe("https://earn.urbanstall.app/powr-admin");
|
|
65
|
+
expect(feedbackAdminLink("unknown")).toBe("/powr-admin");
|
|
66
|
+
});
|
|
67
|
+
it("rings other admins, and the sender only when they are the only admin", () => {
|
|
68
|
+
expect(adminIdsFromProfiles([{
|
|
69
|
+
userId: admin
|
|
70
|
+
}, {
|
|
71
|
+
userId: sender
|
|
72
|
+
}], sender)).toEqual([admin]);
|
|
73
|
+
expect(adminIdsFromProfiles([{
|
|
74
|
+
userId: sender
|
|
75
|
+
}], sender)).toEqual([sender]);
|
|
76
|
+
});
|
|
77
|
+
it("emits a Pulse notify and still returns when the ring fails", async () => {
|
|
78
|
+
const db = {
|
|
79
|
+
collection(name) {
|
|
80
|
+
expect(name).toBe("profiles");
|
|
81
|
+
return {
|
|
82
|
+
find() {
|
|
83
|
+
return {
|
|
84
|
+
limit: () => ({
|
|
85
|
+
toArray: async () => [{
|
|
86
|
+
userId: admin
|
|
87
|
+
}]
|
|
88
|
+
})
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const emitted = [];
|
|
95
|
+
const result = await ringFeedback(db, {
|
|
96
|
+
_id: "note1",
|
|
97
|
+
projectId,
|
|
98
|
+
title: "Banner size",
|
|
99
|
+
kind: "app",
|
|
100
|
+
createdBy: sender,
|
|
101
|
+
createdByName: "Ravi"
|
|
102
|
+
}, {
|
|
103
|
+
emitActivity: async event => {
|
|
104
|
+
emitted.push(event);
|
|
105
|
+
return {
|
|
106
|
+
insertedId: "act1"
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
expect(result.insertedId).toBe("act1");
|
|
111
|
+
expect(emitted[0].action).toBe("feedback_filed");
|
|
112
|
+
expect(emitted[0].notify.userIds).toEqual([admin]);
|
|
113
|
+
expect(emitted[0].notify.title).toBe("New feedback");
|
|
114
|
+
expect(emitted[0].notify.body).toBe("Ravi · Tech · Banner size");
|
|
115
|
+
expect(emitted[0].notify.link).toBe("https://earn.urbanstall.app/powr-admin");
|
|
116
|
+
const quiet = await ringFeedback({
|
|
117
|
+
collection() {
|
|
118
|
+
return {
|
|
119
|
+
find: () => ({
|
|
120
|
+
limit: () => ({
|
|
121
|
+
toArray: async () => []
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}, {
|
|
127
|
+
_id: "note2",
|
|
128
|
+
projectId,
|
|
129
|
+
title: "Quiet",
|
|
130
|
+
createdBy: sender
|
|
131
|
+
}, {
|
|
132
|
+
emitActivity: async () => {
|
|
133
|
+
throw new Error("should not emit");
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
expect(quiet).toBeNull();
|
|
137
|
+
});
|
|
55
138
|
});
|
package/dist/utils/posts.js
CHANGED
|
@@ -28,6 +28,13 @@ function parseTags(value) {
|
|
|
28
28
|
}
|
|
29
29
|
return tags;
|
|
30
30
|
}
|
|
31
|
+
function parsePublishedAt(value) {
|
|
32
|
+
if (value == null || String(value).trim() === "") bad("Publish date is required.");
|
|
33
|
+
const date = new Date(value);
|
|
34
|
+
if (Number.isNaN(date.getTime())) bad("Publish date is not a real date.");
|
|
35
|
+
if (date.getTime() > Date.now() + 2 * 60 * 1000) bad("Publish date cannot be in the future.");
|
|
36
|
+
return date;
|
|
37
|
+
}
|
|
31
38
|
function readMinutes(markdown) {
|
|
32
39
|
const words = String(markdown || "").trim().split(/\s+/).filter(Boolean).length;
|
|
33
40
|
if (!words) return 1;
|
|
@@ -151,6 +158,7 @@ module.exports = {
|
|
|
151
158
|
ROLES,
|
|
152
159
|
slugify,
|
|
153
160
|
parseTags,
|
|
161
|
+
parsePublishedAt,
|
|
154
162
|
readMinutes,
|
|
155
163
|
parsePostWrite,
|
|
156
164
|
parseAuthorWrite,
|
package/dist/utils/posts.test.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const {
|
|
4
4
|
slugify,
|
|
5
5
|
parsePostWrite,
|
|
6
|
+
parsePublishedAt,
|
|
6
7
|
parseAuthorWrite,
|
|
7
8
|
publicCard,
|
|
8
9
|
staffPost,
|
|
@@ -53,6 +54,16 @@ describe("parsePostWrite", () => {
|
|
|
53
54
|
});
|
|
54
55
|
});
|
|
55
56
|
});
|
|
57
|
+
describe("parsePublishedAt", () => {
|
|
58
|
+
it("keeps a past date", () => {
|
|
59
|
+
expect(parsePublishedAt("2019-01-20T04:30:00.000Z").toISOString()).toBe("2019-01-20T04:30:00.000Z");
|
|
60
|
+
});
|
|
61
|
+
it("rejects a future date and a blank date", () => {
|
|
62
|
+
expect(() => parsePublishedAt("2099-01-01T00:00:00.000Z")).toThrow("Publish date cannot be in the future.");
|
|
63
|
+
expect(() => parsePublishedAt("")).toThrow("Publish date is required.");
|
|
64
|
+
expect(() => parsePublishedAt("not-a-date")).toThrow("Publish date is not a real date.");
|
|
65
|
+
});
|
|
66
|
+
});
|
|
56
67
|
describe("readMinutes", () => {
|
|
57
68
|
it("rounds about 200 words to a minute", () => {
|
|
58
69
|
const words = new Array(400).fill("word").join(" ");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.19.
|
|
3
|
+
"version": "4.19.2",
|
|
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",
|