@plusscommunities/pluss-core-aws 2.0.23 → 2.0.24-beta.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/helper/notifySiteConfigs.js +117 -0
- package/package.json +2 -2
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Notify SiteConfigs Service of Operational Data Changes
|
|
3
|
+
*
|
|
4
|
+
* This helper provides a pre-filtering mechanism for auto-propagation.
|
|
5
|
+
* When operational data changes (sites, usertypes, interfaces, strings, jobTypes),
|
|
6
|
+
* this function:
|
|
7
|
+
*
|
|
8
|
+
* 1. Queries SourceSiteIdIndex to check if any templates use this site as source
|
|
9
|
+
* 2. If none found → early return (no Lambda invoke, 99% of cases)
|
|
10
|
+
* 3. If templates found → invokes siteConfigs Lambda to handle republishing
|
|
11
|
+
*
|
|
12
|
+
* This hybrid approach ensures:
|
|
13
|
+
* - No Lambda latency for most sites (pre-filter catches non-source sites)
|
|
14
|
+
* - Publishing logic stays encapsulated in siteConfigs service
|
|
15
|
+
* - Minimal cross-service coupling
|
|
16
|
+
*
|
|
17
|
+
* Usage:
|
|
18
|
+
* const notifySiteConfigs = require("@plusscommunities/pluss-core-aws/helper/notifySiteConfigs");
|
|
19
|
+
* await notifySiteConfigs(siteId, logId);
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const AWS = require("aws-sdk");
|
|
23
|
+
const indexQuery = require("../db/common/indexQuery");
|
|
24
|
+
const { log } = require("./index");
|
|
25
|
+
|
|
26
|
+
const lambda = new AWS.Lambda();
|
|
27
|
+
|
|
28
|
+
const TABLE_NAME = "siteconfigs";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check if a site has any templates using it as a source
|
|
32
|
+
* @param {string} siteId - The site ID to check
|
|
33
|
+
* @returns {Promise<Array>} Array of template IDs (empty if none)
|
|
34
|
+
*/
|
|
35
|
+
const getTemplatesForSourceSite = async (siteId) => {
|
|
36
|
+
try {
|
|
37
|
+
const result = await indexQuery(TABLE_NAME, {
|
|
38
|
+
IndexName: "SourceSiteIdIndex",
|
|
39
|
+
KeyConditionExpression: "SourceSiteId = :sourceSiteId",
|
|
40
|
+
ExpressionAttributeValues: {
|
|
41
|
+
":sourceSiteId": siteId,
|
|
42
|
+
},
|
|
43
|
+
ProjectionExpression: "Id", // Only need IDs for pre-filter
|
|
44
|
+
});
|
|
45
|
+
return (result.Items || []).map((item) => item.Id);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
// Table or index might not exist in all deployments
|
|
48
|
+
log("notifySiteConfigs", "QueryError", { siteId, error: err.message });
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Invoke the siteConfigs operationalDataChanged Lambda
|
|
55
|
+
* @param {string} siteId - The source site that changed
|
|
56
|
+
* @param {Array<string>} templateIds - List of template IDs to republish
|
|
57
|
+
* @param {string} logId - Log ID for tracing
|
|
58
|
+
*/
|
|
59
|
+
const invokeSiteConfigsLambda = async (siteId, templateIds, logId) => {
|
|
60
|
+
// Build function name from environment
|
|
61
|
+
// Format: {client}-siteConfigs-{stage}-operationalDataChanged
|
|
62
|
+
const client = process.env.client || "dev";
|
|
63
|
+
const stage = process.env.stage || "dev";
|
|
64
|
+
const functionName = `${client}-siteConfigs-${stage}-operationalDataChanged`;
|
|
65
|
+
|
|
66
|
+
const payload = {
|
|
67
|
+
siteId,
|
|
68
|
+
templateIds,
|
|
69
|
+
logId,
|
|
70
|
+
source: "notifySiteConfigs",
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
log("notifySiteConfigs", "InvokingLambda", { functionName, siteId, templateCount: templateIds.length }, logId);
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
await lambda
|
|
77
|
+
.invoke({
|
|
78
|
+
FunctionName: functionName,
|
|
79
|
+
InvocationType: "Event", // Async invocation
|
|
80
|
+
Payload: JSON.stringify(payload),
|
|
81
|
+
})
|
|
82
|
+
.promise();
|
|
83
|
+
|
|
84
|
+
log("notifySiteConfigs", "LambdaInvoked", { functionName, siteId }, logId);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
log("notifySiteConfigs", "LambdaError", { functionName, siteId, error: err.message }, logId);
|
|
87
|
+
// Don't throw - this is a non-critical operation
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Notify siteConfigs service that operational data has changed for a site.
|
|
93
|
+
* Performs pre-filtering to avoid unnecessary Lambda invocations.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} siteId - The site ID where operational data changed
|
|
96
|
+
* @param {string} logId - Optional log ID for tracing
|
|
97
|
+
*/
|
|
98
|
+
const notifySiteConfigs = async (siteId, logId) => {
|
|
99
|
+
if (!siteId) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Pre-filter: Check if this site is a source for any templates
|
|
104
|
+
const templateIds = await getTemplatesForSourceSite(siteId);
|
|
105
|
+
|
|
106
|
+
if (templateIds.length === 0) {
|
|
107
|
+
// No Lambda invocation needed
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
log("notifySiteConfigs", "TemplatesFound", { siteId, count: templateIds.length }, logId);
|
|
112
|
+
|
|
113
|
+
// Invoke siteConfigs Lambda to handle republishing
|
|
114
|
+
await invokeSiteConfigsLambda(siteId, templateIds, logId);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
module.exports = notifySiteConfigs;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plusscommunities/pluss-core-aws",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.24-beta.0",
|
|
4
4
|
"description": "Core extension package for Pluss Communities platform",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"betapatch": "npm version prepatch --preid=beta",
|
|
@@ -34,4 +34,4 @@
|
|
|
34
34
|
"serverless-domain-manager": "^3.3.1",
|
|
35
35
|
"serverless-prune-plugin": "^1.4.1"
|
|
36
36
|
}
|
|
37
|
-
}
|
|
37
|
+
}
|