@plusscommunities/pluss-core-aws 1.2.6 → 1.3.3-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.
@@ -0,0 +1,36 @@
1
+ const AWS = require("aws-sdk");
2
+ const { getConfig } = require("../config");
3
+
4
+ module.exports = (from, to, subject, content, access = null) => {
5
+ return new Promise((resolve, reject) => {
6
+ const { serverlessConfig } = getConfig();
7
+ AWS.config.update({
8
+ accessKeyId: access ? access.key : serverlessConfig.key,
9
+ secretAccessKey: access ? access.secret : serverlessConfig.secret,
10
+ });
11
+
12
+ const sesv2 = new AWS.SESV2();
13
+ const params = {
14
+ Content: {
15
+ Simple: {
16
+ Body: {
17
+ Html: {
18
+ Data: content,
19
+ },
20
+ },
21
+ Subject: {
22
+ Data: subject,
23
+ },
24
+ },
25
+ },
26
+ Destination: {
27
+ ToAddresses: to.split(",").map((i) => i.trim()),
28
+ },
29
+ FromEmailAddress: from,
30
+ };
31
+ sesv2.sendEmail(params, (err, data) => {
32
+ if (err) return reject(err);
33
+ return resolve(data);
34
+ });
35
+ });
36
+ };
@@ -0,0 +1,18 @@
1
+ const indexQuery = require("../common/indexQuery");
2
+
3
+ module.exports = async (userId, type = null, id = null) => {
4
+ const query = {
5
+ IndexName: "UserIndex",
6
+ KeyConditionExpression: "UserId = :userId",
7
+ ExpressionAttributeValues: {
8
+ ":userId": userId,
9
+ },
10
+ };
11
+ const { Items } = await indexQuery("notificationsettings", query);
12
+ if (type) {
13
+ return Items.find(
14
+ (i) => i.EntityType === type && (!id || i.EntityId === id)
15
+ );
16
+ }
17
+ return Items;
18
+ };
@@ -21,7 +21,15 @@ const saveNotification = (receiver, notification) => {
21
21
  });
22
22
  };
23
23
 
24
- module.exports = (receivers, type, site, id, data, sendPush) => {
24
+ module.exports = (
25
+ receivers,
26
+ type,
27
+ site,
28
+ id,
29
+ data,
30
+ sendPush,
31
+ config = { type: "app", id: null, ignoreMute: false }
32
+ ) => {
25
33
  const notification = {};
26
34
  notification.Timestamp = moment.utc().valueOf();
27
35
  notification.Type = type;
@@ -34,6 +42,6 @@ module.exports = (receivers, type, site, id, data, sendPush) => {
34
42
  });
35
43
  if (sendPush) {
36
44
  const notiData = prepNotification(notification);
37
- sendNotifications(receivers, notiData.Text, type, id, notiData);
45
+ sendNotifications(receivers, notiData.Text, type, id, notiData, config);
38
46
  }
39
47
  };
@@ -1,4 +1,5 @@
1
1
  const nodemailer = require("nodemailer");
2
+ const sendEmail = require("../aws/sendEmail");
2
3
  const { getConfig } = require("../config");
3
4
 
4
5
  module.exports = function (
@@ -12,18 +13,8 @@ module.exports = function (
12
13
  ) {
13
14
  return new Promise((resolve, reject) => {
14
15
  console.log("sending email");
16
+ const { communityConfig, serverlessConfig, emailConfig } = getConfig();
15
17
 
16
- const transporter = nodemailer.createTransport({
17
- //host: 'smtp.ethereal.email',
18
- service: "gmail",
19
- //port: 587,
20
- auth: {
21
- user: "info@joinpluss.com",
22
- pass: "smartcommunities",
23
- },
24
- });
25
-
26
- const { communityConfig } = getConfig();
27
18
  if (useTemplate) {
28
19
  content = `<div style="padding: 24px; padding-top: 0px; background-color: #f2f4f8;margin: 0px;">
29
20
  <div style="background-color: #fff; padding: 48px; padding-top: 65px; padding-bottom: 24px;">
@@ -52,25 +43,57 @@ module.exports = function (
52
43
  </div>`;
53
44
  }
54
45
 
46
+ const defaultFrom = emailConfig
47
+ ? emailConfig.contactEmail
48
+ : `"${communityConfig.name}" noreply@${communityConfig.subdomain}.plusscommunities.com'`;
55
49
  const mailOptions = {
56
- from:
57
- fromEmail != null
58
- ? fromEmail
59
- : `"${communityConfig.name}" noreply@${communityConfig.subdomain}.plusscommunities.com'`,
50
+ from: fromEmail != null ? fromEmail : defaultFrom,
60
51
  to: toEmail,
61
52
  subject,
62
53
  text: content,
63
54
  html: content,
64
55
  };
65
56
 
66
- transporter.sendMail(mailOptions, (error) => {
67
- if (error) {
68
- console.log("Email failed", toEmail, subject, error);
69
- reject(error);
70
- } else {
71
- console.log("Email success", toEmail, subject);
72
- resolve();
73
- }
74
- });
57
+ if (serverlessConfig && serverlessConfig.key && serverlessConfig.secret) {
58
+ console.log("serverlessConfig exists, using Amazon SES", mailOptions);
59
+ sendEmail(
60
+ mailOptions.from,
61
+ mailOptions.to,
62
+ mailOptions.subject,
63
+ mailOptions.html,
64
+ serverlessConfig
65
+ )
66
+ .then((result) => {
67
+ console.log("Email success", toEmail, subject, result);
68
+ resolve();
69
+ })
70
+ .catch((error) => {
71
+ console.log("Email failed", toEmail, subject, error);
72
+ reject(error);
73
+ });
74
+ } else {
75
+ console.log(
76
+ "serverlessConfig doesn't exist, fall back to nodemailer",
77
+ mailOptions
78
+ );
79
+ const transporter = nodemailer.createTransport({
80
+ //host: 'smtp.ethereal.email',
81
+ service: "gmail",
82
+ //port: 587,
83
+ auth: {
84
+ user: "info@joinpluss.com",
85
+ pass: "smartcommunities",
86
+ },
87
+ });
88
+ transporter.sendMail(mailOptions, (error) => {
89
+ if (error) {
90
+ console.log("Email failed", toEmail, subject, error);
91
+ reject(error);
92
+ } else {
93
+ console.log("Email success", toEmail, subject);
94
+ resolve();
95
+ }
96
+ });
97
+ }
75
98
  });
76
99
  };
@@ -1,15 +1,18 @@
1
1
  const { Expo } = require("expo-server-sdk");
2
2
  const _ = require("lodash");
3
+ const moment = require("moment");
3
4
  const getUser = require("../db/users/getUser");
5
+ const getNotificationSetting = require("../db/notifications/getNotificationSetting");
4
6
  const { log } = require("../helper");
5
7
 
6
- const SendNotification = (tokens, message, type, key, params) => {
8
+ const SendNotification = (tokens, message, type, key, params, config) => {
7
9
  const logId = log("SendNotification", "input", {
8
10
  tokens,
9
11
  message,
10
12
  type,
11
13
  key,
12
14
  params,
15
+ config,
13
16
  });
14
17
 
15
18
  const expo = new Expo();
@@ -21,17 +24,24 @@ const SendNotification = (tokens, message, type, key, params) => {
21
24
  Object.keys(params).forEach((paramKey) => {
22
25
  data[paramKey] = params[paramKey];
23
26
  });
27
+ data.muteConfig = config;
24
28
  log("SendNotification", "params", data, logId);
25
29
 
26
30
  for (const token of tokens) {
27
31
  if (Expo.isExpoPushToken(token)) {
28
- messages.push({
32
+ const msg = {
29
33
  to: token,
30
34
  sound: "default",
31
35
  body: message,
32
36
  data,
33
37
  badge: 1,
34
- });
38
+ };
39
+ if (data.muteConfig && !data.muteConfig.ignoreMute) {
40
+ msg.categoryId = data.muteConfig.type
41
+ ? `mute_${data.muteConfig.type === "app" ? "app" : "entity"}`
42
+ : "mute_app";
43
+ }
44
+ messages.push(msg);
35
45
  } else {
36
46
  log(
37
47
  "SendNotification",
@@ -42,6 +52,7 @@ const SendNotification = (tokens, message, type, key, params) => {
42
52
  }
43
53
  }
44
54
 
55
+ log("SendNotification", "messages", messages);
45
56
  const chunks = expo.chunkPushNotifications(messages);
46
57
  for (const chunk of chunks) {
47
58
  expo
@@ -59,7 +70,7 @@ const SendNotification = (tokens, message, type, key, params) => {
59
70
  );
60
71
  // send separately
61
72
  _.values(error.details).forEach((appTokens) => {
62
- SendNotification(appTokens, message, type, key, params);
73
+ SendNotification(appTokens, message, type, key, params, config);
63
74
  });
64
75
  } else {
65
76
  log("SendNotification", "SendError", error, logId);
@@ -68,22 +79,69 @@ const SendNotification = (tokens, message, type, key, params) => {
68
79
  }
69
80
  };
70
81
 
71
- module.exports = (receiverKeys, message, type, key, value) => {
82
+ const checkMuted = async (userId, config) => {
83
+ if (!config.ignoreMute) {
84
+ const notiSettings = await getNotificationSetting(userId);
85
+ const logId = log("checkMuted", "notiSettings", notiSettings);
86
+
87
+ const appSetting = notiSettings.find((n) => n.EntityType === "app");
88
+ const isAppMuted = appSetting
89
+ ? moment() <= moment(appSetting.Expiry)
90
+ : false;
91
+ log("checkMuted", "isAppMuted", isAppMuted, logId);
92
+ if (isAppMuted) return true;
93
+
94
+ const entitySetting = notiSettings.find(
95
+ (n) => n.EntityType === config.type && n.EntityId === config.id
96
+ );
97
+ const isEntityMuted = entitySetting
98
+ ? moment() <= moment(entitySetting.Expiry)
99
+ : false;
100
+ log("checkMuted", "isEntityMuted", isEntityMuted, logId);
101
+ if (isEntityMuted) return true;
102
+ }
103
+
104
+ return false;
105
+ };
106
+
107
+ module.exports = (
108
+ receiverKeys,
109
+ message,
110
+ type,
111
+ key,
112
+ value,
113
+ config = { type: "app", id: null, ignoreMute: false }
114
+ ) => {
115
+ const logId = log("sendNotifications", "input", {
116
+ receiverKeys,
117
+ message,
118
+ type,
119
+ key,
120
+ value,
121
+ config,
122
+ });
72
123
  const tokens = [];
73
124
  const promises = [];
74
125
  receiverKeys.forEach((uid) => {
75
126
  promises.push(
76
127
  new Promise((resolve, reject) => {
77
128
  getUser(uid)
78
- .then((user) => {
79
- if (!user.tokens) {
80
- if (user.token) {
81
- tokens.push(user.token);
129
+ .then(async (user) => {
130
+ try {
131
+ const isMuted = await checkMuted(user.Id, config);
132
+ if (!isMuted) {
133
+ if (!user.tokens) {
134
+ if (user.token) {
135
+ tokens.push(user.token);
136
+ }
137
+ } else {
138
+ _.values(user.tokens).forEach((entry) => {
139
+ tokens.push(entry.Token);
140
+ });
141
+ }
82
142
  }
83
- } else {
84
- _.values(user.tokens).forEach((entry) => {
85
- tokens.push(entry.Token);
86
- });
143
+ } catch (error) {
144
+ log("sendNotifications", "error", error.toString());
87
145
  }
88
146
  resolve();
89
147
  })
@@ -94,6 +152,6 @@ module.exports = (receiverKeys, message, type, key, value) => {
94
152
  );
95
153
  });
96
154
  Promise.all(promises).then((res) => {
97
- SendNotification(tokens, message, type, key, value);
155
+ SendNotification(tokens, message, type, key, value, config);
98
156
  });
99
157
  };
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-core-aws",
3
- "version": "1.2.6",
3
+ "version": "1.3.3-beta.0",
4
4
  "description": "Core extension package for Pluss Communities platform",
5
5
  "scripts": {
6
+ "betapatch": "npm version prepatch --preid=beta",
6
7
  "patch": "npm version patch",
8
+ "betaupload": "npm publish --access public --tag beta",
9
+ "betaupload:p": "npm run betapatch && npm run betaupload",
7
10
  "upload": "npm publish --access public",
8
11
  "upload:p": "npm run patch && npm run upload"
9
12
  },