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