@plusscommunities/pluss-core-aws 2.0.26-beta.0 → 2.0.26
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.
|
@@ -3,7 +3,8 @@ const moment = require("moment");
|
|
|
3
3
|
const checkActivityExists = require("./checkActivityExists");
|
|
4
4
|
const updateRef = require("../common/updateRef");
|
|
5
5
|
const getRole = require("../users/getRole");
|
|
6
|
-
const
|
|
6
|
+
const getUser = require("../users/getUser");
|
|
7
|
+
const { getMultiRowId, getRowId, log } = require("../../helper");
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Creates an analytics entry
|
|
@@ -42,33 +43,56 @@ module.exports = async (
|
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
// 3. user.category doesn't exist on the user object — category (staff/primary/linked)
|
|
51
|
-
// lives on the role record.
|
|
52
|
-
// Centralising here avoids burdening call sites with role resolution and guarantees
|
|
53
|
-
// accuracy (always reads current role, not potentially stale caller-provided values).
|
|
54
|
-
// getRole is a DynamoDB point-read (~5ms) and this runs async, so no user-facing latency impact.
|
|
46
|
+
// Never fail on a missing user — persist with null enrichment.
|
|
47
|
+
const actor = user || {};
|
|
48
|
+
|
|
49
|
+
// Type/category are per-site role facts, so resolve them here — callers only
|
|
50
|
+
// carry minimal user objects.
|
|
55
51
|
let userType = null;
|
|
56
52
|
let userCategory = null;
|
|
53
|
+
let enrichmentFailed = false;
|
|
57
54
|
|
|
58
55
|
try {
|
|
59
|
-
const role = await getRole(site,
|
|
56
|
+
const role = await getRole(site, actor.id);
|
|
60
57
|
if (role) {
|
|
61
58
|
userType = role.type || null;
|
|
62
59
|
userCategory = role.category || null;
|
|
63
60
|
}
|
|
64
61
|
} catch (e) {
|
|
65
|
-
//
|
|
62
|
+
// Log so an outage is distinguishable from a missing role, then fall
|
|
63
|
+
// through to the master fallback.
|
|
64
|
+
enrichmentFailed = true;
|
|
65
|
+
log("logActivity", "RoleLookupFailed", {
|
|
66
|
+
site,
|
|
67
|
+
userId: actor.id || null,
|
|
68
|
+
err: e,
|
|
69
|
+
});
|
|
66
70
|
}
|
|
67
71
|
|
|
68
|
-
//
|
|
69
|
-
|
|
72
|
+
// Master fallback: previews omit type (undefined ≠ the system user's explicit
|
|
73
|
+
// null), so resolve it from the user record.
|
|
74
|
+
let primaryType = actor.type;
|
|
75
|
+
if (!userType && primaryType === undefined && actor.id) {
|
|
76
|
+
try {
|
|
77
|
+
const fullUser = await getUser(actor.id);
|
|
78
|
+
primaryType = fullUser ? fullUser.type : null;
|
|
79
|
+
} catch (e) {
|
|
80
|
+
// Best-effort — log so a lookup failure is distinguishable from a
|
|
81
|
+
// genuinely uncategorised user, but never block the activity write.
|
|
82
|
+
enrichmentFailed = true;
|
|
83
|
+
log("logActivity", "EnrichmentLookupFailed", {
|
|
84
|
+
site,
|
|
85
|
+
userId: actor.id,
|
|
86
|
+
err: e,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!userType && primaryType === "master") {
|
|
70
91
|
userType = "master";
|
|
71
|
-
|
|
92
|
+
}
|
|
93
|
+
// Masters count as staff for category filtering.
|
|
94
|
+
if (userType === "master" && !userCategory) {
|
|
95
|
+
userCategory = "staff";
|
|
72
96
|
}
|
|
73
97
|
|
|
74
98
|
// Log the activity
|
|
@@ -78,8 +102,8 @@ module.exports = async (
|
|
|
78
102
|
Timestamp: timestamp || moment.utc().valueOf(),
|
|
79
103
|
EntityType: entityType,
|
|
80
104
|
SubjectId: entityId,
|
|
81
|
-
UserId:
|
|
82
|
-
User: user,
|
|
105
|
+
UserId: actor.id || null,
|
|
106
|
+
User: user || null,
|
|
83
107
|
Site: site,
|
|
84
108
|
EntityId: getRowId(entityId, entityType),
|
|
85
109
|
ActivityId: getMultiRowId([entityId, entityType, actionType]),
|
|
@@ -88,6 +112,14 @@ module.exports = async (
|
|
|
88
112
|
userCategory,
|
|
89
113
|
};
|
|
90
114
|
|
|
115
|
+
// An unresolved lookup failure means enrichment didn't happen — omit the
|
|
116
|
+
// keys so aggregation applies pre-enrichment totals semantics rather than
|
|
117
|
+
// recording a falsely "enriched but uncategorised" event.
|
|
118
|
+
if (enrichmentFailed && userType === null && userCategory === null) {
|
|
119
|
+
delete activity.userType;
|
|
120
|
+
delete activity.userCategory;
|
|
121
|
+
}
|
|
122
|
+
|
|
91
123
|
await updateRef(`analytics`, activity);
|
|
92
124
|
return true;
|
|
93
125
|
};
|
package/package.json
CHANGED