powr-sdk-api 4.15.2 → 4.15.3
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 +194 -0
- package/dist/routes/index.js +2 -0
- package/dist/utils/feedback.js +64 -0
- package/dist/utils/feedback.test.js +55 -0
- package/package.json +1 -1
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const {
|
|
5
|
+
ObjectId
|
|
6
|
+
} = require("mongodb");
|
|
7
|
+
const router = express.Router();
|
|
8
|
+
const {
|
|
9
|
+
getDb
|
|
10
|
+
} = require("../services/mongo");
|
|
11
|
+
const {
|
|
12
|
+
isPageviewAdmin
|
|
13
|
+
} = require("../utils/track");
|
|
14
|
+
const {
|
|
15
|
+
parseFeedbackCreate,
|
|
16
|
+
parseFeedbackStatus,
|
|
17
|
+
feedbackSummary
|
|
18
|
+
} = require("../utils/feedback");
|
|
19
|
+
async function callerIsAdmin(db, req) {
|
|
20
|
+
var _req$user, _req$user2;
|
|
21
|
+
const rawId = String(((_req$user = req.user) === null || _req$user === void 0 ? void 0 : _req$user.powrId) || "");
|
|
22
|
+
let profileAccess;
|
|
23
|
+
let userAccess;
|
|
24
|
+
if (rawId.length === 24 && ObjectId.isValid(rawId)) {
|
|
25
|
+
const oid = new ObjectId(rawId);
|
|
26
|
+
const [profile, user] = await Promise.all([db.collection("profiles").findOne({
|
|
27
|
+
userId: {
|
|
28
|
+
$in: [oid, rawId]
|
|
29
|
+
},
|
|
30
|
+
projectId: String(req.projectId || "")
|
|
31
|
+
}, {
|
|
32
|
+
projection: {
|
|
33
|
+
access: 1
|
|
34
|
+
}
|
|
35
|
+
}), db.collection("users").findOne({
|
|
36
|
+
_id: oid
|
|
37
|
+
}, {
|
|
38
|
+
projection: {
|
|
39
|
+
access: 1
|
|
40
|
+
}
|
|
41
|
+
})]);
|
|
42
|
+
if (profile) profileAccess = profile.access;
|
|
43
|
+
if (user) userAccess = user.access;
|
|
44
|
+
}
|
|
45
|
+
return isPageviewAdmin({
|
|
46
|
+
jwtAccess: (_req$user2 = req.user) === null || _req$user2 === void 0 ? void 0 : _req$user2.access,
|
|
47
|
+
profileAccess,
|
|
48
|
+
userAccess
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
router.get("/mine", async (req, res) => {
|
|
52
|
+
try {
|
|
53
|
+
const db = await getDb();
|
|
54
|
+
const rows = await db.collection("powrFeedback").find({
|
|
55
|
+
projectId: String(req.projectId),
|
|
56
|
+
createdBy: String(req.user.powrId || "")
|
|
57
|
+
}).sort({
|
|
58
|
+
createdAt: -1
|
|
59
|
+
}).limit(100).toArray();
|
|
60
|
+
return res.json({
|
|
61
|
+
success: true,
|
|
62
|
+
data: rows.map(feedbackSummary)
|
|
63
|
+
});
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error("GET /feedback/mine", error);
|
|
66
|
+
return res.status(500).json({
|
|
67
|
+
success: false,
|
|
68
|
+
message: "Failed to list your notes."
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
router.get("/", async (req, res) => {
|
|
73
|
+
try {
|
|
74
|
+
const db = await getDb();
|
|
75
|
+
if (!(await callerIsAdmin(db, req))) {
|
|
76
|
+
return res.status(403).json({
|
|
77
|
+
success: false,
|
|
78
|
+
message: "Access denied."
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const rows = await db.collection("powrFeedback").find({
|
|
82
|
+
projectId: String(req.projectId)
|
|
83
|
+
}).sort({
|
|
84
|
+
createdAt: -1
|
|
85
|
+
}).limit(200).toArray();
|
|
86
|
+
return res.json({
|
|
87
|
+
success: true,
|
|
88
|
+
count: rows.length,
|
|
89
|
+
data: rows.map(feedbackSummary)
|
|
90
|
+
});
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error("GET /feedback", error);
|
|
93
|
+
return res.status(500).json({
|
|
94
|
+
success: false,
|
|
95
|
+
message: "Failed to list feedback."
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
router.post("/", async (req, res) => {
|
|
100
|
+
try {
|
|
101
|
+
let fields;
|
|
102
|
+
try {
|
|
103
|
+
fields = parseFeedbackCreate(req.body || {});
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return res.status(error.statusCode || 400).json({
|
|
106
|
+
success: false,
|
|
107
|
+
message: error.message
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const now = new Date();
|
|
111
|
+
const doc = {
|
|
112
|
+
projectId: String(req.projectId),
|
|
113
|
+
title: fields.title,
|
|
114
|
+
body: fields.body,
|
|
115
|
+
kind: fields.kind,
|
|
116
|
+
status: "open",
|
|
117
|
+
createdBy: String(req.user.powrId || ""),
|
|
118
|
+
createdByName: String(req.user.fullName || "").trim(),
|
|
119
|
+
createdAt: now,
|
|
120
|
+
updatedAt: now
|
|
121
|
+
};
|
|
122
|
+
const db = await getDb();
|
|
123
|
+
const result = await db.collection("powrFeedback").insertOne(doc);
|
|
124
|
+
return res.status(201).json({
|
|
125
|
+
success: true,
|
|
126
|
+
data: feedbackSummary({
|
|
127
|
+
...doc,
|
|
128
|
+
_id: result.insertedId
|
|
129
|
+
})
|
|
130
|
+
});
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error("POST /feedback", error);
|
|
133
|
+
return res.status(500).json({
|
|
134
|
+
success: false,
|
|
135
|
+
message: "Failed to save feedback."
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
router.patch("/:id", async (req, res) => {
|
|
140
|
+
try {
|
|
141
|
+
const id = String(req.params.id || "");
|
|
142
|
+
if (!ObjectId.isValid(id) || id.length !== 24) {
|
|
143
|
+
return res.status(400).json({
|
|
144
|
+
success: false,
|
|
145
|
+
message: "Invalid note id."
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
let status;
|
|
149
|
+
try {
|
|
150
|
+
status = parseFeedbackStatus(req.body || {});
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return res.status(error.statusCode || 400).json({
|
|
153
|
+
success: false,
|
|
154
|
+
message: error.message
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const db = await getDb();
|
|
158
|
+
if (!(await callerIsAdmin(db, req))) {
|
|
159
|
+
return res.status(403).json({
|
|
160
|
+
success: false,
|
|
161
|
+
message: "Access denied."
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
const result = await db.collection("powrFeedback").findOneAndUpdate({
|
|
165
|
+
_id: new ObjectId(id),
|
|
166
|
+
projectId: String(req.projectId)
|
|
167
|
+
}, {
|
|
168
|
+
$set: {
|
|
169
|
+
status,
|
|
170
|
+
updatedAt: new Date()
|
|
171
|
+
}
|
|
172
|
+
}, {
|
|
173
|
+
returnDocument: "after"
|
|
174
|
+
});
|
|
175
|
+
const row = result && result.value ? result.value : result;
|
|
176
|
+
if (!row || !row._id) {
|
|
177
|
+
return res.status(404).json({
|
|
178
|
+
success: false,
|
|
179
|
+
message: "Note not found."
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return res.json({
|
|
183
|
+
success: true,
|
|
184
|
+
data: feedbackSummary(row)
|
|
185
|
+
});
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.error("PATCH /feedback/:id", error);
|
|
188
|
+
return res.status(500).json({
|
|
189
|
+
success: false,
|
|
190
|
+
message: "Failed to update feedback."
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
module.exports = router;
|
package/dist/routes/index.js
CHANGED
|
@@ -15,6 +15,7 @@ const {
|
|
|
15
15
|
const commentsRoutes = require('./comments');
|
|
16
16
|
const filesRoutes = require('./files');
|
|
17
17
|
const formsRoutes = require('./forms');
|
|
18
|
+
const feedbackRoutes = require('./feedback');
|
|
18
19
|
const invoicesRoutes = require('./invoices');
|
|
19
20
|
const likesRoutes = require('./likes');
|
|
20
21
|
const functionsRoutes = require('./functions');
|
|
@@ -45,6 +46,7 @@ const createPowrRoutes = (options = {}) => {
|
|
|
45
46
|
router.use('/comments', verifyToken, commentsRoutes);
|
|
46
47
|
router.use('/files', verifyToken, filesRoutes);
|
|
47
48
|
router.use('/forms', formsRoutes);
|
|
49
|
+
router.use('/feedback', verifyToken, feedbackRoutes);
|
|
48
50
|
router.use('/invoices', verifyToken, invoicesRoutes);
|
|
49
51
|
router.use('/likes', verifyToken, likesRoutes);
|
|
50
52
|
router.use('/functions', verifyToken, functionsRoutes);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const KINDS = ["app", "other"];
|
|
4
|
+
const STATUSES = ["open", "handled"];
|
|
5
|
+
function parseFeedbackCreate(body) {
|
|
6
|
+
const title = String((body === null || body === void 0 ? void 0 : body.title) || "").trim();
|
|
7
|
+
if (!title) {
|
|
8
|
+
const error = new Error("Title is required.");
|
|
9
|
+
error.statusCode = 400;
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
if (title.length > 140) {
|
|
13
|
+
const error = new Error("Title must be under 140 characters.");
|
|
14
|
+
error.statusCode = 400;
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
const note = String((body === null || body === void 0 ? void 0 : body.body) || "").trim();
|
|
18
|
+
if (note.length > 4000) {
|
|
19
|
+
const error = new Error("Note must be under 4000 characters.");
|
|
20
|
+
error.statusCode = 400;
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
const kind = String((body === null || body === void 0 ? void 0 : body.kind) || "app").trim().toLowerCase();
|
|
24
|
+
if (!KINDS.includes(kind)) {
|
|
25
|
+
const error = new Error("Kind must be app or other.");
|
|
26
|
+
error.statusCode = 400;
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
title,
|
|
31
|
+
body: note,
|
|
32
|
+
kind
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function parseFeedbackStatus(body) {
|
|
36
|
+
const status = String((body === null || body === void 0 ? void 0 : body.status) || "").trim().toLowerCase();
|
|
37
|
+
if (!STATUSES.includes(status)) {
|
|
38
|
+
const error = new Error("Status must be open or handled.");
|
|
39
|
+
error.statusCode = 400;
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
return status;
|
|
43
|
+
}
|
|
44
|
+
function feedbackSummary(row) {
|
|
45
|
+
return {
|
|
46
|
+
_id: String(row._id),
|
|
47
|
+
projectId: String(row.projectId || ""),
|
|
48
|
+
title: row.title || "",
|
|
49
|
+
body: row.body || "",
|
|
50
|
+
kind: row.kind || "app",
|
|
51
|
+
status: row.status || "open",
|
|
52
|
+
createdBy: row.createdBy ? String(row.createdBy) : "",
|
|
53
|
+
createdByName: row.createdByName || "",
|
|
54
|
+
createdAt: row.createdAt || null,
|
|
55
|
+
updatedAt: row.updatedAt || null
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
module.exports = {
|
|
59
|
+
KINDS,
|
|
60
|
+
STATUSES,
|
|
61
|
+
parseFeedbackCreate,
|
|
62
|
+
parseFeedbackStatus,
|
|
63
|
+
feedbackSummary
|
|
64
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
parseFeedbackCreate,
|
|
5
|
+
parseFeedbackStatus,
|
|
6
|
+
feedbackSummary
|
|
7
|
+
} = require("./feedback");
|
|
8
|
+
describe("parseFeedbackCreate", () => {
|
|
9
|
+
it("keeps title, note, and kind", () => {
|
|
10
|
+
expect(parseFeedbackCreate({
|
|
11
|
+
title: " Map is blank ",
|
|
12
|
+
body: "After login",
|
|
13
|
+
kind: "APP"
|
|
14
|
+
})).toEqual({
|
|
15
|
+
title: "Map is blank",
|
|
16
|
+
body: "After login",
|
|
17
|
+
kind: "app"
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
it("defaults kind to app", () => {
|
|
21
|
+
expect(parseFeedbackCreate({
|
|
22
|
+
title: "Hello"
|
|
23
|
+
}).kind).toBe("app");
|
|
24
|
+
});
|
|
25
|
+
it("rejects an empty title", () => {
|
|
26
|
+
expect(() => parseFeedbackCreate({
|
|
27
|
+
title: " "
|
|
28
|
+
})).toThrow("Title is required.");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe("parseFeedbackStatus", () => {
|
|
32
|
+
it("accepts handled", () => {
|
|
33
|
+
expect(parseFeedbackStatus({
|
|
34
|
+
status: "Handled"
|
|
35
|
+
})).toBe("handled");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
describe("feedbackSummary", () => {
|
|
39
|
+
it("returns the admin fields", () => {
|
|
40
|
+
const row = feedbackSummary({
|
|
41
|
+
_id: "abc",
|
|
42
|
+
projectId: "p1",
|
|
43
|
+
title: "Late rider",
|
|
44
|
+
body: "Twenty minutes",
|
|
45
|
+
kind: "other",
|
|
46
|
+
status: "open",
|
|
47
|
+
createdBy: "u1",
|
|
48
|
+
createdByName: "Ravi",
|
|
49
|
+
createdAt: "2026-10-06"
|
|
50
|
+
});
|
|
51
|
+
expect(row.title).toBe("Late rider");
|
|
52
|
+
expect(row.kind).toBe("other");
|
|
53
|
+
expect(row.createdByName).toBe("Ravi");
|
|
54
|
+
});
|
|
55
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.15.
|
|
3
|
+
"version": "4.15.3",
|
|
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",
|