@etainabl/nodejs-sdk 1.2.11 → 1.2.13
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/__tests__/reporting.test.ts +88 -0
- package/dist/cjs/reporting.js +5 -2
- package/dist/mjs/reporting.js +5 -2
- package/package.json +6 -4
- package/src/reporting.ts +6 -2
- package/tsconfig.base.json +1 -1
- package/tsconfig.test.json +13 -0
- package/vitest.config.js +10 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
import { reporting } from '../src/index.js';
|
|
3
|
+
|
|
4
|
+
const exampleScheduledReport = {
|
|
5
|
+
"_id" : "66964ddcdc2a32666b573d4b",
|
|
6
|
+
"name" : "Last Months Meter Report - 008453651410226013008",
|
|
7
|
+
"startDate" : moment('2024-07-18T08:03:00.000+0000'),
|
|
8
|
+
"frequency" : "1|month",
|
|
9
|
+
"frequencyDay" : "any",
|
|
10
|
+
"frequencyPeriod" : "any",
|
|
11
|
+
"reportPeriod" : "1|month",
|
|
12
|
+
"subject" : "{{ siteName }} Last Month Meter Report for {{ accountMeterPointNumber }}",
|
|
13
|
+
"message" : "Hi\n\nPlease find attached last month's meter management report for {{ accountName }} at {{ siteName }} covering the periods {{ reportStartDate }} to {{ reportEndDate }} for {{ accountMeterSerialNumber }}. \n\nIf you have any questions, please contact helpdesk@carbonxgen.com.\n\nMany thanks\nCarbonxgen",
|
|
14
|
+
"recipients" : [
|
|
15
|
+
"test@etainabl.com"
|
|
16
|
+
],
|
|
17
|
+
"enabled" : true,
|
|
18
|
+
"reportTemplateId" : "668f0cfd341c6b91bee5add3",
|
|
19
|
+
"overrides" : {
|
|
20
|
+
"accountId" : "655fcf07b7d3920014045314"
|
|
21
|
+
},
|
|
22
|
+
"userSub" : "auth0|6560834e165d6e67c4a4b8c9",
|
|
23
|
+
"companyId" : "655b6d833f36810014b817a1",
|
|
24
|
+
"deleted" : false,
|
|
25
|
+
"createdAt" : moment('2024-07-16T10:39:24.917+0000'),
|
|
26
|
+
"updatedAt" : moment('2024-08-14T10:37:16.263+0000'),
|
|
27
|
+
"bulkUpdateCode" : "rerun17092024"
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
describe('Scheduled report run times', () => {
|
|
31
|
+
|
|
32
|
+
it('should generate the correct run times for monthly report', async () => {
|
|
33
|
+
const nextRunTimes = reporting.getScheduledReportRunTimes(exampleScheduledReport, 5, moment('2024-06-23T00:00:00.000+0000'));
|
|
34
|
+
|
|
35
|
+
expect(nextRunTimes.map(t => t.toDate())).toEqual([
|
|
36
|
+
new Date('2024-07-18T08:03:00.000+0000'),
|
|
37
|
+
new Date('2024-08-18T08:03:00.000+0000'),
|
|
38
|
+
new Date('2024-09-18T08:03:00.000+0000'),
|
|
39
|
+
new Date('2024-10-18T08:03:00.000+0000'),
|
|
40
|
+
new Date('2024-11-18T08:03:00.000+0000')
|
|
41
|
+
]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should generate the correct run times for weekly report', async () => {
|
|
45
|
+
const simulatedTaskTime = moment('2024-07-23T00:00:00.000+0000');
|
|
46
|
+
|
|
47
|
+
const editedScheduledReport = {
|
|
48
|
+
...exampleScheduledReport,
|
|
49
|
+
frequency: "1|week"
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const scheduledStartDate = moment(editedScheduledReport.startDate);
|
|
53
|
+
|
|
54
|
+
const nextRunTimes = reporting.getScheduledReportRunTimes(editedScheduledReport, 5, simulatedTaskTime);
|
|
55
|
+
|
|
56
|
+
expect(nextRunTimes.map(t => t.toDate())).toEqual([
|
|
57
|
+
scheduledStartDate.add(1, 'week').toDate(),
|
|
58
|
+
scheduledStartDate.add(1, 'week').toDate(),
|
|
59
|
+
scheduledStartDate.add(1, 'week').toDate(),
|
|
60
|
+
scheduledStartDate.add(1, 'week').toDate(),
|
|
61
|
+
scheduledStartDate.add(1, 'week').toDate()
|
|
62
|
+
]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should generate a single run time for a one-time report', async () => {
|
|
66
|
+
|
|
67
|
+
const expiredScheduledReport = {
|
|
68
|
+
...exampleScheduledReport,
|
|
69
|
+
frequency: "1|once",
|
|
70
|
+
startDate: moment('2024-07-25T08:03:00.000+0000')
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const nextRunTimes = reporting.getScheduledReportRunTimes(expiredScheduledReport, 5, moment('2024-06-23T00:00:00.000+0000'));
|
|
74
|
+
expect(nextRunTimes.map(t => t.toDate())).toEqual([new Date("2024-07-25T08:03:00.000Z")]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should generate no run times for an expired one-time report', async () => {
|
|
78
|
+
|
|
79
|
+
const expiredScheduledReport = {
|
|
80
|
+
...exampleScheduledReport,
|
|
81
|
+
frequency: "1|once",
|
|
82
|
+
startDate: moment('2024-06-18T08:03:00.000+0000')
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const nextRunTimes = reporting.getScheduledReportRunTimes(expiredScheduledReport, 5, moment('2024-06-23T00:00:00.000+0000'));
|
|
86
|
+
expect(nextRunTimes).toEqual([]);
|
|
87
|
+
});
|
|
88
|
+
});
|
package/dist/cjs/reporting.js
CHANGED
|
@@ -53,8 +53,11 @@ const getScheduledReportRunTimes = (schedule, limit = 1, taskTime = (0, moment_1
|
|
|
53
53
|
if (includeStartDate) {
|
|
54
54
|
runTimes = [originalStartDate];
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
const [, freq] = schedule.frequency.split('|');
|
|
57
|
+
if (freq === 'once') {
|
|
58
|
+
const nextRunTime = runTimes[0];
|
|
59
|
+
// If this is now beyond the start date, return an empty array
|
|
60
|
+
return taskTime.isAfter(nextRunTime, 'minute') ? [] : runTimes;
|
|
58
61
|
}
|
|
59
62
|
const scheduleRunTimes = Array.from(Array(includeStartDate ? limit - 1 : limit).keys()).map(() => {
|
|
60
63
|
const nextRunTime = getNextRunTime(startDate, schedule, taskTime);
|
package/dist/mjs/reporting.js
CHANGED
|
@@ -47,8 +47,11 @@ export const getScheduledReportRunTimes = (schedule, limit = 1, taskTime = momen
|
|
|
47
47
|
if (includeStartDate) {
|
|
48
48
|
runTimes = [originalStartDate];
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const [, freq] = schedule.frequency.split('|');
|
|
51
|
+
if (freq === 'once') {
|
|
52
|
+
const nextRunTime = runTimes[0];
|
|
53
|
+
// If this is now beyond the start date, return an empty array
|
|
54
|
+
return taskTime.isAfter(nextRunTime, 'minute') ? [] : runTimes;
|
|
52
55
|
}
|
|
53
56
|
const scheduleRunTimes = Array.from(Array(includeStartDate ? limit - 1 : limit).keys()).map(() => {
|
|
54
57
|
const nextRunTime = getNextRunTime(startDate, schedule, taskTime);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etainabl/nodejs-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.13",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/mjs/index.js",
|
|
6
6
|
"author": "Jonathan Lambert <jonathan@etainabl.com>",
|
|
@@ -12,11 +12,13 @@
|
|
|
12
12
|
"winston": "^3.10.0"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
|
-
"@types/node": "^20.4.5"
|
|
15
|
+
"@types/node": "^20.4.5",
|
|
16
|
+
"vitest": "^2.1.1"
|
|
16
17
|
},
|
|
17
18
|
"scripts": {
|
|
18
|
-
"process": "yarn build && yarn publish",
|
|
19
|
-
"build": "rm -rf dist/* && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && ./fixup.sh"
|
|
19
|
+
"process": "vitest run && yarn build && yarn publish",
|
|
20
|
+
"build": "rm -rf dist/* && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && ./fixup.sh",
|
|
21
|
+
"test": "vitest"
|
|
20
22
|
},
|
|
21
23
|
"exports": {
|
|
22
24
|
".": {
|
package/src/reporting.ts
CHANGED
|
@@ -55,10 +55,14 @@ export const getScheduledReportRunTimes = (schedule: any, limit = 1, taskTime =
|
|
|
55
55
|
if (includeStartDate) {
|
|
56
56
|
runTimes = [originalStartDate];
|
|
57
57
|
}
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
const [, freq] = schedule.frequency.split('|');
|
|
60
|
+
|
|
60
61
|
if (freq === 'once') {
|
|
61
|
-
|
|
62
|
+
const nextRunTime = runTimes[0];
|
|
63
|
+
|
|
64
|
+
// If this is now beyond the start date, return an empty array
|
|
65
|
+
return taskTime.isAfter(nextRunTime, 'minute') ? [] : runTimes;
|
|
62
66
|
}
|
|
63
67
|
|
|
64
68
|
|
package/tsconfig.base.json
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"rootDir": ".", // Set rootDir to the root folder for tests and mocks
|
|
6
|
+
"types": ["vitest/globals"],
|
|
7
|
+
"noEmit": true // Prevents emitting files since these are test files
|
|
8
|
+
},
|
|
9
|
+
"include": [
|
|
10
|
+
"__tests__/**/*",
|
|
11
|
+
"__mocks__/**/*"
|
|
12
|
+
]
|
|
13
|
+
}
|