powr-sdk-api 4.13.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 +67 -1
- package/dist/utils/track.js +31 -1
- package/dist/utils/track.test.js +33 -1
- package/package.json +1 -1
package/dist/routes/track.js
CHANGED
|
@@ -1,14 +1,53 @@
|
|
|
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
|
|
7
10
|
} = require("../services/mongo");
|
|
8
11
|
const {
|
|
9
12
|
PAGEVIEW_TTL_SECONDS,
|
|
10
|
-
|
|
13
|
+
PAGEVIEW_LIST_LIMIT,
|
|
14
|
+
PAGEVIEW_LIST_FIELDS,
|
|
15
|
+
buildPageview,
|
|
16
|
+
isPageviewAdmin,
|
|
17
|
+
recentPageviewFilter
|
|
11
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
|
+
}
|
|
12
51
|
let indexesPromise = null;
|
|
13
52
|
function ensurePageviewIndexes(db) {
|
|
14
53
|
if (!indexesPromise) {
|
|
@@ -30,6 +69,33 @@ function ensurePageviewIndexes(db) {
|
|
|
30
69
|
}
|
|
31
70
|
return indexesPromise;
|
|
32
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: "Only an admin can view page views."
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
await ensurePageviewIndexes(db);
|
|
82
|
+
const rows = await db.collection("pageviews").find(recentPageviewFilter(req.projectId), {
|
|
83
|
+
projection: PAGEVIEW_LIST_FIELDS
|
|
84
|
+
}).sort({
|
|
85
|
+
createdAt: -1
|
|
86
|
+
}).limit(PAGEVIEW_LIST_LIMIT).toArray();
|
|
87
|
+
return res.json({
|
|
88
|
+
success: true,
|
|
89
|
+
data: rows
|
|
90
|
+
});
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error("Error listing pageviews:", error);
|
|
93
|
+
return res.status(500).json({
|
|
94
|
+
success: false,
|
|
95
|
+
message: "Failed to load page views."
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
33
99
|
router.post("/", async (req, res) => {
|
|
34
100
|
try {
|
|
35
101
|
const built = buildPageview({
|
package/dist/utils/track.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const PAGEVIEW_TTL_SECONDS = 30 * 24 * 60 * 60;
|
|
4
|
+
const PAGEVIEW_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
5
|
+
const PAGEVIEW_LIST_LIMIT = 200;
|
|
6
|
+
const PAGEVIEW_LIST_FIELDS = {
|
|
7
|
+
projectId: 1,
|
|
8
|
+
userId: 1,
|
|
9
|
+
page: 1,
|
|
10
|
+
session: 1,
|
|
11
|
+
referrer: 1,
|
|
12
|
+
createdAt: 1
|
|
13
|
+
};
|
|
4
14
|
const MAX_PATH = 300;
|
|
5
15
|
const MAX_SESSION = 64;
|
|
6
16
|
function stripQueryAndHash(value) {
|
|
@@ -104,7 +114,27 @@ function buildPageview({
|
|
|
104
114
|
doc
|
|
105
115
|
};
|
|
106
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
|
+
}
|
|
125
|
+
function recentPageviewFilter(projectId, now = new Date()) {
|
|
126
|
+
return {
|
|
127
|
+
projectId: String(projectId || ""),
|
|
128
|
+
createdAt: {
|
|
129
|
+
$gte: new Date(now.getTime() - PAGEVIEW_WINDOW_MS)
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
107
133
|
module.exports = {
|
|
108
134
|
PAGEVIEW_TTL_SECONDS,
|
|
109
|
-
|
|
135
|
+
PAGEVIEW_LIST_LIMIT,
|
|
136
|
+
PAGEVIEW_LIST_FIELDS,
|
|
137
|
+
buildPageview,
|
|
138
|
+
isPageviewAdmin,
|
|
139
|
+
recentPageviewFilter
|
|
110
140
|
};
|
package/dist/utils/track.test.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
4
|
buildPageview,
|
|
5
|
-
PAGEVIEW_TTL_SECONDS
|
|
5
|
+
PAGEVIEW_TTL_SECONDS,
|
|
6
|
+
isPageviewAdmin,
|
|
7
|
+
recentPageviewFilter
|
|
6
8
|
} = require("./track");
|
|
7
9
|
const projectId = "507f1f77bcf86cd799439011";
|
|
8
10
|
const member = "aaaaaaaaaaaaaaaaaaaaaaaa";
|
|
@@ -63,6 +65,36 @@ describe("buildPageview", () => {
|
|
|
63
65
|
expect(result.doc.referrer).toBeUndefined();
|
|
64
66
|
});
|
|
65
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
|
+
});
|
|
90
|
+
describe("recentPageviewFilter", () => {
|
|
91
|
+
it("keeps the last 7 days for one project", () => {
|
|
92
|
+
const now = new Date("2026-09-29T00:00:00.000Z");
|
|
93
|
+
const filter = recentPageviewFilter(projectId, now);
|
|
94
|
+
expect(filter.projectId).toBe(projectId);
|
|
95
|
+
expect(filter.createdAt.$gte.toISOString()).toBe("2026-09-22T00:00:00.000Z");
|
|
96
|
+
});
|
|
97
|
+
});
|
|
66
98
|
describe("pageview ttl", () => {
|
|
67
99
|
it("expires after 30 days", () => {
|
|
68
100
|
expect(PAGEVIEW_TTL_SECONDS).toBe(30 * 24 * 60 * 60);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.
|
|
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",
|