powr-sdk-api 4.16.1 → 4.17.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.
- package/dist/index.js +5 -1
- package/dist/managers/activities.js +154 -0
- package/dist/managers/activities.test.js +154 -0
- package/dist/managers/audit.js +26 -0
- package/dist/managers/audit.test.js +64 -0
- package/dist/managers/index.js +9 -1
- package/dist/routes/activities.js +60 -17
- package/dist/routes/audit.js +80 -0
- package/dist/routes/index.js +3 -1
- package/dist/utils/eventLog.js +117 -0
- package/dist/utils/eventLog.test.js +120 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -22,7 +22,9 @@ const {
|
|
|
22
22
|
initializeTools,
|
|
23
23
|
executeTasks,
|
|
24
24
|
executeTool,
|
|
25
|
-
createTask
|
|
25
|
+
createTask,
|
|
26
|
+
emitActivity,
|
|
27
|
+
emitAudit
|
|
26
28
|
} = require("./managers");
|
|
27
29
|
const {
|
|
28
30
|
verifyToken
|
|
@@ -48,5 +50,7 @@ module.exports = {
|
|
|
48
50
|
executeTasks,
|
|
49
51
|
executeTool,
|
|
50
52
|
createTask,
|
|
53
|
+
emitActivity,
|
|
54
|
+
emitAudit,
|
|
51
55
|
getPowrDb
|
|
52
56
|
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
parseEvent,
|
|
5
|
+
buildEventDoc,
|
|
6
|
+
buildListQuery,
|
|
7
|
+
defaultGetDb
|
|
8
|
+
} = require("../utils/eventLog");
|
|
9
|
+
const COLLECTION = "activities";
|
|
10
|
+
const NOTIFY_PATH = "/ops/notify";
|
|
11
|
+
const FORWARD_MS = 8000;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Frozen Pulse notify contract (Gate C codes against this).
|
|
15
|
+
* Activities only. Audit never calls this.
|
|
16
|
+
*
|
|
17
|
+
* POST {PULSE_API_URL}/ops/notify
|
|
18
|
+
* Header: X-Service-Key: {PULSE_SERVICE_KEY}
|
|
19
|
+
* Body: {
|
|
20
|
+
* activityId, projectId, action, userIds[],
|
|
21
|
+
* title, body, link, actorId, targetType, targetId
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* Host never sends FCM. Unset PULSE_API_URL → store the activity, skip notify.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
function pulseNotifyUrl(env = process.env) {
|
|
28
|
+
const base = String(env && env.PULSE_API_URL || "").trim().replace(/\/$/, "");
|
|
29
|
+
if (!base) return null;
|
|
30
|
+
return `${base}${NOTIFY_PATH}`;
|
|
31
|
+
}
|
|
32
|
+
function buildPulseNotifyBody({
|
|
33
|
+
activityId,
|
|
34
|
+
parsed,
|
|
35
|
+
notify
|
|
36
|
+
}) {
|
|
37
|
+
return {
|
|
38
|
+
activityId: String(activityId),
|
|
39
|
+
projectId: notify.projectId || parsed.projectId,
|
|
40
|
+
action: parsed.action,
|
|
41
|
+
userIds: notify.userIds,
|
|
42
|
+
title: notify.title,
|
|
43
|
+
body: notify.body,
|
|
44
|
+
link: notify.link,
|
|
45
|
+
actorId: parsed.userId != null ? String(parsed.userId) : null,
|
|
46
|
+
targetType: parsed.targetType,
|
|
47
|
+
targetId: parsed.targetId != null ? String(parsed.targetId) : null
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async function forwardPulseNotify({
|
|
51
|
+
activityId,
|
|
52
|
+
parsed,
|
|
53
|
+
notify,
|
|
54
|
+
fetchImpl,
|
|
55
|
+
env
|
|
56
|
+
}) {
|
|
57
|
+
const url = pulseNotifyUrl(env);
|
|
58
|
+
if (!url) {
|
|
59
|
+
return {
|
|
60
|
+
forwarded: false,
|
|
61
|
+
skipped: "PULSE_API_URL unset"
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const key = String(env && env.PULSE_SERVICE_KEY || "").trim();
|
|
65
|
+
if (!key) {
|
|
66
|
+
return {
|
|
67
|
+
forwarded: false,
|
|
68
|
+
skipped: "PULSE_SERVICE_KEY unset"
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const body = buildPulseNotifyBody({
|
|
72
|
+
activityId,
|
|
73
|
+
parsed,
|
|
74
|
+
notify
|
|
75
|
+
});
|
|
76
|
+
const impl = fetchImpl || global.fetch;
|
|
77
|
+
if (typeof impl !== "function") {
|
|
78
|
+
return {
|
|
79
|
+
forwarded: false,
|
|
80
|
+
skipped: "fetch unavailable"
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const controller = new AbortController();
|
|
84
|
+
const timer = setTimeout(() => controller.abort(), FORWARD_MS);
|
|
85
|
+
try {
|
|
86
|
+
const response = await impl(url, {
|
|
87
|
+
method: "POST",
|
|
88
|
+
redirect: "manual",
|
|
89
|
+
cache: "no-store",
|
|
90
|
+
signal: controller.signal,
|
|
91
|
+
headers: {
|
|
92
|
+
"Content-Type": "application/json",
|
|
93
|
+
"X-Service-Key": key
|
|
94
|
+
},
|
|
95
|
+
body: JSON.stringify(body)
|
|
96
|
+
});
|
|
97
|
+
const status = Number(response && response.status);
|
|
98
|
+
const ok = status >= 200 && status < 300;
|
|
99
|
+
return {
|
|
100
|
+
forwarded: ok,
|
|
101
|
+
status: Number.isFinite(status) ? status : null,
|
|
102
|
+
error: ok ? null : `HTTP ${status}`
|
|
103
|
+
};
|
|
104
|
+
} catch (error) {
|
|
105
|
+
const timedOut = error && (error.name === "AbortError" || error.name === "TimeoutError");
|
|
106
|
+
return {
|
|
107
|
+
forwarded: false,
|
|
108
|
+
status: null,
|
|
109
|
+
error: timedOut ? "Timed out" : "Request failed"
|
|
110
|
+
};
|
|
111
|
+
} finally {
|
|
112
|
+
clearTimeout(timer);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function emitActivity(options, deps = {}) {
|
|
116
|
+
const parsed = parseEvent(options, {
|
|
117
|
+
allowNotify: true
|
|
118
|
+
});
|
|
119
|
+
const getDbImpl = deps.getDb || defaultGetDb;
|
|
120
|
+
const db = await getDbImpl();
|
|
121
|
+
const doc = buildEventDoc(parsed, deps.now || new Date());
|
|
122
|
+
const result = await db.collection(COLLECTION).insertOne(doc);
|
|
123
|
+
const insertedId = result.insertedId;
|
|
124
|
+
let notify = null;
|
|
125
|
+
if (parsed.notify) {
|
|
126
|
+
notify = await forwardPulseNotify({
|
|
127
|
+
activityId: insertedId,
|
|
128
|
+
parsed,
|
|
129
|
+
notify: parsed.notify,
|
|
130
|
+
fetchImpl: deps.fetchImpl || global.fetch,
|
|
131
|
+
env: deps.env || process.env
|
|
132
|
+
});
|
|
133
|
+
console.log("pulse notify", {
|
|
134
|
+
activityId: String(insertedId),
|
|
135
|
+
forwarded: notify.forwarded === true,
|
|
136
|
+
status: notify.status == null ? null : notify.status,
|
|
137
|
+
skipped: notify.skipped || null,
|
|
138
|
+
error: notify.error || null
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
insertedId,
|
|
143
|
+
notify
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
module.exports = {
|
|
147
|
+
COLLECTION,
|
|
148
|
+
NOTIFY_PATH,
|
|
149
|
+
emitActivity,
|
|
150
|
+
buildListQuery,
|
|
151
|
+
buildPulseNotifyBody,
|
|
152
|
+
forwardPulseNotify,
|
|
153
|
+
pulseNotifyUrl
|
|
154
|
+
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
emitActivity,
|
|
5
|
+
buildPulseNotifyBody,
|
|
6
|
+
forwardPulseNotify,
|
|
7
|
+
pulseNotifyUrl,
|
|
8
|
+
NOTIFY_PATH
|
|
9
|
+
} = require("./activities");
|
|
10
|
+
const {
|
|
11
|
+
parseEvent
|
|
12
|
+
} = require("../utils/eventLog");
|
|
13
|
+
const projectId = "69ca11dbdbc0b193a48e6ccd";
|
|
14
|
+
const actor = "6893ec11c19c093a5bf21720";
|
|
15
|
+
const other = "aaaaaaaaaaaaaaaaaaaaaaaa";
|
|
16
|
+
function validEmit(extra) {
|
|
17
|
+
return {
|
|
18
|
+
projectId,
|
|
19
|
+
action: "task_assigned",
|
|
20
|
+
userId: actor,
|
|
21
|
+
targetType: "task",
|
|
22
|
+
targetId: other,
|
|
23
|
+
metadata: {
|
|
24
|
+
source: "spriny-api"
|
|
25
|
+
},
|
|
26
|
+
...extra
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
describe("buildPulseNotifyBody", () => {
|
|
30
|
+
it("is the Gate C contract — activities only, no kind", () => {
|
|
31
|
+
const parsed = parseEvent(validEmit(), {
|
|
32
|
+
allowNotify: true
|
|
33
|
+
});
|
|
34
|
+
const body = buildPulseNotifyBody({
|
|
35
|
+
activityId: "abc",
|
|
36
|
+
parsed,
|
|
37
|
+
notify: {
|
|
38
|
+
userIds: [other],
|
|
39
|
+
title: "Assigned",
|
|
40
|
+
body: "A task is yours",
|
|
41
|
+
link: "/tasks/1"
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
expect(body).toEqual({
|
|
45
|
+
activityId: "abc",
|
|
46
|
+
projectId,
|
|
47
|
+
action: "task_assigned",
|
|
48
|
+
userIds: [other],
|
|
49
|
+
title: "Assigned",
|
|
50
|
+
body: "A task is yours",
|
|
51
|
+
link: "/tasks/1",
|
|
52
|
+
actorId: actor,
|
|
53
|
+
targetType: "task",
|
|
54
|
+
targetId: other
|
|
55
|
+
});
|
|
56
|
+
expect(body.kind).toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe("forwardPulseNotify", () => {
|
|
60
|
+
const parsed = parseEvent(validEmit({
|
|
61
|
+
notify: {
|
|
62
|
+
userIds: [other],
|
|
63
|
+
title: "Assigned",
|
|
64
|
+
body: "A task is yours",
|
|
65
|
+
link: "/tasks/1"
|
|
66
|
+
}
|
|
67
|
+
}), {
|
|
68
|
+
allowNotify: true
|
|
69
|
+
});
|
|
70
|
+
const notify = parsed.notify;
|
|
71
|
+
it("skips when PULSE_API_URL is unset", async () => {
|
|
72
|
+
const fetchImpl = jest.fn();
|
|
73
|
+
const result = await forwardPulseNotify({
|
|
74
|
+
activityId: "abc",
|
|
75
|
+
parsed,
|
|
76
|
+
notify,
|
|
77
|
+
fetchImpl,
|
|
78
|
+
env: {}
|
|
79
|
+
});
|
|
80
|
+
expect(result).toEqual({
|
|
81
|
+
forwarded: false,
|
|
82
|
+
skipped: "PULSE_API_URL unset"
|
|
83
|
+
});
|
|
84
|
+
expect(fetchImpl).not.toHaveBeenCalled();
|
|
85
|
+
expect(pulseNotifyUrl({})).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
it("POSTs /ops/notify once with the service key", async () => {
|
|
88
|
+
const fetchImpl = jest.fn().mockResolvedValue({
|
|
89
|
+
status: 202
|
|
90
|
+
});
|
|
91
|
+
const result = await forwardPulseNotify({
|
|
92
|
+
activityId: "abc",
|
|
93
|
+
parsed,
|
|
94
|
+
notify,
|
|
95
|
+
fetchImpl,
|
|
96
|
+
env: {
|
|
97
|
+
PULSE_API_URL: "https://pulse.example.test/",
|
|
98
|
+
PULSE_SERVICE_KEY: "pulse-key"
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
expect(result).toEqual({
|
|
102
|
+
forwarded: true,
|
|
103
|
+
status: 202,
|
|
104
|
+
error: null
|
|
105
|
+
});
|
|
106
|
+
expect(fetchImpl).toHaveBeenCalledTimes(1);
|
|
107
|
+
const [url, init] = fetchImpl.mock.calls[0];
|
|
108
|
+
expect(url).toBe(`https://pulse.example.test${NOTIFY_PATH}`);
|
|
109
|
+
expect(init.method).toBe("POST");
|
|
110
|
+
expect(init.headers["X-Service-Key"]).toBe("pulse-key");
|
|
111
|
+
expect(JSON.parse(init.body).kind).toBeUndefined();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
describe("emitActivity", () => {
|
|
115
|
+
function fakeDb(insertedId = "id1") {
|
|
116
|
+
const insertOne = jest.fn().mockResolvedValue({
|
|
117
|
+
insertedId
|
|
118
|
+
});
|
|
119
|
+
return {
|
|
120
|
+
insertOne,
|
|
121
|
+
getDb: async () => ({
|
|
122
|
+
collection(name) {
|
|
123
|
+
expect(name).toBe("activities");
|
|
124
|
+
return {
|
|
125
|
+
insertOne
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
it("writes the timeline row and skips notify when Pulse env is unset", async () => {
|
|
132
|
+
const {
|
|
133
|
+
getDb,
|
|
134
|
+
insertOne
|
|
135
|
+
} = fakeDb();
|
|
136
|
+
const result = await emitActivity(validEmit({
|
|
137
|
+
notify: {
|
|
138
|
+
userIds: [other],
|
|
139
|
+
title: "Assigned",
|
|
140
|
+
body: "A task is yours"
|
|
141
|
+
}
|
|
142
|
+
}), {
|
|
143
|
+
getDb,
|
|
144
|
+
env: {},
|
|
145
|
+
fetchImpl: jest.fn()
|
|
146
|
+
});
|
|
147
|
+
expect(result.insertedId).toBe("id1");
|
|
148
|
+
expect(result.notify).toEqual({
|
|
149
|
+
forwarded: false,
|
|
150
|
+
skipped: "PULSE_API_URL unset"
|
|
151
|
+
});
|
|
152
|
+
expect(insertOne.mock.calls[0][0].kind).toBeUndefined();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
parseEvent,
|
|
5
|
+
buildEventDoc,
|
|
6
|
+
buildListQuery,
|
|
7
|
+
defaultGetDb
|
|
8
|
+
} = require("../utils/eventLog");
|
|
9
|
+
const COLLECTION = "audit";
|
|
10
|
+
async function emitAudit(options, deps = {}) {
|
|
11
|
+
const parsed = parseEvent(options, {
|
|
12
|
+
allowNotify: false
|
|
13
|
+
});
|
|
14
|
+
const getDbImpl = deps.getDb || defaultGetDb;
|
|
15
|
+
const db = await getDbImpl();
|
|
16
|
+
const doc = buildEventDoc(parsed, deps.now || new Date());
|
|
17
|
+
const result = await db.collection(COLLECTION).insertOne(doc);
|
|
18
|
+
return {
|
|
19
|
+
insertedId: result.insertedId
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
module.exports = {
|
|
23
|
+
COLLECTION,
|
|
24
|
+
emitAudit,
|
|
25
|
+
buildListQuery
|
|
26
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
emitAudit
|
|
5
|
+
} = require("./audit");
|
|
6
|
+
const projectId = "69ca11dbdbc0b193a48e6ccd";
|
|
7
|
+
const actor = "6893ec11c19c093a5bf21720";
|
|
8
|
+
const uuid = "9ad595de-babf-44c4-baec-cb7b82675e47";
|
|
9
|
+
describe("emitAudit", () => {
|
|
10
|
+
function fakeDb(insertedId = "aud1") {
|
|
11
|
+
const insertOne = jest.fn().mockResolvedValue({
|
|
12
|
+
insertedId
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
insertOne,
|
|
16
|
+
getDb: async () => ({
|
|
17
|
+
collection(name) {
|
|
18
|
+
expect(name).toBe("audit");
|
|
19
|
+
return {
|
|
20
|
+
insertOne
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
it("writes the audit table and never forwards Pulse", async () => {
|
|
27
|
+
const fetchImpl = jest.fn();
|
|
28
|
+
const {
|
|
29
|
+
getDb,
|
|
30
|
+
insertOne
|
|
31
|
+
} = fakeDb();
|
|
32
|
+
const result = await emitAudit({
|
|
33
|
+
projectId,
|
|
34
|
+
action: "urbanstall_refund_payment",
|
|
35
|
+
userId: actor,
|
|
36
|
+
targetType: "orders",
|
|
37
|
+
targetId: uuid,
|
|
38
|
+
metadata: {
|
|
39
|
+
source: "urbanstall-api"
|
|
40
|
+
}
|
|
41
|
+
}, {
|
|
42
|
+
getDb,
|
|
43
|
+
fetchImpl
|
|
44
|
+
});
|
|
45
|
+
expect(result.insertedId).toBe("aud1");
|
|
46
|
+
expect(result.notify).toBeUndefined();
|
|
47
|
+
expect(fetchImpl).not.toHaveBeenCalled();
|
|
48
|
+
const doc = insertOne.mock.calls[0][0];
|
|
49
|
+
expect(doc.kind).toBeUndefined();
|
|
50
|
+
expect(doc.targetId).toBe(uuid);
|
|
51
|
+
expect(doc.action).toBe("urbanstall_refund_payment");
|
|
52
|
+
});
|
|
53
|
+
it("rejects a notify payload", async () => {
|
|
54
|
+
await expect(emitAudit({
|
|
55
|
+
projectId,
|
|
56
|
+
action: "urbanstall_order_cancelled",
|
|
57
|
+
notify: {
|
|
58
|
+
userIds: [actor],
|
|
59
|
+
title: "Cancelled",
|
|
60
|
+
body: "No"
|
|
61
|
+
}
|
|
62
|
+
})).rejects.toThrow("audit does not notify");
|
|
63
|
+
});
|
|
64
|
+
});
|
package/dist/managers/index.js
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
const functionsManager = require('./functions');
|
|
4
4
|
const toolsManager = require('./tools');
|
|
5
5
|
const scheduledTasksManager = require('./tasks');
|
|
6
|
+
const {
|
|
7
|
+
emitActivity
|
|
8
|
+
} = require('./activities');
|
|
9
|
+
const {
|
|
10
|
+
emitAudit
|
|
11
|
+
} = require('./audit');
|
|
6
12
|
|
|
7
13
|
// Async Functions initialization function
|
|
8
14
|
const initializeFunctions = async (options = {}) => {
|
|
@@ -33,5 +39,7 @@ module.exports = {
|
|
|
33
39
|
initializeTools,
|
|
34
40
|
executeTasks,
|
|
35
41
|
executeTool,
|
|
36
|
-
createTask
|
|
42
|
+
createTask,
|
|
43
|
+
emitActivity,
|
|
44
|
+
emitAudit
|
|
37
45
|
};
|
|
@@ -5,23 +5,35 @@ const router = express.Router();
|
|
|
5
5
|
const {
|
|
6
6
|
getDb
|
|
7
7
|
} = require("../services/mongo");
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const {
|
|
9
|
+
emitActivity,
|
|
10
|
+
buildListQuery
|
|
11
|
+
} = require("../managers/activities");
|
|
10
12
|
router.get("/", async (req, res) => {
|
|
11
13
|
try {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
let query;
|
|
15
|
+
let limit;
|
|
16
|
+
try {
|
|
17
|
+
({
|
|
18
|
+
query,
|
|
19
|
+
limit
|
|
20
|
+
} = buildListQuery({
|
|
21
|
+
projectId: req.projectId,
|
|
22
|
+
contentId: req.query.contentId,
|
|
23
|
+
action: req.query.action,
|
|
24
|
+
source: req.query.source,
|
|
25
|
+
limit: req.query.limit
|
|
26
|
+
}));
|
|
27
|
+
} catch (error) {
|
|
28
|
+
return res.status(error.statusCode || 400).json({
|
|
29
|
+
success: false,
|
|
30
|
+
message: error.message
|
|
31
|
+
});
|
|
22
32
|
}
|
|
23
33
|
const db = await getDb();
|
|
24
|
-
const activities = await db.collection("activities").find(query).
|
|
34
|
+
const activities = await db.collection("activities").find(query).sort({
|
|
35
|
+
createdAt: -1
|
|
36
|
+
}).limit(limit).toArray();
|
|
25
37
|
return res.json({
|
|
26
38
|
success: true,
|
|
27
39
|
data: activities
|
|
@@ -34,16 +46,13 @@ router.get("/", async (req, res) => {
|
|
|
34
46
|
});
|
|
35
47
|
}
|
|
36
48
|
});
|
|
37
|
-
|
|
38
|
-
// Create new activity
|
|
39
49
|
router.post("/", async (req, res) => {
|
|
40
50
|
try {
|
|
41
51
|
const {
|
|
42
52
|
contentId,
|
|
43
53
|
activity
|
|
44
54
|
} = req.body;
|
|
45
|
-
const projectId = req.projectId;
|
|
46
|
-
|
|
55
|
+
const projectId = req.projectId;
|
|
47
56
|
if (!contentId || !activity) {
|
|
48
57
|
return res.status(400).json({
|
|
49
58
|
success: false,
|
|
@@ -71,4 +80,38 @@ router.post("/", async (req, res) => {
|
|
|
71
80
|
});
|
|
72
81
|
}
|
|
73
82
|
});
|
|
83
|
+
router.post("/emit", async (req, res) => {
|
|
84
|
+
try {
|
|
85
|
+
const body = req.body || {};
|
|
86
|
+
const {
|
|
87
|
+
insertedId,
|
|
88
|
+
notify
|
|
89
|
+
} = await emitActivity({
|
|
90
|
+
projectId: req.projectId || body.projectId,
|
|
91
|
+
action: body.action,
|
|
92
|
+
userId: body.userId,
|
|
93
|
+
targetType: body.targetType,
|
|
94
|
+
targetId: body.targetId,
|
|
95
|
+
metadata: body.metadata,
|
|
96
|
+
notify: body.notify
|
|
97
|
+
});
|
|
98
|
+
return res.status(201).json({
|
|
99
|
+
success: true,
|
|
100
|
+
id: insertedId && insertedId.toString ? insertedId.toString() : String(insertedId),
|
|
101
|
+
notify
|
|
102
|
+
});
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (error.statusCode) {
|
|
105
|
+
return res.status(error.statusCode).json({
|
|
106
|
+
success: false,
|
|
107
|
+
message: error.message
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
console.error("activities emit:", error);
|
|
111
|
+
return res.status(500).json({
|
|
112
|
+
success: false,
|
|
113
|
+
message: error.message || "Failed to emit activity"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
});
|
|
74
117
|
module.exports = router;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const router = express.Router();
|
|
5
|
+
const {
|
|
6
|
+
getDb
|
|
7
|
+
} = require("../services/mongo");
|
|
8
|
+
const {
|
|
9
|
+
emitAudit,
|
|
10
|
+
buildListQuery
|
|
11
|
+
} = require("../managers/audit");
|
|
12
|
+
router.get("/", async (req, res) => {
|
|
13
|
+
try {
|
|
14
|
+
let query;
|
|
15
|
+
let limit;
|
|
16
|
+
try {
|
|
17
|
+
({
|
|
18
|
+
query,
|
|
19
|
+
limit
|
|
20
|
+
} = buildListQuery({
|
|
21
|
+
projectId: req.projectId,
|
|
22
|
+
action: req.query.action,
|
|
23
|
+
source: req.query.source,
|
|
24
|
+
limit: req.query.limit
|
|
25
|
+
}));
|
|
26
|
+
} catch (error) {
|
|
27
|
+
return res.status(error.statusCode || 400).json({
|
|
28
|
+
success: false,
|
|
29
|
+
message: error.message
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const db = await getDb();
|
|
33
|
+
const rows = await db.collection("audit").find(query).sort({
|
|
34
|
+
createdAt: -1
|
|
35
|
+
}).limit(limit).toArray();
|
|
36
|
+
return res.json({
|
|
37
|
+
success: true,
|
|
38
|
+
data: rows
|
|
39
|
+
});
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error("Error retrieving audit:", error);
|
|
42
|
+
return res.status(500).json({
|
|
43
|
+
success: false,
|
|
44
|
+
message: "Failed to retrieve audit."
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
router.post("/emit", async (req, res) => {
|
|
49
|
+
try {
|
|
50
|
+
const body = req.body || {};
|
|
51
|
+
const {
|
|
52
|
+
insertedId
|
|
53
|
+
} = await emitAudit({
|
|
54
|
+
projectId: req.projectId || body.projectId,
|
|
55
|
+
action: body.action,
|
|
56
|
+
userId: body.userId,
|
|
57
|
+
targetType: body.targetType,
|
|
58
|
+
targetId: body.targetId,
|
|
59
|
+
metadata: body.metadata,
|
|
60
|
+
notify: body.notify
|
|
61
|
+
});
|
|
62
|
+
return res.status(201).json({
|
|
63
|
+
success: true,
|
|
64
|
+
id: insertedId && insertedId.toString ? insertedId.toString() : String(insertedId)
|
|
65
|
+
});
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (error.statusCode) {
|
|
68
|
+
return res.status(error.statusCode).json({
|
|
69
|
+
success: false,
|
|
70
|
+
message: error.message
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
console.error("audit emit:", error);
|
|
74
|
+
return res.status(500).json({
|
|
75
|
+
success: false,
|
|
76
|
+
message: error.message || "Failed to emit audit"
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
module.exports = router;
|
package/dist/routes/index.js
CHANGED
|
@@ -23,6 +23,7 @@ const ratingsRoutes = require('./ratings');
|
|
|
23
23
|
const usersRoutes = require('./users');
|
|
24
24
|
const waitlistsRoutes = require('./waitlists');
|
|
25
25
|
const activitiesRoutes = require('./activities');
|
|
26
|
+
const auditRoutes = require('./audit');
|
|
26
27
|
const trackRoutes = require('./track');
|
|
27
28
|
const authRoutes = require('./auth');
|
|
28
29
|
const loginCodeRoutes = require('./loginCode');
|
|
@@ -56,7 +57,8 @@ const createPowrRoutes = (options = {}) => {
|
|
|
56
57
|
router.use('/users/me/preferences', verifyToken, preferencesRoutes);
|
|
57
58
|
router.use('/users', verifyTokenOrServiceKey, usersRoutes);
|
|
58
59
|
router.use('/waitlists', verifyToken, waitlistsRoutes);
|
|
59
|
-
router.use('/activities',
|
|
60
|
+
router.use('/activities', verifyTokenOrServiceKey, activitiesRoutes);
|
|
61
|
+
router.use('/audit', verifyTokenOrServiceKey, auditRoutes);
|
|
60
62
|
router.use('/track', verifyToken, trackRoutes);
|
|
61
63
|
router.use('/slides', verifyToken, slidesRoutes);
|
|
62
64
|
router.use('/notifications', verifyToken, notificationsRoutes);
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
ObjectId
|
|
5
|
+
} = require("mongodb");
|
|
6
|
+
const MAX_USER_IDS = 100;
|
|
7
|
+
function reject(message, statusCode = 400) {
|
|
8
|
+
const error = new Error(message);
|
|
9
|
+
error.statusCode = statusCode;
|
|
10
|
+
throw error;
|
|
11
|
+
}
|
|
12
|
+
function isObjectIdString(id) {
|
|
13
|
+
return typeof id === "string" && id.length === 24 && ObjectId.isValid(id);
|
|
14
|
+
}
|
|
15
|
+
function normalizeId(id) {
|
|
16
|
+
if (id == null || id === "") return null;
|
|
17
|
+
if (typeof id === "string" && isObjectIdString(id)) return new ObjectId(id);
|
|
18
|
+
return id;
|
|
19
|
+
}
|
|
20
|
+
function parseNotify(notify) {
|
|
21
|
+
if (notify == null) return null;
|
|
22
|
+
if (typeof notify !== "object" || Array.isArray(notify)) {
|
|
23
|
+
reject("notify must be an object.");
|
|
24
|
+
}
|
|
25
|
+
if (!Array.isArray(notify.userIds)) {
|
|
26
|
+
reject("notify.userIds is required.");
|
|
27
|
+
}
|
|
28
|
+
const userIds = notify.userIds.map(id => String(id || "").trim()).filter(Boolean);
|
|
29
|
+
if (userIds.length === 0) reject("notify.userIds is required.");
|
|
30
|
+
if (userIds.length > MAX_USER_IDS) {
|
|
31
|
+
reject("notify.userIds must be 100 or fewer.");
|
|
32
|
+
}
|
|
33
|
+
const title = String(notify.title || "").trim();
|
|
34
|
+
if (!title) reject("notify.title is required.");
|
|
35
|
+
if (title.length > 140) reject("notify.title must be under 140 characters.");
|
|
36
|
+
const body = String(notify.body || "").trim();
|
|
37
|
+
if (!body) reject("notify.body is required.");
|
|
38
|
+
if (body.length > 2000) reject("notify.body must be under 2000 characters.");
|
|
39
|
+
const linkRaw = notify.link == null ? "" : String(notify.link).trim();
|
|
40
|
+
if (linkRaw.length > 500) reject("notify.link must be under 500 characters.");
|
|
41
|
+
const projectId = notify.projectId == null || notify.projectId === "" ? null : String(notify.projectId).trim();
|
|
42
|
+
return {
|
|
43
|
+
userIds,
|
|
44
|
+
title,
|
|
45
|
+
body,
|
|
46
|
+
link: linkRaw || null,
|
|
47
|
+
projectId
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function parseEvent(options, {
|
|
51
|
+
allowNotify = false
|
|
52
|
+
} = {}) {
|
|
53
|
+
const source = options || {};
|
|
54
|
+
const projectId = String(source.projectId || "").trim();
|
|
55
|
+
const action = String(source.action || "").trim();
|
|
56
|
+
if (!projectId) reject("projectId is required.");
|
|
57
|
+
if (!action) reject("action is required.");
|
|
58
|
+
if (action.length > 80) reject("action must be under 80 characters.");
|
|
59
|
+
if (source.kind != null && source.kind !== "") {
|
|
60
|
+
reject("kind is not used. POST /activities/emit or POST /audit/emit.");
|
|
61
|
+
}
|
|
62
|
+
if (!allowNotify && source.notify != null) {
|
|
63
|
+
reject("audit does not notify.");
|
|
64
|
+
}
|
|
65
|
+
const metadata = source.metadata && typeof source.metadata === "object" && !Array.isArray(source.metadata) ? source.metadata : {};
|
|
66
|
+
return {
|
|
67
|
+
projectId,
|
|
68
|
+
action,
|
|
69
|
+
userId: normalizeId(source.userId),
|
|
70
|
+
targetType: source.targetType ? String(source.targetType).trim() : null,
|
|
71
|
+
targetId: normalizeId(source.targetId),
|
|
72
|
+
metadata,
|
|
73
|
+
notify: allowNotify ? parseNotify(source.notify) : null
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function buildEventDoc(parsed, now = new Date()) {
|
|
77
|
+
return {
|
|
78
|
+
projectId: parsed.projectId,
|
|
79
|
+
action: parsed.action,
|
|
80
|
+
userId: parsed.userId,
|
|
81
|
+
targetType: parsed.targetType,
|
|
82
|
+
targetId: parsed.targetId,
|
|
83
|
+
metadata: parsed.metadata,
|
|
84
|
+
createdAt: now
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function buildListQuery({
|
|
88
|
+
projectId,
|
|
89
|
+
contentId,
|
|
90
|
+
action,
|
|
91
|
+
source,
|
|
92
|
+
limit
|
|
93
|
+
} = {}) {
|
|
94
|
+
const query = {
|
|
95
|
+
projectId
|
|
96
|
+
};
|
|
97
|
+
if (contentId) query.contentId = contentId;
|
|
98
|
+
if (action) query.action = String(action);
|
|
99
|
+
if (source) query["metadata.source"] = String(source);
|
|
100
|
+
const lim = Math.min(Math.max(parseInt(limit, 10) || 100, 1), 500);
|
|
101
|
+
return {
|
|
102
|
+
query,
|
|
103
|
+
limit: lim
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function defaultGetDb() {
|
|
107
|
+
return require("../services/mongo").getDb();
|
|
108
|
+
}
|
|
109
|
+
module.exports = {
|
|
110
|
+
reject,
|
|
111
|
+
normalizeId,
|
|
112
|
+
parseNotify,
|
|
113
|
+
parseEvent,
|
|
114
|
+
buildEventDoc,
|
|
115
|
+
buildListQuery,
|
|
116
|
+
defaultGetDb
|
|
117
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
ObjectId
|
|
5
|
+
} = require("mongodb");
|
|
6
|
+
const {
|
|
7
|
+
parseEvent,
|
|
8
|
+
parseNotify,
|
|
9
|
+
buildEventDoc,
|
|
10
|
+
buildListQuery,
|
|
11
|
+
normalizeId
|
|
12
|
+
} = require("./eventLog");
|
|
13
|
+
const projectId = "69ca11dbdbc0b193a48e6ccd";
|
|
14
|
+
const actor = "6893ec11c19c093a5bf21720";
|
|
15
|
+
const other = "aaaaaaaaaaaaaaaaaaaaaaaa";
|
|
16
|
+
const uuid = "9ad595de-babf-44c4-baec-cb7b82675e47";
|
|
17
|
+
function validEvent(extra) {
|
|
18
|
+
return {
|
|
19
|
+
projectId,
|
|
20
|
+
action: "task_assigned",
|
|
21
|
+
userId: actor,
|
|
22
|
+
targetType: "task",
|
|
23
|
+
targetId: other,
|
|
24
|
+
metadata: {
|
|
25
|
+
source: "spriny-api"
|
|
26
|
+
},
|
|
27
|
+
...extra
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
describe("parseEvent", () => {
|
|
31
|
+
it("requires projectId and action", () => {
|
|
32
|
+
expect(() => parseEvent({})).toThrow("projectId");
|
|
33
|
+
expect(() => parseEvent({
|
|
34
|
+
projectId
|
|
35
|
+
})).toThrow("action");
|
|
36
|
+
});
|
|
37
|
+
it("rejects kind — use the activity or audit door", () => {
|
|
38
|
+
expect(() => parseEvent(validEvent({
|
|
39
|
+
kind: "audit"
|
|
40
|
+
}))).toThrow("kind is not used");
|
|
41
|
+
});
|
|
42
|
+
it("keeps a UUID targetId as a string", () => {
|
|
43
|
+
const parsed = parseEvent(validEvent({
|
|
44
|
+
targetId: uuid
|
|
45
|
+
}));
|
|
46
|
+
expect(parsed.targetId).toBe(uuid);
|
|
47
|
+
expect(parsed.userId).toBeInstanceOf(ObjectId);
|
|
48
|
+
expect(String(parsed.userId)).toBe(actor);
|
|
49
|
+
});
|
|
50
|
+
it("rejects notify on audit", () => {
|
|
51
|
+
expect(() => parseEvent(validEvent({
|
|
52
|
+
notify: {
|
|
53
|
+
userIds: [other],
|
|
54
|
+
title: "Ping",
|
|
55
|
+
body: "Hi"
|
|
56
|
+
}
|
|
57
|
+
}), {
|
|
58
|
+
allowNotify: false
|
|
59
|
+
})).toThrow("audit does not notify");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe("normalizeId", () => {
|
|
63
|
+
it("does not coerce a 12-character string", () => {
|
|
64
|
+
expect(normalizeId("hello-worlds")).toBe("hello-worlds");
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
describe("parseNotify", () => {
|
|
68
|
+
it("returns null when omitted", () => {
|
|
69
|
+
expect(parseNotify(undefined)).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
it("requires userIds, title, and body", () => {
|
|
72
|
+
expect(() => parseNotify({})).toThrow("userIds");
|
|
73
|
+
expect(() => parseNotify({
|
|
74
|
+
userIds: [actor]
|
|
75
|
+
})).toThrow("title");
|
|
76
|
+
expect(() => parseNotify({
|
|
77
|
+
userIds: [actor],
|
|
78
|
+
title: "Ping"
|
|
79
|
+
})).toThrow("body");
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
describe("buildListQuery", () => {
|
|
83
|
+
it("filters action and source, and caps limit", () => {
|
|
84
|
+
const {
|
|
85
|
+
query,
|
|
86
|
+
limit
|
|
87
|
+
} = buildListQuery({
|
|
88
|
+
projectId,
|
|
89
|
+
action: "urbanstall_refund_payment",
|
|
90
|
+
source: "urbanstall-api",
|
|
91
|
+
limit: "999"
|
|
92
|
+
});
|
|
93
|
+
expect(query).toEqual({
|
|
94
|
+
projectId,
|
|
95
|
+
action: "urbanstall_refund_payment",
|
|
96
|
+
"metadata.source": "urbanstall-api"
|
|
97
|
+
});
|
|
98
|
+
expect(query.kind).toBeUndefined();
|
|
99
|
+
expect(limit).toBe(500);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
describe("buildEventDoc", () => {
|
|
103
|
+
it("does not persist kind or notify on the row", () => {
|
|
104
|
+
const parsed = parseEvent(validEvent({
|
|
105
|
+
notify: {
|
|
106
|
+
userIds: [other],
|
|
107
|
+
title: "Assigned",
|
|
108
|
+
body: "A task is yours"
|
|
109
|
+
}
|
|
110
|
+
}), {
|
|
111
|
+
allowNotify: true
|
|
112
|
+
});
|
|
113
|
+
const now = new Date("2026-10-12T00:00:00.000Z");
|
|
114
|
+
const doc = buildEventDoc(parsed, now);
|
|
115
|
+
expect(doc.kind).toBeUndefined();
|
|
116
|
+
expect(doc.notify).toBeUndefined();
|
|
117
|
+
expect(doc.createdAt).toBe(now);
|
|
118
|
+
expect(doc.action).toBe("task_assigned");
|
|
119
|
+
});
|
|
120
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "powr-sdk-api",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.17.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",
|