@go-mailer/jarvis 5.0.3 → 5.0.5

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/index.js CHANGED
@@ -1,20 +1,22 @@
1
- const EnvVar = require('./lib/env')
2
- const FeatureFlag = require('./lib/flag')
3
- const Redis = require('./lib/redis/index')
4
- const QueryBuilder = require('./lib/query')
5
- const HTTP = require('./lib/middlewares/http')
6
- const Authenticator = require('./lib/middlewares/auth')
7
- const AutomationConstants = require('./lib/constants/automation')
8
- const { RequestLogger, ProcessLogger } = require('./lib/middlewares/logger')
1
+ const EnvVar = require("./lib/env");
2
+ const FeatureFlag = require("./lib/flag");
3
+ const Redis = require("./lib/redis/index");
4
+ const QueryBuilder = require("./lib/query");
5
+ const HTTP = require("./lib/middlewares/http");
6
+ const Authenticator = require("./lib/middlewares/auth");
7
+ const AutomationConstants = require("./lib/constants/automation");
8
+ const { RequestLogger, ProcessLogger } = require("./lib/middlewares/logger");
9
+ const Utility = require("./lib/utilitiy/index");
9
10
 
10
- module.exports = {
11
+ module.exports = {
11
12
  Authenticator,
12
13
  AutomationConstants,
13
14
  EnvVar,
14
15
  FeatureFlag,
15
16
  HTTP,
16
17
  ProcessLogger,
17
- RequestLogger,
18
18
  QueryBuilder,
19
- Redis
20
- }
19
+ Redis,
20
+ RequestLogger,
21
+ Utility,
22
+ };
package/lib/query.js CHANGED
@@ -178,8 +178,9 @@ const generatePipeline = ({ group_by, seek_conditions, sort_condition, sum, fiel
178
178
  $match: { ...seek_conditions }
179
179
  })
180
180
 
181
- if (sort_condition) {
182
- const sort_options = sort_condition.split(' ').reduce((sac, condition) => {
181
+ let sort_options = sort_condition
182
+ if (sort_condition && typeof(sort_condition) === 'string') {
183
+ sort_options = sort_condition.split(' ').reduce((sac, condition) => {
183
184
  let key = condition
184
185
  let value = 1
185
186
  if (key.includes('-')) {
@@ -189,7 +190,9 @@ const generatePipeline = ({ group_by, seek_conditions, sort_condition, sum, fiel
189
190
 
190
191
  return { ...sac, [key]: value }
191
192
  }, {})
193
+ }
192
194
 
195
+ if (sort_options && Object.keys(sort_options).length) {
193
196
  pipeline.push({
194
197
  $sort: { ...sort_options }
195
198
  })
@@ -0,0 +1,69 @@
1
+ const processRangeInDays = (min = 0, seconds_in_day = 86400000) => {
2
+ const ranges = [];
3
+ let x = min;
4
+ for (let i = 0; i < 31; i++) {
5
+ const [, mmm, dd] = new Date(x).toDateString().split(" ");
6
+ ranges.push({ start: x, end: x + seconds_in_day - 1000, label: `${mmm} ${dd}` });
7
+ x += seconds_in_day;
8
+ }
9
+
10
+ return ranges;
11
+ };
12
+
13
+ const processRangeInWeeks = (min = 0, seconds_in_day = 86400000) => {
14
+ const ranges = [];
15
+ let x = min;
16
+ const [, mmm, dd] = new Date(x).toDateString().split(" ");
17
+ for (let i = 0; i < Math.ceil(100 / 7); i++) {
18
+ ranges[i] = {
19
+ start: x,
20
+ end: x + 6 * seconds_in_day - 1000,
21
+ label: `${mmm} ${dd}`,
22
+ };
23
+ x += 6 * seconds_in_day;
24
+ }
25
+
26
+ return ranges;
27
+ };
28
+
29
+ const processRangeInMonths = (min, max) => {
30
+ // group in months
31
+ const d = new Date(min);
32
+ const year = d.getFullYear();
33
+ const month = d.getUTCMonth();
34
+
35
+ const ranges = [];
36
+ let x = min;
37
+ for (let i = 1; x <= max; i++) {
38
+ const [, mmm, , yyyy] = new Date(x).toDateString().split(" ");
39
+ ranges.push({
40
+ start: x,
41
+ end: Date.parse(new Date(year, month + i, 1)) - 1000,
42
+ label: `${mmm} ${yyyy}`,
43
+ });
44
+
45
+ x = Date.parse(new Date(year, month + i, 1));
46
+ }
47
+
48
+ return ranges;
49
+ };
50
+
51
+ module.exports = {
52
+ processDateRange: (start, stop) => {
53
+ const min = Date.parse(start);
54
+ const max = Date.parse(stop);
55
+ const difference = max - min;
56
+ const seconds_in_day = 86400000;
57
+
58
+ let ranges = [];
59
+ if (difference / seconds_in_day <= 31) {
60
+ ranges = processRangeInDays(min, seconds_in_day);
61
+ } else if (difference / seconds_in_day <= 100) {
62
+ ranges = processRangeInWeeks(min, seconds_in_day);
63
+ } else {
64
+ ranges = processRangeInMonths(min, max);
65
+ }
66
+
67
+ return ranges;
68
+ },
69
+ };
@@ -0,0 +1,204 @@
1
+ /** */
2
+ const MONTH_MAP = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
3
+ const NUM_OF_MILLISECONDS_IN_ONE_DAY = 86400000;
4
+
5
+ /** */
6
+ const convertDateFromIsoToHTMLFormat = (iso_date) => {
7
+ const date = new Date(iso_date);
8
+ const day = padDateValue(date.getDate());
9
+ const month = padDateValue(date.getMonth() + 1);
10
+ const year = date.getFullYear();
11
+
12
+ const converted_date = `${year}-${month}-${day}`;
13
+ return iso_date ? converted_date : "";
14
+ };
15
+
16
+ /**
17
+ * Extracts day string of the given timestamp for graphs. Return ex. 'Jan 10'
18
+ * @param {Number} timestamp
19
+ */
20
+ const extractDayStringForGraph = (timestamp) => {
21
+ const date = new Date(timestamp);
22
+ const month = MONTH_MAP[date.getMonth()];
23
+ const day = padDateValue(date.getDate());
24
+
25
+ return `${month} ${day}`;
26
+ };
27
+
28
+ const generateHTMLFormDateTimeDefaults = () => {
29
+ const date = new Date().toISOString();
30
+ const generated_date = `${convertDateFromIsoToHTMLFormat(date)}`;
31
+ return generated_date;
32
+ };
33
+
34
+ /**
35
+ * Generates a configuration of back-counted days starting from a base time.
36
+ *
37
+ * @param {*} base_timestamp The day to start back count
38
+ * @param {*} spread The numbers of days to count back.
39
+ */
40
+ const generateDaysConfigurationForGraph = (base_timestamp, spread = 7, key_values) => {
41
+ let curr_timestamp = getDayTimestampForRawTimestamp(base_timestamp);
42
+ const config = {};
43
+ for (let i = 0; i < spread; i++) {
44
+ const date_string = extractDayStringForGraph(curr_timestamp);
45
+ // key_values = [subscribers , unsubscribers]; [opens, bounces]
46
+ config[date_string] = {
47
+ date: date_string,
48
+ [key_values[0]]: 0,
49
+ [key_values[1]]: 0,
50
+ };
51
+ curr_timestamp -= NUM_OF_MILLISECONDS_IN_ONE_DAY;
52
+ }
53
+ return config;
54
+ };
55
+
56
+ /**
57
+ * Generates the number of milliseconds at (12:00AM) for the date specified in the raw_timestamp.
58
+ *
59
+ * @param {Number} raw_timestamp number of milliseconds
60
+ */
61
+ const getDayTimestampForRawTimestamp = (raw_timestamp) => {
62
+ const date = new Date(raw_timestamp);
63
+ const day = `${padDateValue(date.getMonth() + 1)}/${padDateValue(date.getDate())}/${date.getFullYear()}`;
64
+ return Date.parse(day);
65
+ };
66
+
67
+ /**
68
+ * Get the timestamp of the first and last days of the month
69
+ */
70
+
71
+ const getMonthTimeRange = () => {
72
+ const year = new Date().getFullYear();
73
+ const current_month = new Date().getMonth();
74
+ const start = Date.parse(new Date(year, current_month, 1));
75
+ const end = Date.parse(new Date(year, current_month + 1, 1)) - 1000;
76
+
77
+ return { start, end };
78
+ };
79
+
80
+ /**
81
+ * Get today's midnight time stamp
82
+ */
83
+
84
+ const getCurrentDayTimestamp = () => {
85
+ const date = new Date();
86
+ const year = date.getFullYear();
87
+ const month = padDateValue(date.getMonth() + 1);
88
+ const day = padDateValue(date.getDate());
89
+ const date_string = `${year}-${month}-${day}`;
90
+ return Date.parse(date_string);
91
+ };
92
+
93
+ /**
94
+ * prefixes 0 on numbers with single digits.
95
+ * @param {Number} value
96
+ */
97
+ const padDateValue = (value) => {
98
+ return value < 10 ? `0${value}` : value;
99
+ };
100
+
101
+ const isCampaignSendDateValid = (chosen_date) => {
102
+ const today = new Date().toDateString();
103
+ return new Date(`${chosen_date}`) >= new Date(today);
104
+ };
105
+
106
+ const toDateString = (date = "") => {
107
+ if (!date) return "";
108
+ return new Date(date).toDateString();
109
+ };
110
+
111
+ const formatDateWithoutDayOfWeek = (date = "") => {
112
+ if (!date) return "";
113
+ const options = { year: "numeric", month: "short", day: "numeric" };
114
+ return new Date(date).toLocaleDateString(undefined, options);
115
+ };
116
+
117
+ const formatDateForDisplay = (raw_value) => {
118
+ const date = new Date(raw_value);
119
+ if (!raw_value || !date) return raw_value;
120
+ const [, mon, day, year, time] = date.toString().split(" ");
121
+ return [mon, `${day},`, year, time].join(" ");
122
+ };
123
+
124
+ const toTimeString = (date = "") => {
125
+ if (!date) return "";
126
+ const options = {
127
+ hour: "numeric",
128
+ minute: "numeric",
129
+ hour12: true,
130
+ };
131
+ return new Date(date).toLocaleTimeString("en-US", options);
132
+ };
133
+
134
+ const generateDefaultRange = () => {
135
+ const [today] = new Date().toISOString().split("T");
136
+ const [thirty_days_ago] = new Date(Date.parse(today) - 30 * 86400000).toISOString().split("T");
137
+ return [thirty_days_ago, today];
138
+ };
139
+
140
+ /**
141
+ *
142
+ * @param {Number} year
143
+ * @returns {startDate, endDate}
144
+ */
145
+ const getYearRange = (year) => {
146
+ const startDate = new Date(year, 0, 1).getTime(); // January 1st
147
+ const endDate = new Date(year, 11, 31).getTime() + 86400000 - 1; // December 31st (23:59:59)
148
+ return { startDate, endDate };
149
+ };
150
+
151
+ const getMonthTimestamps = (year) => {
152
+ const timestamps = [];
153
+
154
+ for (let month = 0; month < 12; month++) {
155
+ const startDate = new Date(year, month, 1);
156
+ const endDate = new Date(year, month + 1, 0, 23, 59, 59, 999); // Last day of the month
157
+
158
+ timestamps.push({
159
+ start: startDate.getTime(),
160
+ end: endDate.getTime(),
161
+ });
162
+ }
163
+
164
+ return timestamps;
165
+ };
166
+
167
+ const getMonthNameFromTimestamp = (timestamp) => {
168
+ const date = new Date(timestamp);
169
+ const months = [
170
+ "January",
171
+ "February",
172
+ "March",
173
+ "April",
174
+ "May",
175
+ "June",
176
+ "July",
177
+ "August",
178
+ "September",
179
+ "October",
180
+ "November",
181
+ "December",
182
+ ];
183
+ return months[date.getMonth()];
184
+ };
185
+
186
+ module.exports = {
187
+ convertDateFromIsoToHTMLFormat,
188
+ extractDayStringForGraph,
189
+ formatDateForDisplay,
190
+ formatDateWithoutDayOfWeek,
191
+ generateDaysConfigurationForGraph,
192
+ generateHTMLFormDateTimeDefaults,
193
+ getCurrentDayTimestamp,
194
+ getDayTimestampForRawTimestamp,
195
+ generateDefaultRange,
196
+ getMonthTimeRange,
197
+ isCampaignSendDateValid,
198
+ padDateValue,
199
+ toDateString,
200
+ toTimeString,
201
+ getYearRange,
202
+ getMonthTimestamps,
203
+ getMonthNameFromTimestamp,
204
+ };
@@ -0,0 +1,9 @@
1
+ const Chart = require("./chart");
2
+ const Date = require("./date");
3
+ const Number = require("./number");
4
+
5
+ module.exports = {
6
+ Chart,
7
+ Date,
8
+ Number,
9
+ };
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ toReadableNumber: (value) => {
3
+ if (!value) return 0;
4
+ if (isNaN(value)) return value;
5
+ return Number(Number(value).toFixed(2)).toLocaleString();
6
+ },
7
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "5.0.3",
3
+ "version": "5.0.5",
4
4
  "main": "index.js",
5
5
  "repository": "git@github.com:go-mailer-ltd/jarvis-node.git",
6
6
  "author": "Nathan Oguntuberu <nateoguns.work@gmail.com>",