powr-sdk-api 4.7.2 → 4.8.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 CHANGED
@@ -22,7 +22,8 @@ const {
22
22
  initializeTools,
23
23
  executeTasks,
24
24
  executeTool,
25
- createTask
25
+ createTask,
26
+ emitActivity
26
27
  } = require("./managers");
27
28
  const {
28
29
  verifyToken
@@ -44,5 +45,6 @@ module.exports = {
44
45
  executeTasks,
45
46
  executeTool,
46
47
  createTask,
48
+ emitActivity,
47
49
  getPowrDb
48
50
  };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ const {
4
+ getDb
5
+ } = require("../services/mongo");
6
+ const {
7
+ ObjectId
8
+ } = require("mongodb");
9
+
10
+ /**
11
+ * Emit an activity to PowrBase. Inserts one document into the activities collection.
12
+ * Does not send notification; an Atlas trigger on the collection handles that.
13
+ *
14
+ * @param {Object} options
15
+ * @param {string} options.projectId - Project context (required)
16
+ * @param {string} options.action - e.g. "task_created", "task_assigned", "enquiry_converted"
17
+ * @param {string|ObjectId} options.userId - Who performed the action
18
+ * @param {string} [options.targetType] - e.g. "task", "project", "enquiry"
19
+ * @param {string|ObjectId} [options.targetId] - ID of the target entity
20
+ * @param {Object} [options.metadata] - Extra payload (e.g. recipientIds, title, body, url for notification)
21
+ * @returns {Promise<{ insertedId: ObjectId }>} The inserted document's _id
22
+ */
23
+ async function emitActivity(options) {
24
+ const {
25
+ projectId,
26
+ action,
27
+ userId,
28
+ targetType,
29
+ targetId,
30
+ metadata = {}
31
+ } = options;
32
+ if (!projectId || !action) {
33
+ throw new Error("emitActivity: projectId and action are required");
34
+ }
35
+ const normalizeId = id => {
36
+ if (id == null) return null;
37
+ if (typeof id === "string" && ObjectId.isValid(id)) return new ObjectId(id);
38
+ return id;
39
+ };
40
+ const doc = {
41
+ projectId,
42
+ action,
43
+ userId: normalizeId(userId),
44
+ targetType: targetType || null,
45
+ targetId: normalizeId(targetId),
46
+ metadata,
47
+ createdAt: new Date()
48
+ };
49
+ const db = await getDb();
50
+ const result = await db.collection("activities").insertOne(doc);
51
+ return {
52
+ insertedId: result.insertedId
53
+ };
54
+ }
55
+ module.exports = {
56
+ emitActivity
57
+ };
@@ -3,6 +3,7 @@
3
3
  const functionsManager = require('./functions');
4
4
  const toolsManager = require('./tools');
5
5
  const scheduledTasksManager = require('./tasks');
6
+ const activitiesManager = require('./activities');
6
7
 
7
8
  // Async Functions initialization function
8
9
  const initializeFunctions = async (options = {}) => {
@@ -27,11 +28,12 @@ const createTask = async taskData => {
27
28
  // Create a new task/workflow
28
29
  return await scheduledTasksManager.createTask(taskData);
29
30
  };
31
+ const emitActivity = activitiesManager.emitActivity;
30
32
  module.exports = {
31
- // Initialization/Execution methods
32
33
  initializeFunctions,
33
34
  initializeTools,
34
35
  executeTasks,
35
36
  executeTool,
36
- createTask
37
+ createTask,
38
+ emitActivity
37
39
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "4.7.2",
3
+ "version": "4.8.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",