@etainabl/nodejs-sdk 1.2.44 → 1.2.45
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/dist/cjs/package.json +2 -2
- package/dist/esm/package.json +3 -0
- package/dist/index.d.cts +782 -0
- package/dist/index.d.ts +782 -0
- package/dist/index.js +626 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +588 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +28 -31
- package/.prettierrc +0 -11
- package/__tests__/reporting.test.ts +0 -88
- package/dist/cjs/api.d.ts +0 -111
- package/dist/cjs/api.js +0 -318
- package/dist/cjs/consumption.d.ts +0 -14
- package/dist/cjs/consumption.js +0 -32
- package/dist/cjs/db.d.ts +0 -6
- package/dist/cjs/db.js +0 -69
- package/dist/cjs/etainabl.d.ts +0 -10
- package/dist/cjs/etainabl.js +0 -2
- package/dist/cjs/index.d.ts +0 -10
- package/dist/cjs/index.js +0 -59
- package/dist/cjs/logger.d.ts +0 -3
- package/dist/cjs/logger.js +0 -14
- package/dist/cjs/monitoring.d.ts +0 -1
- package/dist/cjs/monitoring.js +0 -31
- package/dist/cjs/reporting.d.ts +0 -2
- package/dist/cjs/reporting.js +0 -69
- package/dist/cjs/slack.d.ts +0 -4
- package/dist/cjs/slack.js +0 -28
- package/dist/cjs/types/account.d.ts +0 -112
- package/dist/cjs/types/account.js +0 -2
- package/dist/cjs/types/address.d.ts +0 -11
- package/dist/cjs/types/address.js +0 -2
- package/dist/cjs/types/asset.d.ts +0 -122
- package/dist/cjs/types/asset.js +0 -3
- package/dist/cjs/types/automation.d.ts +0 -33
- package/dist/cjs/types/automation.js +0 -2
- package/dist/cjs/types/company.d.ts +0 -42
- package/dist/cjs/types/company.js +0 -2
- package/dist/cjs/types/dataIngest.d.ts +0 -8
- package/dist/cjs/types/dataIngest.js +0 -2
- package/dist/cjs/types/email.d.ts +0 -18
- package/dist/cjs/types/email.js +0 -2
- package/dist/cjs/types/entity.d.ts +0 -16
- package/dist/cjs/types/entity.js +0 -2
- package/dist/cjs/types/index.d.ts +0 -19
- package/dist/cjs/types/index.js +0 -2
- package/dist/cjs/types/invoice.d.ts +0 -118
- package/dist/cjs/types/invoice.js +0 -2
- package/dist/cjs/types/log.d.ts +0 -10
- package/dist/cjs/types/log.js +0 -2
- package/dist/cjs/types/portal.d.ts +0 -8
- package/dist/cjs/types/portal.js +0 -2
- package/dist/cjs/types/reading.d.ts +0 -17
- package/dist/cjs/types/reading.js +0 -2
- package/dist/cjs/types/report.d.ts +0 -21
- package/dist/cjs/types/report.js +0 -2
- package/dist/cjs/types/scraperRun.d.ts +0 -15
- package/dist/cjs/types/scraperRun.js +0 -2
- package/dist/cjs/types/statusHistory.d.ts +0 -5
- package/dist/cjs/types/statusHistory.js +0 -2
- package/dist/cjs/types/supplier.d.ts +0 -28
- package/dist/cjs/types/supplier.js +0 -3
- package/dist/cjs/units.d.ts +0 -22
- package/dist/cjs/units.js +0 -94
- package/dist/mjs/api.d.ts +0 -111
- package/dist/mjs/api.js +0 -308
- package/dist/mjs/consumption.d.ts +0 -14
- package/dist/mjs/consumption.js +0 -22
- package/dist/mjs/db.d.ts +0 -6
- package/dist/mjs/db.js +0 -53
- package/dist/mjs/etainabl.d.ts +0 -10
- package/dist/mjs/etainabl.js +0 -1
- package/dist/mjs/index.d.ts +0 -10
- package/dist/mjs/index.js +0 -10
- package/dist/mjs/logger.d.ts +0 -3
- package/dist/mjs/logger.js +0 -9
- package/dist/mjs/monitoring.d.ts +0 -1
- package/dist/mjs/monitoring.js +0 -15
- package/dist/mjs/package.json +0 -3
- package/dist/mjs/reporting.d.ts +0 -2
- package/dist/mjs/reporting.js +0 -62
- package/dist/mjs/slack.d.ts +0 -4
- package/dist/mjs/slack.js +0 -14
- package/dist/mjs/types/account.d.ts +0 -112
- package/dist/mjs/types/account.js +0 -1
- package/dist/mjs/types/address.d.ts +0 -11
- package/dist/mjs/types/address.js +0 -1
- package/dist/mjs/types/asset.d.ts +0 -122
- package/dist/mjs/types/asset.js +0 -2
- package/dist/mjs/types/automation.d.ts +0 -33
- package/dist/mjs/types/automation.js +0 -1
- package/dist/mjs/types/company.d.ts +0 -42
- package/dist/mjs/types/company.js +0 -1
- package/dist/mjs/types/dataIngest.d.ts +0 -8
- package/dist/mjs/types/dataIngest.js +0 -1
- package/dist/mjs/types/email.d.ts +0 -18
- package/dist/mjs/types/email.js +0 -1
- package/dist/mjs/types/entity.d.ts +0 -16
- package/dist/mjs/types/entity.js +0 -1
- package/dist/mjs/types/index.d.ts +0 -19
- package/dist/mjs/types/index.js +0 -1
- package/dist/mjs/types/invoice.d.ts +0 -118
- package/dist/mjs/types/invoice.js +0 -1
- package/dist/mjs/types/log.d.ts +0 -10
- package/dist/mjs/types/log.js +0 -1
- package/dist/mjs/types/portal.d.ts +0 -8
- package/dist/mjs/types/portal.js +0 -1
- package/dist/mjs/types/reading.d.ts +0 -17
- package/dist/mjs/types/reading.js +0 -1
- package/dist/mjs/types/report.d.ts +0 -21
- package/dist/mjs/types/report.js +0 -1
- package/dist/mjs/types/scraperRun.d.ts +0 -15
- package/dist/mjs/types/scraperRun.js +0 -1
- package/dist/mjs/types/statusHistory.d.ts +0 -5
- package/dist/mjs/types/statusHistory.js +0 -1
- package/dist/mjs/types/supplier.d.ts +0 -28
- package/dist/mjs/types/supplier.js +0 -2
- package/dist/mjs/units.d.ts +0 -22
- package/dist/mjs/units.js +0 -89
- package/eslint.config.js +0 -60
- package/fixup.sh +0 -11
- package/src/api.ts +0 -382
- package/src/consumption.ts +0 -40
- package/src/db.ts +0 -69
- package/src/etainabl.ts +0 -10
- package/src/index.ts +0 -21
- package/src/logger.ts +0 -13
- package/src/monitoring.ts +0 -18
- package/src/reporting.ts +0 -78
- package/src/slack.ts +0 -16
- package/src/types/account.ts +0 -116
- package/src/types/address.ts +0 -12
- package/src/types/asset.ts +0 -142
- package/src/types/automation.ts +0 -35
- package/src/types/company.ts +0 -47
- package/src/types/dataIngest.ts +0 -8
- package/src/types/email.ts +0 -18
- package/src/types/entity.ts +0 -17
- package/src/types/index.ts +0 -20
- package/src/types/invoice.ts +0 -119
- package/src/types/log.ts +0 -10
- package/src/types/portal.ts +0 -9
- package/src/types/reading.ts +0 -17
- package/src/types/report.ts +0 -21
- package/src/types/scraperRun.ts +0 -15
- package/src/types/statusHistory.ts +0 -5
- package/src/types/supplier.ts +0 -31
- package/src/units.ts +0 -111
- package/tsconfig.base.json +0 -31
- package/tsconfig.cjs.json +0 -8
- package/tsconfig.json +0 -9
- package/tsconfig.test.json +0 -13
- package/vitest.config.js +0 -10
- package/vitest.workspace.js +0 -5
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/api.ts
|
|
8
|
+
import axios from "axios";
|
|
9
|
+
import https from "https";
|
|
10
|
+
|
|
11
|
+
// src/logger.ts
|
|
12
|
+
import winston from "winston";
|
|
13
|
+
var logger_default = (namespace) => winston.createLogger({
|
|
14
|
+
level: "debug",
|
|
15
|
+
format: winston.format.combine(
|
|
16
|
+
winston.format.timestamp(),
|
|
17
|
+
winston.format.json()
|
|
18
|
+
),
|
|
19
|
+
defaultMeta: { service: process.env.AWS_LAMBDA_FUNCTION_NAME, script: namespace },
|
|
20
|
+
transports: [
|
|
21
|
+
new winston.transports.Console()
|
|
22
|
+
]
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// src/api.ts
|
|
26
|
+
var log = logger_default("etainablApi");
|
|
27
|
+
function _handleResponse(req, res, isPaged = false) {
|
|
28
|
+
if (!res) {
|
|
29
|
+
throw new Error(`No response from API (${req.method} ${req.url})`);
|
|
30
|
+
}
|
|
31
|
+
if (res.status !== 200) {
|
|
32
|
+
throw new Error(`${res.status} ${res.statusText} response from API (${req.method} ${req.url})`);
|
|
33
|
+
}
|
|
34
|
+
if (!res.data) {
|
|
35
|
+
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
36
|
+
}
|
|
37
|
+
if (isPaged && !res.data.data) {
|
|
38
|
+
throw new Error(`No data from API (${req.method} ${req.url})`);
|
|
39
|
+
}
|
|
40
|
+
return res;
|
|
41
|
+
}
|
|
42
|
+
var factory = {
|
|
43
|
+
getWithId: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
|
|
44
|
+
const req = {
|
|
45
|
+
method: "GET",
|
|
46
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ""}`
|
|
47
|
+
};
|
|
48
|
+
log.info(`API Request: ${req.method} ${process.env.ETAsINABL_API_URL}/${req.url}`);
|
|
49
|
+
let res;
|
|
50
|
+
try {
|
|
51
|
+
res = await etainablApi.get(req.url, options);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
57
|
+
_handleResponse(req, res);
|
|
58
|
+
return res.data;
|
|
59
|
+
},
|
|
60
|
+
get: (etainablApi, endpoint, postEndpoint) => async (options = {}) => {
|
|
61
|
+
const req = {
|
|
62
|
+
method: "GET",
|
|
63
|
+
url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ""}`
|
|
64
|
+
};
|
|
65
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
66
|
+
let res;
|
|
67
|
+
try {
|
|
68
|
+
res = await etainablApi.get(req.url, options);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
74
|
+
_handleResponse(req, res);
|
|
75
|
+
return res.data;
|
|
76
|
+
},
|
|
77
|
+
list: (etainablApi, endpoint, postEndpoint) => async (options = {}) => {
|
|
78
|
+
const req = {
|
|
79
|
+
method: "GET",
|
|
80
|
+
url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ""}`
|
|
81
|
+
};
|
|
82
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
83
|
+
let res;
|
|
84
|
+
try {
|
|
85
|
+
res = await etainablApi.get(req.url, options);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
91
|
+
_handleResponse(req, res, true);
|
|
92
|
+
return res.data;
|
|
93
|
+
},
|
|
94
|
+
update: (etainablApi, endpoint, postEndpoint) => async (id, data, options = {}) => {
|
|
95
|
+
const req = {
|
|
96
|
+
method: "PATCH",
|
|
97
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ""}`
|
|
98
|
+
};
|
|
99
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
100
|
+
let res;
|
|
101
|
+
try {
|
|
102
|
+
res = await etainablApi.patch(req.url, data, options);
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
105
|
+
throw e;
|
|
106
|
+
}
|
|
107
|
+
_handleResponse(req, res);
|
|
108
|
+
return res.data;
|
|
109
|
+
},
|
|
110
|
+
create: (etainablApi, endpoint, postEndpoint) => async (data, options = {}) => {
|
|
111
|
+
const req = {
|
|
112
|
+
method: "POST",
|
|
113
|
+
url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ""}`
|
|
114
|
+
};
|
|
115
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
116
|
+
let res;
|
|
117
|
+
try {
|
|
118
|
+
res = await etainablApi.post(req.url, data, options);
|
|
119
|
+
} catch (e) {
|
|
120
|
+
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
121
|
+
throw e;
|
|
122
|
+
}
|
|
123
|
+
_handleResponse(req, res);
|
|
124
|
+
return res.data;
|
|
125
|
+
},
|
|
126
|
+
remove: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
|
|
127
|
+
const req = {
|
|
128
|
+
method: "DELETE",
|
|
129
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ""}`
|
|
130
|
+
};
|
|
131
|
+
let res;
|
|
132
|
+
log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
133
|
+
try {
|
|
134
|
+
res = await etainablApi.delete(req.url, options);
|
|
135
|
+
} catch (e) {
|
|
136
|
+
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
137
|
+
throw e;
|
|
138
|
+
}
|
|
139
|
+
_handleResponse(req, res);
|
|
140
|
+
return res.data;
|
|
141
|
+
},
|
|
142
|
+
customWithId: (etainablApi, method, endpoint, postEndpoint) => async (id, data, options = {}) => {
|
|
143
|
+
const req = {
|
|
144
|
+
method,
|
|
145
|
+
url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ""}`,
|
|
146
|
+
data,
|
|
147
|
+
...options
|
|
148
|
+
};
|
|
149
|
+
log.info(`API Request (Custom): ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
|
|
150
|
+
let res;
|
|
151
|
+
try {
|
|
152
|
+
res = await etainablApi.request(req);
|
|
153
|
+
} catch (e) {
|
|
154
|
+
if (e.response?.data) throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
|
|
155
|
+
throw e;
|
|
156
|
+
}
|
|
157
|
+
_handleResponse(req, res);
|
|
158
|
+
return res.data;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
var subFactory = {
|
|
162
|
+
// e.g. POST /assets/:id/documents
|
|
163
|
+
create: (etainablApi, endpoint, subEndpoint) => async (id, data, options = {}) => {
|
|
164
|
+
const subUrl = `${id}/${subEndpoint}`;
|
|
165
|
+
return factory.create(etainablApi, endpoint, subUrl)(data, options);
|
|
166
|
+
},
|
|
167
|
+
// e.g. PATCH /assets/:id/documents/:documentId
|
|
168
|
+
update: (etainablApi, endpoint, subEndpoint) => async (id, subId, data, options = {}) => {
|
|
169
|
+
const subUrl = `${subEndpoint}/${subId}`;
|
|
170
|
+
return factory.update(etainablApi, endpoint, subUrl)(id, data, options);
|
|
171
|
+
},
|
|
172
|
+
// e.g. DELETE /assets/:id/documents/:documentId
|
|
173
|
+
remove: (etainablApi, endpoint, subEndpoint) => async (id, subId, options = {}) => {
|
|
174
|
+
const subUrl = `${subEndpoint}/${subId}`;
|
|
175
|
+
return factory.remove(etainablApi, endpoint, subUrl)(id, options);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
var api_default = (auth, instanceOptions = {}) => {
|
|
179
|
+
try {
|
|
180
|
+
const headers = {};
|
|
181
|
+
if (auth.key) {
|
|
182
|
+
headers["x-key"] = auth.key;
|
|
183
|
+
} else if (auth.token) {
|
|
184
|
+
headers["Authorization"] = auth.token;
|
|
185
|
+
} else {
|
|
186
|
+
headers["x-key"] = process.env.ETAINABL_API_KEY;
|
|
187
|
+
}
|
|
188
|
+
const etainablApi = axios.create({
|
|
189
|
+
baseURL: process.env.ETAINABL_API_URL,
|
|
190
|
+
timeout: 3e5,
|
|
191
|
+
httpsAgent: new https.Agent({ keepAlive: true }),
|
|
192
|
+
headers,
|
|
193
|
+
...instanceOptions
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
instance: etainablApi,
|
|
197
|
+
// accounts
|
|
198
|
+
getAccount: factory.getWithId(etainablApi, "accounts"),
|
|
199
|
+
listAccounts: factory.list(etainablApi, "accounts"),
|
|
200
|
+
updateAccount: factory.update(etainablApi, "accounts"),
|
|
201
|
+
createAccount: factory.create(etainablApi, "accounts"),
|
|
202
|
+
removeAccount: factory.remove(etainablApi, "accounts"),
|
|
203
|
+
getAccountSchema: factory.get(etainablApi, "accounts", "schema"),
|
|
204
|
+
invalidateAccountCache: factory.customWithId(etainablApi, "put", "accounts", "invalidate-cache"),
|
|
205
|
+
// assets
|
|
206
|
+
getAsset: factory.getWithId(etainablApi, "assets"),
|
|
207
|
+
listAssets: factory.list(etainablApi, "assets"),
|
|
208
|
+
updateAsset: factory.update(etainablApi, "assets"),
|
|
209
|
+
createAsset: factory.create(etainablApi, "assets"),
|
|
210
|
+
removeAsset: factory.remove(etainablApi, "assets"),
|
|
211
|
+
getAssetSchema: factory.get(etainablApi, "assets", "schema"),
|
|
212
|
+
// assetGroups
|
|
213
|
+
getAssetGroup: factory.getWithId(etainablApi, "asset-groups"),
|
|
214
|
+
listAssetGroups: factory.list(etainablApi, "asset-groups"),
|
|
215
|
+
updateAssetGroup: factory.update(etainablApi, "asset-groups"),
|
|
216
|
+
createAssetGroup: factory.create(etainablApi, "asset-groups"),
|
|
217
|
+
removeAssetGroup: factory.remove(etainablApi, "asset-groups"),
|
|
218
|
+
getAssetGroupAssets: factory.getWithId(etainablApi, "asset-groups", "assets"),
|
|
219
|
+
getAssetGroupSchema: factory.get(etainablApi, "asset-groups", "schema"),
|
|
220
|
+
// automation
|
|
221
|
+
getAutomation: factory.getWithId(etainablApi, "automation"),
|
|
222
|
+
listAutomations: factory.list(etainablApi, "automation"),
|
|
223
|
+
updateAutomation: factory.update(etainablApi, "automation"),
|
|
224
|
+
createAutomation: factory.create(etainablApi, "automation"),
|
|
225
|
+
removeAutomation: factory.remove(etainablApi, "automation"),
|
|
226
|
+
createAutomationLog: subFactory.create(etainablApi, "automation", "logs"),
|
|
227
|
+
updateAutomationLog: subFactory.update(etainablApi, "automation", "logs"),
|
|
228
|
+
removeAutomationLog: subFactory.remove(etainablApi, "automation", "logs"),
|
|
229
|
+
// company
|
|
230
|
+
getCompany: factory.getWithId(etainablApi, "companies"),
|
|
231
|
+
// consumption
|
|
232
|
+
getConsumption: factory.getWithId(etainablApi, "consumptions"),
|
|
233
|
+
listConsumptions: factory.list(etainablApi, "consumptions"),
|
|
234
|
+
updateConsumption: factory.update(etainablApi, "consumptions"),
|
|
235
|
+
createConsumption: factory.create(etainablApi, "consumptions"),
|
|
236
|
+
removeConsumption: factory.remove(etainablApi, "consumptions"),
|
|
237
|
+
getConsumptionSchema: factory.get(etainablApi, "consumptions", "schema"),
|
|
238
|
+
// emails
|
|
239
|
+
getEmail: factory.getWithId(etainablApi, "emails"),
|
|
240
|
+
listEmails: factory.list(etainablApi, "emails"),
|
|
241
|
+
updateEmail: factory.update(etainablApi, "emails"),
|
|
242
|
+
createEmail: factory.create(etainablApi, "emails"),
|
|
243
|
+
removeEmail: factory.remove(etainablApi, "emails"),
|
|
244
|
+
// emission factors
|
|
245
|
+
getEmissionFactor: factory.getWithId(etainablApi, "emission-factors"),
|
|
246
|
+
listEmissionFactors: factory.list(etainablApi, "emission-factors"),
|
|
247
|
+
updateEmissionFactor: factory.update(etainablApi, "emission-factors"),
|
|
248
|
+
createEmissionFactor: factory.create(etainablApi, "emission-factors"),
|
|
249
|
+
removeEmissionFactor: factory.remove(etainablApi, "emission-factors"),
|
|
250
|
+
// entity
|
|
251
|
+
getEntity: factory.getWithId(etainablApi, "entities"),
|
|
252
|
+
listEntities: factory.list(etainablApi, "entities"),
|
|
253
|
+
updateEntity: factory.update(etainablApi, "entities"),
|
|
254
|
+
createEntity: factory.create(etainablApi, "entities"),
|
|
255
|
+
removeEntity: factory.remove(etainablApi, "entities"),
|
|
256
|
+
getEntitiesSchema: factory.get(etainablApi, "entities", "schema"),
|
|
257
|
+
// logs
|
|
258
|
+
getLog: factory.getWithId(etainablApi, "logs"),
|
|
259
|
+
listLogs: factory.list(etainablApi, "logs"),
|
|
260
|
+
updateLog: factory.update(etainablApi, "logs"),
|
|
261
|
+
createLog: factory.create(etainablApi, "logs"),
|
|
262
|
+
removeLog: factory.remove(etainablApi, "logs"),
|
|
263
|
+
// readings
|
|
264
|
+
getReading: factory.getWithId(etainablApi, "readings"),
|
|
265
|
+
listReadings: factory.list(etainablApi, "readings"),
|
|
266
|
+
updateReading: factory.update(etainablApi, "readings"),
|
|
267
|
+
createReading: factory.create(etainablApi, "readings"),
|
|
268
|
+
removeReading: factory.remove(etainablApi, "readings"),
|
|
269
|
+
getReadingSchema: factory.get(etainablApi, "readings", "schema"),
|
|
270
|
+
// reports
|
|
271
|
+
getReport: factory.getWithId(etainablApi, "reports"),
|
|
272
|
+
listReports: factory.list(etainablApi, "reports"),
|
|
273
|
+
updateReport: factory.update(etainablApi, "reports"),
|
|
274
|
+
createReport: factory.create(etainablApi, "reports"),
|
|
275
|
+
removeReport: factory.remove(etainablApi, "reports"),
|
|
276
|
+
sendReport: factory.customWithId(etainablApi, "post", "reports", "send"),
|
|
277
|
+
// report templates
|
|
278
|
+
getReportTemplate: factory.getWithId(etainablApi, "report-templates"),
|
|
279
|
+
listReportTemplates: factory.list(etainablApi, "report-templates"),
|
|
280
|
+
updateReportTemplate: factory.update(etainablApi, "report-templates"),
|
|
281
|
+
createReportTemplate: factory.create(etainablApi, "report-templates"),
|
|
282
|
+
removeReportTemplate: factory.remove(etainablApi, "report-templates"),
|
|
283
|
+
// scheduled reports
|
|
284
|
+
getScheduledReport: factory.getWithId(etainablApi, "scheduled-reports"),
|
|
285
|
+
listScheduledReports: factory.list(etainablApi, "scheduled-reports"),
|
|
286
|
+
updateScheduledReport: factory.update(etainablApi, "scheduled-reports"),
|
|
287
|
+
createScheduledReport: factory.create(etainablApi, "scheduled-reports"),
|
|
288
|
+
removeScheduledReport: factory.remove(etainablApi, "scheduled-reports"),
|
|
289
|
+
sendScheduledReport: factory.customWithId(etainablApi, "post", "scheduled-reports", "send"),
|
|
290
|
+
// invoices
|
|
291
|
+
getInvoice: factory.getWithId(etainablApi, "invoices"),
|
|
292
|
+
listInvoices: factory.list(etainablApi, "invoices"),
|
|
293
|
+
updateInvoice: factory.update(etainablApi, "invoices"),
|
|
294
|
+
createInvoice: factory.create(etainablApi, "invoices"),
|
|
295
|
+
removeInvoice: factory.remove(etainablApi, "invoices"),
|
|
296
|
+
getInvoiceSchema: factory.get(etainablApi, "invoices", "schema"),
|
|
297
|
+
//suppliers
|
|
298
|
+
listSuppliers: factory.list(etainablApi, "suppliers"),
|
|
299
|
+
getSupplierSchema: factory.get(etainablApi, "suppliers", "schema"),
|
|
300
|
+
// import templates
|
|
301
|
+
getImportTemplate: factory.getWithId(etainablApi, "import-templates"),
|
|
302
|
+
//data imports
|
|
303
|
+
updateDataIngest: factory.update(etainablApi, "data-ingests"),
|
|
304
|
+
createDataIngest: factory.create(etainablApi, "data-ingests"),
|
|
305
|
+
listDataIngest: factory.list(etainablApi, "data-ingests")
|
|
306
|
+
};
|
|
307
|
+
} catch (e) {
|
|
308
|
+
log.error(e);
|
|
309
|
+
throw e;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// src/db.ts
|
|
314
|
+
import { MongoClient } from "mongodb";
|
|
315
|
+
var log2 = logger_default("dbHelpers");
|
|
316
|
+
var cachedDb;
|
|
317
|
+
async function connectToDatabase(retryAttempt = 1) {
|
|
318
|
+
if (!process.env.ETAINABL_DB_URL) throw new Error("ETAINABL_DB_URL is not set");
|
|
319
|
+
if (!process.env.AWS_ACCESS_KEY_ID) throw new Error("AWS_ACCESS_KEY_ID is not set");
|
|
320
|
+
if (!process.env.AWS_SECRET_ACCESS_KEY) throw new Error("AWS_SECRET_ACCESS_KEY is not set");
|
|
321
|
+
if (cachedDb) {
|
|
322
|
+
log2.debug("Using cached MongoDB connection.");
|
|
323
|
+
return Promise.resolve(cachedDb);
|
|
324
|
+
}
|
|
325
|
+
const uri = `mongodb+srv://${process.env.ETAINABL_DB_URL}`;
|
|
326
|
+
try {
|
|
327
|
+
if (process.env.DB_BASIC_AUTH === "true") {
|
|
328
|
+
log2.debug("Connecting to MongoDB server... (Auth: Basic)");
|
|
329
|
+
const client2 = new MongoClient(uri);
|
|
330
|
+
await client2.connect();
|
|
331
|
+
log2.debug("Connected successfully to MongoDB server! (Auth: Basic)");
|
|
332
|
+
cachedDb = client2.db("etainabl");
|
|
333
|
+
return cachedDb;
|
|
334
|
+
}
|
|
335
|
+
log2.debug("Connecting to MongoDB server... (Auth: AWS)");
|
|
336
|
+
const client = new MongoClient(uri, {
|
|
337
|
+
auth: {
|
|
338
|
+
username: process.env.AWS_ACCESS_KEY_ID,
|
|
339
|
+
password: process.env.AWS_SECRET_ACCESS_KEY
|
|
340
|
+
},
|
|
341
|
+
authSource: "$external",
|
|
342
|
+
authMechanism: "MONGODB-AWS"
|
|
343
|
+
});
|
|
344
|
+
await client.connect();
|
|
345
|
+
log2.debug("Connected successfully to MongoDB server! (Auth: AWS)");
|
|
346
|
+
cachedDb = client.db("etainabl");
|
|
347
|
+
return cachedDb;
|
|
348
|
+
} catch (e) {
|
|
349
|
+
if (retryAttempt > 5) {
|
|
350
|
+
console.log(`Error connecting to MongoDB server after 5 attempts...`);
|
|
351
|
+
throw e;
|
|
352
|
+
}
|
|
353
|
+
console.log(`MongoDB Connection error: ${e.message}`);
|
|
354
|
+
console.log(`Error connecting to MongoDB server... Retrying in 3 seconds... (Attempt ${retryAttempt})`);
|
|
355
|
+
return connectToDatabase(retryAttempt + 1);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
var db_default = {
|
|
359
|
+
connectToDatabase
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
// src/slack.ts
|
|
363
|
+
import axios2 from "axios";
|
|
364
|
+
var postMessage = async (message) => {
|
|
365
|
+
const url = "https://hooks.slack.com/services/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";
|
|
366
|
+
const data = {
|
|
367
|
+
text: `[${(process.env.ENV || "").toUpperCase()}][${process.env.AWS_LAMBDA_FUNCTION_NAME}] ${message}`
|
|
368
|
+
};
|
|
369
|
+
const headers = {
|
|
370
|
+
"Content-Type": "application/json"
|
|
371
|
+
};
|
|
372
|
+
return axios2.post(url, data, { headers });
|
|
373
|
+
};
|
|
374
|
+
var slack_default = {
|
|
375
|
+
postMessage
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/units.ts
|
|
379
|
+
var units_exports = {};
|
|
380
|
+
__export(units_exports, {
|
|
381
|
+
accountTypeMap: () => accountTypeMap,
|
|
382
|
+
accountTypeUnitMap: () => accountTypeUnitMap,
|
|
383
|
+
checkAccountTypeVsUnits: () => checkAccountTypeVsUnits,
|
|
384
|
+
convertItems: () => convertItems
|
|
385
|
+
});
|
|
386
|
+
var accountTypeMap = {
|
|
387
|
+
electricity: "kwh",
|
|
388
|
+
gas: "kwh",
|
|
389
|
+
water: "m3",
|
|
390
|
+
waste: "kg",
|
|
391
|
+
solar: "kwh",
|
|
392
|
+
heating: "kwh",
|
|
393
|
+
flow: "m3/h",
|
|
394
|
+
cooling: "kwh",
|
|
395
|
+
temperature: "C",
|
|
396
|
+
oil: "l"
|
|
397
|
+
};
|
|
398
|
+
var unitConversionFactors = {
|
|
399
|
+
kwh: {
|
|
400
|
+
kwh: 1,
|
|
401
|
+
mwh: 1e3,
|
|
402
|
+
wh: 1e-3,
|
|
403
|
+
m3: 39 * 1.02264 / 3.6,
|
|
404
|
+
ft3: 0.0283 * 39 * 1.02264 / 3.6,
|
|
405
|
+
hcf: 2.83 * 39 * 1.02264 / 3.6
|
|
406
|
+
},
|
|
407
|
+
m3: {
|
|
408
|
+
m3: 1,
|
|
409
|
+
l: 1e-3
|
|
410
|
+
},
|
|
411
|
+
C: {
|
|
412
|
+
C: 1
|
|
413
|
+
},
|
|
414
|
+
kg: {
|
|
415
|
+
kg: 1,
|
|
416
|
+
lbs: 0.45359237,
|
|
417
|
+
tonnes: 1e3
|
|
418
|
+
},
|
|
419
|
+
"m3/h": {
|
|
420
|
+
"m3/h": 1
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
var accountTypeUnitMap = {
|
|
424
|
+
electricity: ["kwh", "mwh", "wh"],
|
|
425
|
+
gas: ["kwh", "m3", "ft3", "hcf"],
|
|
426
|
+
water: ["m3", "l"],
|
|
427
|
+
waste: ["kg", "lbs", "tonnes"],
|
|
428
|
+
solar: ["kwh", "mwh", "wh"],
|
|
429
|
+
heating: ["kwh", "mwh", "wh"],
|
|
430
|
+
flow: ["m3/h"],
|
|
431
|
+
cooling: ["kwh", "mwh", "wh"],
|
|
432
|
+
temperature: ["C"],
|
|
433
|
+
oil: ["l"]
|
|
434
|
+
};
|
|
435
|
+
var convertItems = (items, type, defaultUnits, accountFactor) => {
|
|
436
|
+
if (!type) throw new Error("Account type is required");
|
|
437
|
+
const baseUnit = accountTypeMap[type];
|
|
438
|
+
if (!baseUnit) throw new Error(`Account type ${type} is not supported`);
|
|
439
|
+
const convertedItems = items.map((item) => {
|
|
440
|
+
const factor = item.factor || accountFactor || 1;
|
|
441
|
+
const units = item.units || item.unit || defaultUnits || baseUnit;
|
|
442
|
+
const convertedValue = item.value * _getConversionFactor(units, baseUnit) * factor;
|
|
443
|
+
return { ...item, value: convertedValue, units: baseUnit };
|
|
444
|
+
});
|
|
445
|
+
return convertedItems;
|
|
446
|
+
};
|
|
447
|
+
var _getConversionFactor = (fromUnit = "kwh", toUnit) => {
|
|
448
|
+
const conversionFactors = unitConversionFactors[toUnit];
|
|
449
|
+
if (!conversionFactors) {
|
|
450
|
+
throw new Error(`Conversion factor base unit ${toUnit} is not defined`);
|
|
451
|
+
}
|
|
452
|
+
if (!conversionFactors[fromUnit]) {
|
|
453
|
+
throw new Error(`Conversion factor from unit ${fromUnit} is not defined`);
|
|
454
|
+
}
|
|
455
|
+
return conversionFactors[fromUnit];
|
|
456
|
+
};
|
|
457
|
+
var checkAccountTypeVsUnits = (type, unit, additionalLog = "") => {
|
|
458
|
+
if (!type) throw new Error("Account type is required");
|
|
459
|
+
if (!unit) throw new Error("Unit is required");
|
|
460
|
+
const parsedType = type.toLowerCase().trim();
|
|
461
|
+
const accountTypeUnits = accountTypeUnitMap[parsedType];
|
|
462
|
+
if (!accountTypeUnits) throw new Error(`Account type "${parsedType}" is not supported ${additionalLog}`);
|
|
463
|
+
const parsedUnit = unit.toLowerCase().trim();
|
|
464
|
+
if (!accountTypeUnits.includes(parsedUnit)) {
|
|
465
|
+
throw new Error(`Account type "${parsedType}" does not support unit "${parsedUnit}" ${additionalLog}`);
|
|
466
|
+
}
|
|
467
|
+
return { type: parsedType, unit: parsedUnit };
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
// src/consumption.ts
|
|
471
|
+
var consumption_exports = {};
|
|
472
|
+
__export(consumption_exports, {
|
|
473
|
+
calcMaxConsumptionValue: () => calcMaxConsumptionValue,
|
|
474
|
+
calcMaxDemand: () => calcMaxDemand,
|
|
475
|
+
calcPeakLoad: () => calcPeakLoad,
|
|
476
|
+
dayNightConsumption: () => dayNightConsumption
|
|
477
|
+
});
|
|
478
|
+
import moment from "moment";
|
|
479
|
+
var dayNightConsumption = (data) => data.reduce((acc, item) => {
|
|
480
|
+
const hour = moment.utc(item.date).hour();
|
|
481
|
+
if (hour >= 0 && hour < 7) {
|
|
482
|
+
acc.nightConsumption += item.consumption;
|
|
483
|
+
} else {
|
|
484
|
+
acc.dayConsumption += item.consumption;
|
|
485
|
+
}
|
|
486
|
+
acc.consumption += item.consumption;
|
|
487
|
+
return acc;
|
|
488
|
+
}, {
|
|
489
|
+
dayConsumption: 0,
|
|
490
|
+
nightConsumption: 0,
|
|
491
|
+
consumption: 0
|
|
492
|
+
});
|
|
493
|
+
var calcMaxConsumptionValue = (data) => Math.max(...data.map((item) => item.consumption));
|
|
494
|
+
var calcMaxDemand = (data) => calcMaxConsumptionValue(data) * 2;
|
|
495
|
+
var calcPeakLoad = (consumption, maxDemand, startDate, endDate) => {
|
|
496
|
+
const days = Math.ceil(moment(endDate).diff(moment(startDate), "days", true));
|
|
497
|
+
return maxDemand === 0 ? 0 : consumption / (maxDemand * 24 * days) * 100;
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
// src/monitoring.ts
|
|
501
|
+
var monitoring_exports = {};
|
|
502
|
+
__export(monitoring_exports, {
|
|
503
|
+
sendHeartbeat: () => sendHeartbeat
|
|
504
|
+
});
|
|
505
|
+
import axios3 from "axios";
|
|
506
|
+
var log3 = logger_default("monitoring");
|
|
507
|
+
var sendHeartbeat = async () => {
|
|
508
|
+
if (!process.env.HEARTBEAT_URL || process.env.HEARTBEAT_URL.endsWith("/")) return false;
|
|
509
|
+
try {
|
|
510
|
+
await axios3.post(process.env.HEARTBEAT_URL);
|
|
511
|
+
return true;
|
|
512
|
+
} catch (e) {
|
|
513
|
+
log3.warn(`Failed to send heartbeat: ${e.message || e}`);
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// src/reporting.ts
|
|
519
|
+
var reporting_exports = {};
|
|
520
|
+
__export(reporting_exports, {
|
|
521
|
+
getScheduledReportRunTimes: () => getScheduledReportRunTimes
|
|
522
|
+
});
|
|
523
|
+
import moment2 from "moment";
|
|
524
|
+
moment2.locale("en", {
|
|
525
|
+
week: {
|
|
526
|
+
dow: 1
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
var getNextRunTime = (startDate, schedule, taskTime) => {
|
|
530
|
+
const [num, freq] = schedule.frequency.split("|");
|
|
531
|
+
const targetDate = moment2(startDate).add(num, freq);
|
|
532
|
+
if (schedule.frequencyPeriod === "first") {
|
|
533
|
+
targetDate.startOf(freq);
|
|
534
|
+
} else if (schedule.frequencyPeriod === "last") {
|
|
535
|
+
targetDate.endOf(freq);
|
|
536
|
+
}
|
|
537
|
+
const isWeekday = targetDate.isoWeekday() > 0 && targetDate.isoWeekday() < 6;
|
|
538
|
+
const isSaturday = targetDate.isoWeekday() === 6;
|
|
539
|
+
if (schedule.frequencyDay === "weekdays" && !isWeekday) {
|
|
540
|
+
if (targetDate.date() / 7 < 2) {
|
|
541
|
+
targetDate.add(isSaturday ? 2 : 1, "days");
|
|
542
|
+
} else {
|
|
543
|
+
targetDate.subtract(isSaturday ? 1 : 2, "days");
|
|
544
|
+
}
|
|
545
|
+
} else if (schedule.frequencyDay === "weekends" && isWeekday) {
|
|
546
|
+
if (targetDate.date() / 7 < 2) {
|
|
547
|
+
targetDate.isoWeekday(6);
|
|
548
|
+
} else {
|
|
549
|
+
targetDate.isoWeekday(0);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (taskTime.isAfter(targetDate, "minute")) {
|
|
553
|
+
return getNextRunTime(targetDate, schedule, taskTime);
|
|
554
|
+
}
|
|
555
|
+
return targetDate;
|
|
556
|
+
};
|
|
557
|
+
var getScheduledReportRunTimes = (schedule, limit = 1, taskTime = moment2()) => {
|
|
558
|
+
if (!schedule.startDate || !schedule.enabled) return [];
|
|
559
|
+
const originalStartDate = moment2.utc(schedule.startDate);
|
|
560
|
+
let startDate = originalStartDate;
|
|
561
|
+
const includeStartDate = taskTime.isSameOrBefore(originalStartDate, "minute");
|
|
562
|
+
let runTimes = [];
|
|
563
|
+
if (includeStartDate) {
|
|
564
|
+
runTimes = [originalStartDate];
|
|
565
|
+
}
|
|
566
|
+
const [, freq] = schedule.frequency.split("|");
|
|
567
|
+
if (freq === "once") {
|
|
568
|
+
const nextRunTime = runTimes[0];
|
|
569
|
+
return taskTime.isAfter(nextRunTime, "minute") ? [] : runTimes;
|
|
570
|
+
}
|
|
571
|
+
const scheduleRunTimes = Array.from(Array(includeStartDate ? limit - 1 : limit).keys()).map(() => {
|
|
572
|
+
const nextRunTime = getNextRunTime(startDate, schedule, taskTime);
|
|
573
|
+
startDate = nextRunTime.hour(originalStartDate.hour()).minute(originalStartDate.minute());
|
|
574
|
+
return nextRunTime;
|
|
575
|
+
});
|
|
576
|
+
return [...runTimes, ...scheduleRunTimes];
|
|
577
|
+
};
|
|
578
|
+
export {
|
|
579
|
+
api_default as api,
|
|
580
|
+
consumption_exports as consumption,
|
|
581
|
+
db_default as db,
|
|
582
|
+
logger_default as logger,
|
|
583
|
+
monitoring_exports as monitoring,
|
|
584
|
+
reporting_exports as reporting,
|
|
585
|
+
slack_default as slack,
|
|
586
|
+
units_exports as units
|
|
587
|
+
};
|
|
588
|
+
//# sourceMappingURL=index.mjs.map
|