@plusscommunities/pluss-core-aws 1.4.19-beta.0 → 1.4.21-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.
@@ -1,22 +1,29 @@
1
- const replaceErrors = (key, value) => {
2
- if (value instanceof Error) {
3
- var error = {};
4
- Object.getOwnPropertyNames(value).forEach(function (key) {
5
- error[key] = value[key];
6
- });
7
- return error;
8
- }
9
- return value;
1
+ const getCircularReplacer = () => {
2
+ const seen = new WeakSet();
3
+ return (key, value) => {
4
+ if (value instanceof Error) {
5
+ var error = {};
6
+ Object.getOwnPropertyNames(value).forEach((key) => {
7
+ error[key] = value[key];
8
+ });
9
+ return error;
10
+ } else if (typeof value === "object" && value !== null) {
11
+ if (seen.has(value)) return;
12
+ seen.add(value);
13
+ }
14
+ return value;
15
+ };
10
16
  };
11
17
 
12
18
  module.exports = (statusCode, body) => {
13
19
  if ([400, 402, 422].includes(statusCode)) console.error(body);
20
+ const jsonBody = JSON.stringify(body.error || body, getCircularReplacer());
14
21
  return {
15
22
  statusCode,
16
23
  headers: {
17
24
  "Content-Type": "application/json",
18
25
  "Access-Control-Allow-Origin": "*",
19
26
  },
20
- body: JSON.stringify(body, replaceErrors),
27
+ body: jsonBody,
21
28
  };
22
29
  };
@@ -13,7 +13,7 @@ module.exports = async (
13
13
  excludeClientBranding = false,
14
14
  brandingImage = null,
15
15
  bcc = null,
16
- }
16
+ } = {}
17
17
  ) => {
18
18
  const { communityConfig, serverlessConfig } = getConfig();
19
19
 
@@ -0,0 +1,32 @@
1
+ module.exports = (template, toReplace = null, user = null) => {
2
+ let replaced = template;
3
+ if (toReplace) {
4
+ Object.keys(toReplace).forEach((key) => {
5
+ replaced = replaced.replace(
6
+ new RegExp(`{{${key}}}`, "g"),
7
+ toReplace[key] || ""
8
+ );
9
+ });
10
+ }
11
+ if (user) {
12
+ const nameReplace =
13
+ (toReplace?.names && toReplace?.names[user.Id]) || user.displayName;
14
+ const phoneReplace =
15
+ (toReplace?.phoneNumbers && toReplace?.phoneNumbers[user.Id]) ||
16
+ user.phoneNumber;
17
+ // Remove surname
18
+ const firstname = nameReplace
19
+ ? nameReplace.split(" ").map((n) => n.trim())[0]
20
+ : null;
21
+ replaced = replaced
22
+ .replace(new RegExp("{{name}}", "g"), firstname || "{{name}}")
23
+ .replace(new RegExp("{{fullname}}", "g"), nameReplace || "{{fullname}}")
24
+ .replace(
25
+ new RegExp("{{phoneNumber}}", "g"),
26
+ phoneReplace || "{{phoneNumber}}"
27
+ )
28
+ .replace(new RegExp("{{email}}", "g"), user.email || "{{email}}");
29
+ }
30
+
31
+ return replaced;
32
+ };
@@ -43,41 +43,37 @@ const SendNotification = async (tokens, message, type, key, params, config) => {
43
43
  }
44
44
  messages.push(msg);
45
45
  } else {
46
- log(
47
- "SendNotification",
48
- "IsTokenError",
49
- `Push token ${token} is not a valid Expo push token`,
50
- logId
51
- );
52
- if (config.raiseError) throw error;
46
+ const message = `Push token ${token} is not a valid Expo push token`;
47
+ log("SendNotification", "IsTokenError", message, logId);
48
+ if (config.raiseError) throw new Error(message);
53
49
  }
54
50
  }
55
51
 
56
52
  log("SendNotification", "messages", messages);
57
53
  const chunks = expo.chunkPushNotifications(messages);
58
54
  for (const chunk of chunks) {
59
- expo
60
- .sendPushNotificationsAsync(chunk)
61
- .then((receipts) => {
62
- log("SendNotification", "Receipts", receipts, logId);
63
- })
64
- .catch((error) => {
65
- if (error.code && error.code === "PUSH_TOO_MANY_EXPERIENCE_IDS") {
66
- log(
67
- "SendNotification",
68
- "CaughtError",
69
- "PUSH_TOO_MANY_EXPERIENCE_IDS",
70
- logId
71
- );
72
- // send separately
73
- _.values(error.details).forEach((appTokens) => {
74
- SendNotification(appTokens, message, type, key, params, config);
75
- });
76
- } else {
77
- log("SendNotification", "SendError", error, logId);
78
- if (config.raiseError) throw error;
79
- }
80
- });
55
+ try {
56
+ const receipts = await expo.sendPushNotificationsAsync(chunk);
57
+ log("SendNotification", "Receipts", receipts, logId);
58
+ } catch (error) {
59
+ if (error.code && error.code === "PUSH_TOO_MANY_EXPERIENCE_IDS") {
60
+ log(
61
+ "SendNotification",
62
+ "CaughtError",
63
+ "PUSH_TOO_MANY_EXPERIENCE_IDS",
64
+ logId
65
+ );
66
+ // send separately
67
+ await Promise.all(
68
+ _.values(error.details).map((appTokens) =>
69
+ SendNotification(appTokens, message, type, key, params, config)
70
+ )
71
+ );
72
+ } else {
73
+ log("SendNotification", "SendError", error, logId);
74
+ if (config.raiseError) throw error;
75
+ }
76
+ }
81
77
  }
82
78
  };
83
79
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-core-aws",
3
- "version": "1.4.19-beta.0",
3
+ "version": "1.4.21-beta.0",
4
4
  "description": "Core extension package for Pluss Communities platform",
5
5
  "scripts": {
6
6
  "betapatch": "npm version prepatch --preid=beta",
7
7
  "patch": "npm version patch",
8
- "betaupload": "npm publish --access public --tag beta",
8
+ "betaupload": "npm i && npm i && npm publish --access public --tag beta",
9
9
  "betaupload:p": "npm run betapatch && npm run betaupload",
10
- "upload": "npm publish --access public",
10
+ "upload": "npm i && npm i && npm publish --access public",
11
11
  "upload:p": "npm run patch && npm run upload"
12
12
  },
13
13
  "author": "Phillip Suh",