powr-sdk-api 4.13.0 → 4.14.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.
@@ -7,7 +7,10 @@ const {
7
7
  } = require("../services/mongo");
8
8
  const {
9
9
  PAGEVIEW_TTL_SECONDS,
10
- buildPageview
10
+ PAGEVIEW_LIST_LIMIT,
11
+ PAGEVIEW_LIST_FIELDS,
12
+ buildPageview,
13
+ recentPageviewFilter
11
14
  } = require("../utils/track");
12
15
  let indexesPromise = null;
13
16
  function ensurePageviewIndexes(db) {
@@ -30,6 +33,34 @@ function ensurePageviewIndexes(db) {
30
33
  }
31
34
  return indexesPromise;
32
35
  }
36
+ router.get("/", async (req, res) => {
37
+ try {
38
+ var _req$user;
39
+ if (Number((_req$user = req.user) === null || _req$user === void 0 ? void 0 : _req$user.access) !== 100) {
40
+ return res.status(403).json({
41
+ success: false,
42
+ message: "Only an admin can view page views."
43
+ });
44
+ }
45
+ const db = await getDb();
46
+ await ensurePageviewIndexes(db);
47
+ const rows = await db.collection("pageviews").find(recentPageviewFilter(req.projectId), {
48
+ projection: PAGEVIEW_LIST_FIELDS
49
+ }).sort({
50
+ createdAt: -1
51
+ }).limit(PAGEVIEW_LIST_LIMIT).toArray();
52
+ return res.json({
53
+ success: true,
54
+ data: rows
55
+ });
56
+ } catch (error) {
57
+ console.error("Error listing pageviews:", error);
58
+ return res.status(500).json({
59
+ success: false,
60
+ message: "Failed to load page views."
61
+ });
62
+ }
63
+ });
33
64
  router.post("/", async (req, res) => {
34
65
  try {
35
66
  const built = buildPageview({
@@ -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,18 @@ function buildPageview({
104
114
  doc
105
115
  };
106
116
  }
117
+ function recentPageviewFilter(projectId, now = new Date()) {
118
+ return {
119
+ projectId: String(projectId || ""),
120
+ createdAt: {
121
+ $gte: new Date(now.getTime() - PAGEVIEW_WINDOW_MS)
122
+ }
123
+ };
124
+ }
107
125
  module.exports = {
108
126
  PAGEVIEW_TTL_SECONDS,
109
- buildPageview
127
+ PAGEVIEW_LIST_LIMIT,
128
+ PAGEVIEW_LIST_FIELDS,
129
+ buildPageview,
130
+ recentPageviewFilter
110
131
  };
@@ -2,7 +2,8 @@
2
2
 
3
3
  const {
4
4
  buildPageview,
5
- PAGEVIEW_TTL_SECONDS
5
+ PAGEVIEW_TTL_SECONDS,
6
+ recentPageviewFilter
6
7
  } = require("./track");
7
8
  const projectId = "507f1f77bcf86cd799439011";
8
9
  const member = "aaaaaaaaaaaaaaaaaaaaaaaa";
@@ -63,6 +64,14 @@ describe("buildPageview", () => {
63
64
  expect(result.doc.referrer).toBeUndefined();
64
65
  });
65
66
  });
67
+ describe("recentPageviewFilter", () => {
68
+ it("keeps the last 7 days for one project", () => {
69
+ const now = new Date("2026-09-29T00:00:00.000Z");
70
+ const filter = recentPageviewFilter(projectId, now);
71
+ expect(filter.projectId).toBe(projectId);
72
+ expect(filter.createdAt.$gte.toISOString()).toBe("2026-09-22T00:00:00.000Z");
73
+ });
74
+ });
66
75
  describe("pageview ttl", () => {
67
76
  it("expires after 30 days", () => {
68
77
  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.13.0",
3
+ "version": "4.14.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",