@plusscommunities/pluss-newsletter-aws 1.4.7 → 1.4.8

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/package-lock.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-newsletter-aws",
3
- "version": "1.4.7",
3
+ "version": "1.4.8",
4
4
  "lockfileVersion": 1,
5
5
  "requires": true,
6
6
  "dependencies": {
@@ -13,9 +13,9 @@
13
13
  }
14
14
  },
15
15
  "@plusscommunities/pluss-core-aws": {
16
- "version": "1.6.4",
17
- "resolved": "https://registry.npmjs.org/@plusscommunities/pluss-core-aws/-/pluss-core-aws-1.6.4.tgz",
18
- "integrity": "sha512-pWMKIq5yE53VuluV1U6Ft67kNcZ43z+MT2neU8Tds2HLVzaAHcsE5jl57T56nktIDUIWggXWqY3MkPsjNbtarA==",
16
+ "version": "1.6.10",
17
+ "resolved": "https://registry.npmjs.org/@plusscommunities/pluss-core-aws/-/pluss-core-aws-1.6.10.tgz",
18
+ "integrity": "sha512-O2n00I+HW1woK6n/U4nbzI4PZg0OoP16+QYrKgVy3NguQMxTaeijr8Yd8ML2SPMb2xF74u7iOkK6LIXIdVXH3A==",
19
19
  "requires": {
20
20
  "@aws/dynamodb-auto-marshaller": "^0.7.1",
21
21
  "amazon-cognito-identity-js": "^2.0.19",
@@ -1451,9 +1451,9 @@
1451
1451
  "integrity": "sha512-P+M65QY2JQ5Y0G9KKdlDpo0zK+/OHptU5AaBwUfAIDJZk1MYf32Frm84EcOytfJE0t5JvkAnKlmjsXDnWzCJmQ=="
1452
1452
  },
1453
1453
  "html-entities": {
1454
- "version": "2.3.3",
1455
- "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz",
1456
- "integrity": "sha512-DV5Ln36z34NNTDgnz0EWGBLZENelNAtkiFA4kyNOG2tDI6Mz1uSWiq1wAKdyjnJwyDiDO7Fa2SO1CTxPXL8VxA=="
1454
+ "version": "2.3.5",
1455
+ "resolved": "https://registry.npmjs.org/html-entities/-/html-entities-2.3.5.tgz",
1456
+ "integrity": "sha512-72TJlcMkYsEJASa/3HnX7VT59htM7iSHbH59NSZbtc+22Ap0Txnlx91sfeB+/A7wNZg7UxtZdhAW4y+/jimrdg=="
1457
1457
  },
1458
1458
  "htmlparser2": {
1459
1459
  "version": "3.10.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-newsletter-aws",
3
- "version": "1.4.7",
3
+ "version": "1.4.8",
4
4
  "description": "Extension package to enable newsletter on Pluss Communities Platform",
5
5
  "scripts": {
6
6
  "gc": "node ../../tools/gc ./",
@@ -23,7 +23,7 @@
23
23
  "license": "ISC",
24
24
  "dependencies": {
25
25
  "@aws/dynamodb-auto-marshaller": "^0.7.1",
26
- "@plusscommunities/pluss-core-aws": "1.6.4",
26
+ "@plusscommunities/pluss-core-aws": "1.6.10",
27
27
  "amazon-cognito-identity-js": "^2.0.19",
28
28
  "axios": "^0.18.0",
29
29
  "expo-server-sdk": "^3.0.1",
@@ -1,10 +1,18 @@
1
1
  const moment = require("moment");
2
+ const _ = require("lodash");
2
3
  const initConfig = require("./initConfig");
3
4
  const indexQuery = require("./common/db/common/indexQuery");
4
5
  const publishNotifications = require("./common/db/notifications/publishNotifications");
5
6
  const getAudience = require("./common/helper/audience/getAudience");
6
7
  const updateAttribute = require("./common/db/common/updateAttribute");
7
8
  const { values } = require("./values.config");
9
+ const scanRefRecursive = require("@plusscommunities/pluss-core-aws/db/common/scanRefRecursive");
10
+ const logAnalyticsActivity = require("@plusscommunities/pluss-core-aws/db/analytics/logAnalyticsActivity");
11
+ const checkActivityExists = require("@plusscommunities/pluss-core-aws/db/analytics/checkActivityExists");
12
+ const updateRef = require("@plusscommunities/pluss-core-aws/db/common/updateRef");
13
+ const editRef = require("@plusscommunities/pluss-core-aws/db/common/editRef");
14
+ const getRef = require("@plusscommunities/pluss-core-aws/db/common/getRef");
15
+ const { getRowId, log } = require("@plusscommunities/pluss-core-aws/helper");
8
16
 
9
17
  const setUnsentNotification = (update) => {
10
18
  update.UnsentNotification = undefined;
@@ -59,9 +67,125 @@ const sendNotifications = (update) => {
59
67
  });
60
68
  };
61
69
 
62
- module.exports.watchNewsletter = (event, context, callback) => {
70
+ const fixActivityLog = async (post, startTime) => {
71
+ const now = moment().unix();
72
+ if (now - startTime > 240) {
73
+ return false;
74
+ }
75
+
76
+ // check whether it's already been logged
77
+ const exists = await checkActivityExists(
78
+ post.Id,
79
+ values.entityKey,
80
+ post.Private ? "PrivatePost" : "Post"
81
+ );
82
+ const logId = log("fixActivityLog", "exists", exists);
83
+
84
+ if (!exists) {
85
+ // add old log
86
+ await logAnalyticsActivity(
87
+ post.Private ? "PrivatePost" : "Post",
88
+ values.entityKey,
89
+ post.Site,
90
+ post.SubmittedBy,
91
+ post.Id,
92
+ null,
93
+ moment(post.Timestamp).valueOf()
94
+ );
95
+ log("fixActivityLog", "Logged", true, logId);
96
+
97
+ // tell system to aggregate this date again
98
+ const date = moment(post.Timestamp).local().format("DD-MM-YYYY");
99
+ log("fixActivityLog", "QueueingDate", date, logId);
100
+ await updateRef("analyticsqueue", {
101
+ Id: getRowId(post.Site, date),
102
+ Site: post.Site,
103
+ Date: date,
104
+ });
105
+ }
106
+
107
+ // update entry to show it's been logged
108
+ await editRef(values.tableNameNewsletterEntries, "RowId", post.RowId, {
109
+ ActivityLogged: true,
110
+ });
111
+
112
+ return true;
113
+ };
114
+
115
+ const checkFixLog = async (startTime) => {
116
+ // Check whether all posts have been logged
117
+ const logId = log("checkFixLog", "StartTime", startTime);
118
+ const stringSite = "plussSpace";
119
+ const stringId = `${values.entityKey}ActivityLogged`;
120
+ const stringRowId = getRowId(stringSite, stringId);
121
+ try {
122
+ const string = await getRef("strings", "RowId", stringRowId);
123
+ if (string && string.Value === "true") {
124
+ log("checkFixLog", "ActivityLogged", true, logId);
125
+ return true;
126
+ }
127
+ } catch (e) {
128
+ // ignore error
129
+ }
130
+
131
+ // Scans the DynamoDB table and fetch all items. The parameters describe which attributes to get.
132
+ const items = await scanRefRecursive(values.tableNameNewsletterEntries, {
133
+ ProjectionExpression: "#id, RowId, Site, SubmittedBy, #ts, ActivityLogged",
134
+ ExpressionAttributeNames: {
135
+ "#id": "Id",
136
+ "#ts": "Timestamp",
137
+ },
138
+ });
139
+ log(
140
+ "checkFixLog",
141
+ "FetchedItemsCount",
142
+ {
143
+ count: items.length,
144
+ },
145
+ logId
146
+ );
147
+
148
+ // Filters out items which have already had the activity log operation applied.
149
+ const itemsToProcess = items.filter((item) => !item.ActivityLogged);
150
+ log(
151
+ "checkFixLog",
152
+ "ItemsToProcessCount",
153
+ { count: itemsToProcess.length },
154
+ logId
155
+ );
156
+
157
+ // Finished logging old entries. Save to avoid continuous full scans of the table.
158
+ if (_.isEmpty(itemsToProcess)) {
159
+ const entry = {
160
+ Site: stringSite,
161
+ StringId: stringId,
162
+ Value: "true",
163
+ RowId: stringRowId,
164
+ };
165
+
166
+ await updateRef("strings", entry);
167
+ }
168
+
169
+ // The loop goes through the remaining items and applies the fixActivityLog operation on each item.
170
+ // If the operation fails on an item, it logs the error but continues with the remaining items.
171
+ for (let item of itemsToProcess) {
172
+ try {
173
+ await fixActivityLog(item, startTime);
174
+ log("checkFixLog", "SuccessFixActivityLog", { id: item.Id }, logId);
175
+ } catch (err) {
176
+ log("checkFixLog", `Error:${item.Id}`, err, logId);
177
+ }
178
+ }
179
+
180
+ log("checkFixLog", "End", "All items processed", logId);
181
+ return true;
182
+ };
183
+
184
+ module.exports.watchNewsletter = async (event, context, callback) => {
63
185
  initConfig();
64
186
 
187
+ const startTime = moment().unix();
188
+
65
189
  const query = {
66
190
  IndexName: "NewsletterEntriesUnsentNotiIndex",
67
191
  KeyConditionExpression:
@@ -81,4 +205,6 @@ module.exports.watchNewsletter = (event, context, callback) => {
81
205
  sendNotifications(update);
82
206
  });
83
207
  });
208
+
209
+ await checkFixLog(startTime);
84
210
  };