powr-sdk-api 4.14.1 → 4.15.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.
@@ -11,7 +11,6 @@ const {
11
11
  const {
12
12
  PAGEVIEW_TTL_SECONDS,
13
13
  PAGEVIEW_LIST_LIMIT,
14
- PAGEVIEW_LIST_FIELDS,
15
14
  buildPageview,
16
15
  isPageviewAdmin,
17
16
  recentPageviewFilter
@@ -79,14 +78,66 @@ router.get("/", async (req, res) => {
79
78
  });
80
79
  }
81
80
  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();
81
+ const filter = recentPageviewFilter(req.projectId);
82
+ const [pages, grouped] = await Promise.all([db.collection("pageviews").countDocuments(filter), db.collection("pageviews").aggregate([{
83
+ $match: filter
84
+ }, {
85
+ $group: {
86
+ _id: "$userId",
87
+ lastSeen: {
88
+ $max: "$createdAt"
89
+ },
90
+ pages: {
91
+ $sum: 1
92
+ }
93
+ }
94
+ }, {
95
+ $sort: {
96
+ lastSeen: -1
97
+ }
98
+ }, {
99
+ $limit: PAGEVIEW_LIST_LIMIT
100
+ }]).toArray()]);
101
+ const ids = grouped.map(row => String(row._id || "")).filter(id => id.length === 24 && ObjectId.isValid(id)).map(id => new ObjectId(id));
102
+ const [users, profiles] = await Promise.all([ids.length ? db.collection("users").find({
103
+ _id: {
104
+ $in: ids
105
+ }
106
+ }, {
107
+ projection: {
108
+ fullName: 1
109
+ }
110
+ }).toArray() : [], ids.length ? db.collection("profiles").find({
111
+ userId: {
112
+ $in: [...ids, ...ids.map(id => String(id))]
113
+ },
114
+ projectId: String(req.projectId || "")
115
+ }, {
116
+ projection: {
117
+ userId: 1,
118
+ access: 1
119
+ }
120
+ }).toArray() : []]);
121
+ const names = new Map(users.map(user => [String(user._id), user.fullName || ""]));
122
+ const accessByUser = new Map(profiles.map(profile => [String(profile.userId), profile.access]));
123
+ const people = grouped.map(row => {
124
+ const userId = String(row._id || "");
125
+ const name = String(names.get(userId) || "").trim();
126
+ return {
127
+ userId,
128
+ name: name || "Unknown",
129
+ access: accessByUser.has(userId) ? accessByUser.get(userId) : null,
130
+ lastSeen: row.lastSeen,
131
+ pages: row.pages
132
+ };
133
+ });
87
134
  return res.json({
88
135
  success: true,
89
- data: rows
136
+ data: {
137
+ visited: people.length,
138
+ pages,
139
+ people
140
+ }
90
141
  });
91
142
  } catch (error) {
92
143
  console.error("Error listing pageviews:", error);
@@ -122,6 +122,37 @@ function isPageviewAdmin({
122
122
  const chosen = profileAccess !== undefined && profileAccess !== null && profileAccess !== "" ? profileAccess : userAccess !== undefined && userAccess !== null && userAccess !== "" ? userAccess : jwtAccess;
123
123
  return Number(chosen) === 100;
124
124
  }
125
+ function summarizeVisitors(rows) {
126
+ const byUser = new Map();
127
+ let pages = 0;
128
+ for (const row of rows || []) {
129
+ pages += 1;
130
+ const userId = String(row && row.userId || "");
131
+ if (!userId) continue;
132
+ const seenAt = row.createdAt ? new Date(row.createdAt) : null;
133
+ const prev = byUser.get(userId);
134
+ if (!prev) {
135
+ byUser.set(userId, {
136
+ userId,
137
+ lastSeen: seenAt,
138
+ pages: 1
139
+ });
140
+ continue;
141
+ }
142
+ prev.pages += 1;
143
+ if (seenAt && (!prev.lastSeen || seenAt > prev.lastSeen)) prev.lastSeen = seenAt;
144
+ }
145
+ const people = [...byUser.values()].sort((a, b) => {
146
+ const left = a.lastSeen ? a.lastSeen.getTime() : 0;
147
+ const right = b.lastSeen ? b.lastSeen.getTime() : 0;
148
+ return right - left;
149
+ });
150
+ return {
151
+ pages,
152
+ visited: people.length,
153
+ people
154
+ };
155
+ }
125
156
  function recentPageviewFilter(projectId, now = new Date()) {
126
157
  return {
127
158
  projectId: String(projectId || ""),
@@ -136,5 +167,6 @@ module.exports = {
136
167
  PAGEVIEW_LIST_FIELDS,
137
168
  buildPageview,
138
169
  isPageviewAdmin,
170
+ summarizeVisitors,
139
171
  recentPageviewFilter
140
172
  };
@@ -4,6 +4,7 @@ const {
4
4
  buildPageview,
5
5
  PAGEVIEW_TTL_SECONDS,
6
6
  isPageviewAdmin,
7
+ summarizeVisitors,
7
8
  recentPageviewFilter
8
9
  } = require("./track");
9
10
  const projectId = "507f1f77bcf86cd799439011";
@@ -87,6 +88,30 @@ describe("isPageviewAdmin", () => {
87
88
  })).toBe(false);
88
89
  });
89
90
  });
91
+ describe("summarizeVisitors", () => {
92
+ it("collapses a path log into one person and a page count", () => {
93
+ const result = summarizeVisitors([{
94
+ userId: member,
95
+ page: "/login",
96
+ createdAt: new Date("2026-09-29T02:14:40.000Z")
97
+ }, {
98
+ userId: member,
99
+ page: "/my-invoices",
100
+ createdAt: new Date("2026-09-29T02:15:14.000Z")
101
+ }, {
102
+ userId: admin,
103
+ page: "/",
104
+ createdAt: new Date("2026-09-29T02:15:37.000Z")
105
+ }]);
106
+ expect(result.pages).toBe(3);
107
+ expect(result.visited).toBe(2);
108
+ expect(result.people[0].userId).toBe(admin);
109
+ expect(result.people[1]).toMatchObject({
110
+ userId: member,
111
+ pages: 2
112
+ });
113
+ });
114
+ });
90
115
  describe("recentPageviewFilter", () => {
91
116
  it("keeps the last 7 days for one project", () => {
92
117
  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.1",
3
+ "version": "4.15.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",