powr-sdk-api 4.14.0 → 4.14.1
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/track.js +38 -3
- package/dist/utils/track.js +9 -0
- package/dist/utils/track.test.js +23 -0
- package/package.json +1 -1
package/dist/routes/track.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const express = require("express");
|
|
4
|
+
const {
|
|
5
|
+
ObjectId
|
|
6
|
+
} = require("mongodb");
|
|
4
7
|
const router = express.Router();
|
|
5
8
|
const {
|
|
6
9
|
getDb
|
|
@@ -10,8 +13,41 @@ const {
|
|
|
10
13
|
PAGEVIEW_LIST_LIMIT,
|
|
11
14
|
PAGEVIEW_LIST_FIELDS,
|
|
12
15
|
buildPageview,
|
|
16
|
+
isPageviewAdmin,
|
|
13
17
|
recentPageviewFilter
|
|
14
18
|
} = require("../utils/track");
|
|
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
|
+
}
|
|
15
51
|
let indexesPromise = null;
|
|
16
52
|
function ensurePageviewIndexes(db) {
|
|
17
53
|
if (!indexesPromise) {
|
|
@@ -35,14 +71,13 @@ function ensurePageviewIndexes(db) {
|
|
|
35
71
|
}
|
|
36
72
|
router.get("/", async (req, res) => {
|
|
37
73
|
try {
|
|
38
|
-
|
|
39
|
-
if (
|
|
74
|
+
const db = await getDb();
|
|
75
|
+
if (!(await callerIsAdmin(db, req))) {
|
|
40
76
|
return res.status(403).json({
|
|
41
77
|
success: false,
|
|
42
78
|
message: "Only an admin can view page views."
|
|
43
79
|
});
|
|
44
80
|
}
|
|
45
|
-
const db = await getDb();
|
|
46
81
|
await ensurePageviewIndexes(db);
|
|
47
82
|
const rows = await db.collection("pageviews").find(recentPageviewFilter(req.projectId), {
|
|
48
83
|
projection: PAGEVIEW_LIST_FIELDS
|
package/dist/utils/track.js
CHANGED
|
@@ -114,6 +114,14 @@ function buildPageview({
|
|
|
114
114
|
doc
|
|
115
115
|
};
|
|
116
116
|
}
|
|
117
|
+
function isPageviewAdmin({
|
|
118
|
+
jwtAccess,
|
|
119
|
+
profileAccess,
|
|
120
|
+
userAccess
|
|
121
|
+
}) {
|
|
122
|
+
const chosen = profileAccess !== undefined && profileAccess !== null && profileAccess !== "" ? profileAccess : userAccess !== undefined && userAccess !== null && userAccess !== "" ? userAccess : jwtAccess;
|
|
123
|
+
return Number(chosen) === 100;
|
|
124
|
+
}
|
|
117
125
|
function recentPageviewFilter(projectId, now = new Date()) {
|
|
118
126
|
return {
|
|
119
127
|
projectId: String(projectId || ""),
|
|
@@ -127,5 +135,6 @@ module.exports = {
|
|
|
127
135
|
PAGEVIEW_LIST_LIMIT,
|
|
128
136
|
PAGEVIEW_LIST_FIELDS,
|
|
129
137
|
buildPageview,
|
|
138
|
+
isPageviewAdmin,
|
|
130
139
|
recentPageviewFilter
|
|
131
140
|
};
|
package/dist/utils/track.test.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const {
|
|
4
4
|
buildPageview,
|
|
5
5
|
PAGEVIEW_TTL_SECONDS,
|
|
6
|
+
isPageviewAdmin,
|
|
6
7
|
recentPageviewFilter
|
|
7
8
|
} = require("./track");
|
|
8
9
|
const projectId = "507f1f77bcf86cd799439011";
|
|
@@ -64,6 +65,28 @@ describe("buildPageview", () => {
|
|
|
64
65
|
expect(result.doc.referrer).toBeUndefined();
|
|
65
66
|
});
|
|
66
67
|
});
|
|
68
|
+
describe("isPageviewAdmin", () => {
|
|
69
|
+
it("allows the project profile or the user record when the token has no access", () => {
|
|
70
|
+
expect(isPageviewAdmin({
|
|
71
|
+
jwtAccess: undefined,
|
|
72
|
+
profileAccess: 100
|
|
73
|
+
})).toBe(true);
|
|
74
|
+
expect(isPageviewAdmin({
|
|
75
|
+
jwtAccess: undefined,
|
|
76
|
+
userAccess: 100
|
|
77
|
+
})).toBe(true);
|
|
78
|
+
expect(isPageviewAdmin({
|
|
79
|
+
jwtAccess: 100
|
|
80
|
+
})).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
it("lets a project profile of 0 override a user record of 100", () => {
|
|
83
|
+
expect(isPageviewAdmin({
|
|
84
|
+
jwtAccess: undefined,
|
|
85
|
+
profileAccess: 0,
|
|
86
|
+
userAccess: 100
|
|
87
|
+
})).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
67
90
|
describe("recentPageviewFilter", () => {
|
|
68
91
|
it("keeps the last 7 days for one project", () => {
|
|
69
92
|
const now = new Date("2026-09-29T00:00:00.000Z");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.14.
|
|
3
|
+
"version": "4.14.1",
|
|
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",
|