@plusscommunities/pluss-core-aws 1.2.6 → 1.3.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,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,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,16 +24,23 @@ 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)) {
32
+ const categoryId =
33
+ data.muteConfig && data.muteConfig.type
34
+ ? `mute_${data.muteConfig.type === "app" ? "app" : "entity"}`
35
+ : null;
36
+
28
37
  messages.push({
29
38
  to: token,
30
39
  sound: "default",
31
40
  body: message,
32
41
  data,
33
42
  badge: 1,
43
+ categoryId,
34
44
  });
35
45
  } else {
36
46
  log(
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@plusscommunities/pluss-core-aws",
3
- "version": "1.2.6",
3
+ "version": "1.3.0",
4
4
  "description": "Core extension package for Pluss Communities platform",
5
5
  "scripts": {
6
6
  "patch": "npm version patch",