powr-sdk-api 4.16.1 → 4.18.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 +6 -1
- package/dist/routes/posts.js +757 -0
- package/dist/utils/eventLog.js +117 -0
- package/dist/utils/eventLog.test.js +120 -0
- package/dist/utils/posts.js +155 -0
- package/dist/utils/posts.test.js +78 -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');
|
|
@@ -35,6 +36,7 @@ const toolsRoutes = require('./tools');
|
|
|
35
36
|
const tasksRoutes = require('./tasks');
|
|
36
37
|
const preferencesRoutes = require('./preferences');
|
|
37
38
|
const schedulerRoutes = require('./scheduler');
|
|
39
|
+
const postsRoutes = require('./posts');
|
|
38
40
|
|
|
39
41
|
// Synchronous router creation
|
|
40
42
|
const createPowrRoutes = (options = {}) => {
|
|
@@ -56,7 +58,8 @@ const createPowrRoutes = (options = {}) => {
|
|
|
56
58
|
router.use('/users/me/preferences', verifyToken, preferencesRoutes);
|
|
57
59
|
router.use('/users', verifyTokenOrServiceKey, usersRoutes);
|
|
58
60
|
router.use('/waitlists', verifyToken, waitlistsRoutes);
|
|
59
|
-
router.use('/activities',
|
|
61
|
+
router.use('/activities', verifyTokenOrServiceKey, activitiesRoutes);
|
|
62
|
+
router.use('/audit', verifyTokenOrServiceKey, auditRoutes);
|
|
60
63
|
router.use('/track', verifyToken, trackRoutes);
|
|
61
64
|
router.use('/slides', verifyToken, slidesRoutes);
|
|
62
65
|
router.use('/notifications', verifyToken, notificationsRoutes);
|
|
@@ -66,6 +69,8 @@ const createPowrRoutes = (options = {}) => {
|
|
|
66
69
|
router.use('/tools', verifyToken, toolsRoutes);
|
|
67
70
|
router.use('/tasks', verifyToken, tasksRoutes);
|
|
68
71
|
router.use('/scheduler', verifyTokenOrServiceKey, schedulerRoutes);
|
|
72
|
+
// Public reads stay open. Writer and editor routes check the token themselves.
|
|
73
|
+
router.use('/posts', postsRoutes);
|
|
69
74
|
return router;
|
|
70
75
|
};
|
|
71
76
|
|