@etainabl/nodejs-sdk 1.2.42 → 1.2.44

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.
Files changed (110) hide show
  1. package/dist/cjs/api.d.ts +111 -0
  2. package/dist/cjs/api.js +318 -0
  3. package/dist/cjs/consumption.d.ts +14 -0
  4. package/dist/cjs/consumption.js +32 -0
  5. package/dist/cjs/db.d.ts +6 -0
  6. package/dist/cjs/db.js +69 -0
  7. package/dist/cjs/etainabl.d.ts +10 -0
  8. package/dist/cjs/etainabl.js +2 -0
  9. package/dist/cjs/index.d.ts +10 -0
  10. package/dist/cjs/logger.d.ts +3 -0
  11. package/dist/cjs/logger.js +14 -0
  12. package/dist/cjs/monitoring.d.ts +1 -0
  13. package/dist/cjs/monitoring.js +31 -0
  14. package/dist/cjs/package.json +3 -0
  15. package/dist/cjs/reporting.d.ts +2 -0
  16. package/dist/cjs/reporting.js +69 -0
  17. package/dist/cjs/slack.d.ts +4 -0
  18. package/dist/cjs/slack.js +28 -0
  19. package/dist/cjs/types/account.d.ts +112 -0
  20. package/dist/cjs/types/account.js +2 -0
  21. package/dist/cjs/types/address.d.ts +11 -0
  22. package/dist/cjs/types/address.js +2 -0
  23. package/dist/cjs/types/asset.d.ts +122 -0
  24. package/dist/cjs/types/asset.js +3 -0
  25. package/dist/cjs/types/automation.d.ts +33 -0
  26. package/dist/cjs/types/automation.js +2 -0
  27. package/dist/cjs/types/company.d.ts +42 -0
  28. package/dist/cjs/types/company.js +2 -0
  29. package/dist/cjs/types/dataIngest.d.ts +8 -0
  30. package/dist/cjs/types/dataIngest.js +2 -0
  31. package/dist/cjs/types/email.d.ts +18 -0
  32. package/dist/cjs/types/email.js +2 -0
  33. package/dist/cjs/types/entity.d.ts +16 -0
  34. package/dist/cjs/types/entity.js +2 -0
  35. package/dist/cjs/types/index.d.ts +19 -0
  36. package/dist/cjs/types/index.js +2 -0
  37. package/dist/cjs/types/invoice.d.ts +118 -0
  38. package/dist/cjs/types/invoice.js +2 -0
  39. package/dist/cjs/types/log.d.ts +10 -0
  40. package/dist/cjs/types/log.js +2 -0
  41. package/dist/cjs/types/portal.d.ts +8 -0
  42. package/dist/cjs/types/portal.js +2 -0
  43. package/dist/cjs/types/reading.d.ts +17 -0
  44. package/dist/cjs/types/reading.js +2 -0
  45. package/dist/cjs/types/report.d.ts +21 -0
  46. package/dist/cjs/types/report.js +2 -0
  47. package/dist/cjs/types/scraperRun.d.ts +15 -0
  48. package/dist/cjs/types/scraperRun.js +2 -0
  49. package/dist/cjs/types/statusHistory.d.ts +5 -0
  50. package/dist/cjs/types/statusHistory.js +2 -0
  51. package/dist/cjs/types/supplier.d.ts +28 -0
  52. package/dist/cjs/types/supplier.js +3 -0
  53. package/dist/cjs/units.d.ts +22 -0
  54. package/dist/cjs/units.js +94 -0
  55. package/dist/mjs/api.d.ts +111 -0
  56. package/dist/mjs/api.js +308 -0
  57. package/dist/mjs/consumption.d.ts +14 -0
  58. package/dist/mjs/consumption.js +22 -0
  59. package/dist/mjs/db.d.ts +6 -0
  60. package/dist/mjs/db.js +53 -0
  61. package/dist/mjs/etainabl.d.ts +10 -0
  62. package/dist/mjs/etainabl.js +1 -0
  63. package/dist/mjs/index.d.ts +10 -0
  64. package/dist/mjs/index.js +10 -0
  65. package/dist/mjs/logger.d.ts +3 -0
  66. package/dist/mjs/logger.js +9 -0
  67. package/dist/mjs/monitoring.d.ts +1 -0
  68. package/dist/mjs/monitoring.js +15 -0
  69. package/dist/mjs/package.json +3 -0
  70. package/dist/mjs/reporting.d.ts +2 -0
  71. package/dist/mjs/reporting.js +62 -0
  72. package/dist/mjs/slack.d.ts +4 -0
  73. package/dist/mjs/slack.js +14 -0
  74. package/dist/mjs/types/account.d.ts +112 -0
  75. package/dist/mjs/types/account.js +1 -0
  76. package/dist/mjs/types/address.d.ts +11 -0
  77. package/dist/mjs/types/address.js +1 -0
  78. package/dist/mjs/types/asset.d.ts +122 -0
  79. package/dist/mjs/types/asset.js +2 -0
  80. package/dist/mjs/types/automation.d.ts +33 -0
  81. package/dist/mjs/types/automation.js +1 -0
  82. package/dist/mjs/types/company.d.ts +42 -0
  83. package/dist/mjs/types/company.js +1 -0
  84. package/dist/mjs/types/dataIngest.d.ts +8 -0
  85. package/dist/mjs/types/dataIngest.js +1 -0
  86. package/dist/mjs/types/email.d.ts +18 -0
  87. package/dist/mjs/types/email.js +1 -0
  88. package/dist/mjs/types/entity.d.ts +16 -0
  89. package/dist/mjs/types/entity.js +1 -0
  90. package/dist/mjs/types/index.d.ts +19 -0
  91. package/dist/mjs/types/index.js +1 -0
  92. package/dist/mjs/types/invoice.d.ts +118 -0
  93. package/dist/mjs/types/invoice.js +1 -0
  94. package/dist/mjs/types/log.d.ts +10 -0
  95. package/dist/mjs/types/log.js +1 -0
  96. package/dist/mjs/types/portal.d.ts +8 -0
  97. package/dist/mjs/types/portal.js +1 -0
  98. package/dist/mjs/types/reading.d.ts +17 -0
  99. package/dist/mjs/types/reading.js +1 -0
  100. package/dist/mjs/types/report.d.ts +21 -0
  101. package/dist/mjs/types/report.js +1 -0
  102. package/dist/mjs/types/scraperRun.d.ts +15 -0
  103. package/dist/mjs/types/scraperRun.js +1 -0
  104. package/dist/mjs/types/statusHistory.d.ts +5 -0
  105. package/dist/mjs/types/statusHistory.js +1 -0
  106. package/dist/mjs/types/supplier.d.ts +28 -0
  107. package/dist/mjs/types/supplier.js +2 -0
  108. package/dist/mjs/units.d.ts +22 -0
  109. package/dist/mjs/units.js +89 -0
  110. package/package.json +9 -4
@@ -0,0 +1,17 @@
1
+ export interface Reading {
2
+ _id: string;
3
+ submittedAt: Date;
4
+ value: number;
5
+ type?: string;
6
+ invoiceStartRead?: number;
7
+ source: string;
8
+ invoiceId?: string;
9
+ rateId?: string;
10
+ contractRateType?: string;
11
+ invoiceBatchId?: string;
12
+ batchId?: string;
13
+ accountId: string;
14
+ entityId: string;
15
+ companyId: string;
16
+ userSub?: string;
17
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,21 @@
1
+ export interface Report {
2
+ _id: string;
3
+ type: 'dynamic' | 'spreadsheet' | 'table' | 'pdf';
4
+ subType?: string;
5
+ fileFormat: 'pdf' | 'xlsx' | 'csv' | 'png' | 'jpg' | 'chart';
6
+ fileName?: string;
7
+ status: 'preparing' | 'ready';
8
+ scheduled?: boolean;
9
+ version?: number;
10
+ metadata?: Record<string, any>;
11
+ data?: Record<string, any>;
12
+ requestedAt: Date;
13
+ availableAt?: Date;
14
+ s3Key: string | null;
15
+ batchId?: string;
16
+ scheduledReportId?: string;
17
+ reportTemplateId?: string;
18
+ entityId: string;
19
+ companyId: string;
20
+ userSub?: string;
21
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,15 @@
1
+ export interface CreateScraperRunParams {
2
+ supplierId: string;
3
+ accountId: string;
4
+ companyId: string;
5
+ }
6
+ export interface ScraperRun {
7
+ _id: string;
8
+ logs?: {}[];
9
+ supplierId: string;
10
+ accountId: string;
11
+ companyId: string;
12
+ invoiceData?: [];
13
+ status: string[];
14
+ error?: string;
15
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ export interface StatusHistory {
2
+ date: Date;
3
+ value: 'active' | 'inactive';
4
+ notes?: string;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,28 @@
1
+ import { Portal } from './portal.js';
2
+ interface PortalSupplierSchema extends Portal {
3
+ concurrent: boolean;
4
+ checkInvoiceFilenames: boolean;
5
+ supplierRefRequired: boolean;
6
+ }
7
+ interface InvoiceOptionsSchema {
8
+ stampPositionX?: number;
9
+ stampPositionY?: number;
10
+ }
11
+ export interface Supplier {
12
+ _id: string;
13
+ name: string;
14
+ logoUrl?: string;
15
+ websiteUrl?: string;
16
+ portalUrl?: string;
17
+ identifier?: string;
18
+ type: 'supplier' | 'distributor';
19
+ region?: string;
20
+ countryCode: string;
21
+ defaultScraper?: string;
22
+ active: boolean;
23
+ transferSupplier: boolean;
24
+ transferSupplierId?: string;
25
+ portal: PortalSupplierSchema;
26
+ invoiceOptions?: InvoiceOptionsSchema;
27
+ }
28
+ export {};
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ ;
@@ -0,0 +1,22 @@
1
+ interface Item {
2
+ units?: string | null;
3
+ unit?: string | null;
4
+ factor?: number | null;
5
+ value: number;
6
+ [key: string]: any;
7
+ }
8
+ export type AccountType = 'electricity' | 'gas' | 'water' | 'waste' | 'solar' | 'heating' | 'flow' | 'cooling' | 'temperature' | 'oil' | 'other';
9
+ export type ETNUnit = 'kwh' | 'kg' | 'm3' | 'lbs' | 'tonnes' | 'wh' | 'mwh' | 'ft3' | 'hcf' | 'm3/h' | 'qty' | 'l' | 'C' | 'mcuf' | 'hcuf' | 'tcuf' | 'ocuf' | 'hm3' | 'tm3' | 'nm3';
10
+ export type BaseUnit = 'kwh' | 'm3' | 'C' | 'kg' | 'm3/h' | 'l';
11
+ export declare const accountTypeMap: {
12
+ [key: string]: BaseUnit;
13
+ };
14
+ export declare const accountTypeUnitMap: {
15
+ [key: string]: ETNUnit[];
16
+ };
17
+ export declare const convertItems: (items: Item[], type: AccountType, defaultUnits: ETNUnit | undefined, accountFactor: number | undefined) => any;
18
+ export declare const checkAccountTypeVsUnits: (type: string, unit: string, additionalLog?: number | "") => {
19
+ type: AccountType;
20
+ unit: ETNUnit;
21
+ };
22
+ export {};
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkAccountTypeVsUnits = exports.convertItems = exports.accountTypeUnitMap = exports.accountTypeMap = void 0;
4
+ exports.accountTypeMap = {
5
+ electricity: 'kwh',
6
+ gas: 'kwh',
7
+ water: 'm3',
8
+ waste: 'kg',
9
+ solar: 'kwh',
10
+ heating: 'kwh',
11
+ flow: 'm3/h',
12
+ cooling: 'kwh',
13
+ temperature: 'C',
14
+ oil: 'l'
15
+ };
16
+ const unitConversionFactors = {
17
+ kwh: {
18
+ kwh: 1,
19
+ mwh: 1000,
20
+ wh: 0.001,
21
+ m3: (39 * 1.02264) / 3.6,
22
+ ft3: (0.0283 * 39 * 1.02264) / 3.6,
23
+ hcf: (2.83 * 39 * 1.02264) / 3.6
24
+ },
25
+ m3: {
26
+ m3: 1,
27
+ l: 0.001
28
+ },
29
+ C: {
30
+ C: 1
31
+ },
32
+ kg: {
33
+ kg: 1,
34
+ lbs: 0.45359237,
35
+ tonnes: 1000
36
+ },
37
+ 'm3/h': {
38
+ 'm3/h': 1
39
+ }
40
+ };
41
+ exports.accountTypeUnitMap = {
42
+ electricity: ['kwh', 'mwh', 'wh'],
43
+ gas: ['kwh', 'm3', 'ft3', 'hcf'],
44
+ water: ['m3', 'l'],
45
+ waste: ['kg', 'lbs', 'tonnes'],
46
+ solar: ['kwh', 'mwh', 'wh'],
47
+ heating: ['kwh', 'mwh', 'wh'],
48
+ flow: ['m3/h'],
49
+ cooling: ['kwh', 'mwh', 'wh'],
50
+ temperature: ['C'],
51
+ oil: ['l']
52
+ };
53
+ // Convert units to base format
54
+ const convertItems = (items, type, defaultUnits, accountFactor) => {
55
+ if (!type)
56
+ throw new Error('Account type is required');
57
+ const baseUnit = exports.accountTypeMap[type];
58
+ if (!baseUnit)
59
+ throw new Error(`Account type ${type} is not supported`);
60
+ const convertedItems = items.map(item => {
61
+ const factor = item.factor || accountFactor || 1;
62
+ const units = item.units || item.unit || defaultUnits || baseUnit;
63
+ const convertedValue = item.value * _getConversionFactor(units, baseUnit) * factor;
64
+ return Object.assign(Object.assign({}, item), { value: convertedValue, units: baseUnit });
65
+ });
66
+ return convertedItems;
67
+ };
68
+ exports.convertItems = convertItems;
69
+ const _getConversionFactor = (fromUnit = 'kwh', toUnit) => {
70
+ const conversionFactors = unitConversionFactors[toUnit];
71
+ if (!conversionFactors) {
72
+ throw new Error(`Conversion factor base unit ${toUnit} is not defined`);
73
+ }
74
+ if (!conversionFactors[fromUnit]) {
75
+ throw new Error(`Conversion factor from unit ${fromUnit} is not defined`);
76
+ }
77
+ return conversionFactors[fromUnit];
78
+ };
79
+ const checkAccountTypeVsUnits = (type, unit, additionalLog = '') => {
80
+ if (!type)
81
+ throw new Error('Account type is required');
82
+ if (!unit)
83
+ throw new Error('Unit is required');
84
+ const parsedType = type.toLowerCase().trim();
85
+ const accountTypeUnits = exports.accountTypeUnitMap[parsedType];
86
+ if (!accountTypeUnits)
87
+ throw new Error(`Account type "${parsedType}" is not supported ${additionalLog}`);
88
+ const parsedUnit = unit.toLowerCase().trim();
89
+ if (!accountTypeUnits.includes(parsedUnit)) {
90
+ throw new Error(`Account type "${parsedType}" does not support unit "${parsedUnit}" ${additionalLog}`);
91
+ }
92
+ return { type: parsedType, unit: parsedUnit };
93
+ };
94
+ exports.checkAccountTypeVsUnits = checkAccountTypeVsUnits;
@@ -0,0 +1,111 @@
1
+ import type { AxiosRequestConfig, AxiosInstance, CreateAxiosDefaults } from 'axios';
2
+ import type { Account, Asset, Automation, Entity, Company, DataIngest, Invoice, Log, Reading, Report, Supplier } from './types/index.js';
3
+ export interface ETNPagedResponse<T = any> {
4
+ data: T[];
5
+ total: number;
6
+ limit: number;
7
+ skip: number;
8
+ }
9
+ export interface ETNReq {
10
+ method: string;
11
+ url: string;
12
+ }
13
+ interface AuthOptions {
14
+ key?: string;
15
+ token?: string;
16
+ }
17
+ declare const _default: (auth: AuthOptions, instanceOptions?: CreateAxiosDefaults) => {
18
+ instance: AxiosInstance;
19
+ getAccount: (id: string, options?: AxiosRequestConfig) => Promise<Account>;
20
+ listAccounts: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Account>>;
21
+ updateAccount: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
22
+ createAccount: (data: any, options?: AxiosRequestConfig) => Promise<any>;
23
+ removeAccount: (id: string, options?: AxiosRequestConfig) => Promise<any>;
24
+ getAccountSchema: (options?: AxiosRequestConfig) => Promise<any>;
25
+ invalidateAccountCache: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
26
+ getAsset: (id: string, options?: AxiosRequestConfig) => Promise<Asset>;
27
+ listAssets: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Asset>>;
28
+ updateAsset: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
29
+ createAsset: (data: any, options?: AxiosRequestConfig) => Promise<any>;
30
+ removeAsset: (id: string, options?: AxiosRequestConfig) => Promise<any>;
31
+ getAssetSchema: (options?: AxiosRequestConfig) => Promise<any>;
32
+ getAssetGroup: (id: string, options?: AxiosRequestConfig) => Promise<any>;
33
+ listAssetGroups: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<any>>;
34
+ updateAssetGroup: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
35
+ createAssetGroup: (data: any, options?: AxiosRequestConfig) => Promise<any>;
36
+ removeAssetGroup: (id: string, options?: AxiosRequestConfig) => Promise<any>;
37
+ getAssetGroupAssets: (id: string, options?: AxiosRequestConfig) => Promise<any>;
38
+ getAssetGroupSchema: (options?: AxiosRequestConfig) => Promise<any>;
39
+ getAutomation: (id: string, options?: AxiosRequestConfig) => Promise<Automation>;
40
+ listAutomations: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Automation>>;
41
+ updateAutomation: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
42
+ createAutomation: (data: any, options?: AxiosRequestConfig) => Promise<any>;
43
+ removeAutomation: (id: string, options?: AxiosRequestConfig) => Promise<any>;
44
+ createAutomationLog: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
45
+ updateAutomationLog: (id: string, subId: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
46
+ removeAutomationLog: (id: string, subId: string, options?: AxiosRequestConfig) => Promise<any>;
47
+ getCompany: (id: string, options?: AxiosRequestConfig) => Promise<Company>;
48
+ getConsumption: (id: string, options?: AxiosRequestConfig) => Promise<any>;
49
+ listConsumptions: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<any>>;
50
+ updateConsumption: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
51
+ createConsumption: (data: any, options?: AxiosRequestConfig) => Promise<any>;
52
+ removeConsumption: (id: string, options?: AxiosRequestConfig) => Promise<any>;
53
+ getConsumptionSchema: (options?: AxiosRequestConfig) => Promise<any>;
54
+ getEmail: (id: string, options?: AxiosRequestConfig) => Promise<any>;
55
+ listEmails: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<any>>;
56
+ updateEmail: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
57
+ createEmail: (data: any, options?: AxiosRequestConfig) => Promise<any>;
58
+ removeEmail: (id: string, options?: AxiosRequestConfig) => Promise<any>;
59
+ getEmissionFactor: (id: string, options?: AxiosRequestConfig) => Promise<any>;
60
+ listEmissionFactors: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<any>>;
61
+ updateEmissionFactor: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
62
+ createEmissionFactor: (data: any, options?: AxiosRequestConfig) => Promise<any>;
63
+ removeEmissionFactor: (id: string, options?: AxiosRequestConfig) => Promise<any>;
64
+ getEntity: (id: string, options?: AxiosRequestConfig) => Promise<Entity>;
65
+ listEntities: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Entity>>;
66
+ updateEntity: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
67
+ createEntity: (data: any, options?: AxiosRequestConfig) => Promise<any>;
68
+ removeEntity: (id: string, options?: AxiosRequestConfig) => Promise<any>;
69
+ getEntitiesSchema: (options?: AxiosRequestConfig) => Promise<any>;
70
+ getLog: (id: string, options?: AxiosRequestConfig) => Promise<Log>;
71
+ listLogs: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Log>>;
72
+ updateLog: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
73
+ createLog: (data: any, options?: AxiosRequestConfig) => Promise<any>;
74
+ removeLog: (id: string, options?: AxiosRequestConfig) => Promise<any>;
75
+ getReading: (id: string, options?: AxiosRequestConfig) => Promise<Reading>;
76
+ listReadings: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Reading>>;
77
+ updateReading: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
78
+ createReading: (data: any, options?: AxiosRequestConfig) => Promise<any>;
79
+ removeReading: (id: string, options?: AxiosRequestConfig) => Promise<any>;
80
+ getReadingSchema: (options?: AxiosRequestConfig) => Promise<any>;
81
+ getReport: (id: string, options?: AxiosRequestConfig) => Promise<Report>;
82
+ listReports: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Report>>;
83
+ updateReport: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
84
+ createReport: (data: any, options?: AxiosRequestConfig) => Promise<any>;
85
+ removeReport: (id: string, options?: AxiosRequestConfig) => Promise<any>;
86
+ sendReport: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
87
+ getReportTemplate: (id: string, options?: AxiosRequestConfig) => Promise<any>;
88
+ listReportTemplates: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<any>>;
89
+ updateReportTemplate: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
90
+ createReportTemplate: (data: any, options?: AxiosRequestConfig) => Promise<any>;
91
+ removeReportTemplate: (id: string, options?: AxiosRequestConfig) => Promise<any>;
92
+ getScheduledReport: (id: string, options?: AxiosRequestConfig) => Promise<any>;
93
+ listScheduledReports: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<any>>;
94
+ updateScheduledReport: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
95
+ createScheduledReport: (data: any, options?: AxiosRequestConfig) => Promise<any>;
96
+ removeScheduledReport: (id: string, options?: AxiosRequestConfig) => Promise<any>;
97
+ sendScheduledReport: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
98
+ getInvoice: (id: string, options?: AxiosRequestConfig) => Promise<Invoice>;
99
+ listInvoices: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Invoice>>;
100
+ updateInvoice: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
101
+ createInvoice: (data: any, options?: AxiosRequestConfig) => Promise<any>;
102
+ removeInvoice: (id: string, options?: AxiosRequestConfig) => Promise<any>;
103
+ getInvoiceSchema: (options?: AxiosRequestConfig) => Promise<any>;
104
+ listSuppliers: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<Supplier>>;
105
+ getSupplierSchema: (options?: AxiosRequestConfig) => Promise<any>;
106
+ getImportTemplate: (id: string, options?: AxiosRequestConfig) => Promise<any>;
107
+ updateDataIngest: (id: string, data: any, options?: AxiosRequestConfig) => Promise<any>;
108
+ createDataIngest: (data: any, options?: AxiosRequestConfig) => Promise<any>;
109
+ listDataIngest: (options?: AxiosRequestConfig) => Promise<ETNPagedResponse<DataIngest>>;
110
+ };
111
+ export default _default;
@@ -0,0 +1,308 @@
1
+ import axios from 'axios';
2
+ import https from 'https';
3
+ import logger from './logger.js';
4
+ const log = logger('etainablApi');
5
+ function _handleResponse(req, res, isPaged = false) {
6
+ if (!res) {
7
+ throw new Error(`No response from API (${req.method} ${req.url})`);
8
+ }
9
+ if (res.status !== 200) {
10
+ throw new Error(`${res.status} ${res.statusText} response from API (${req.method} ${req.url})`);
11
+ }
12
+ if (!res.data) {
13
+ throw new Error(`No data from API (${req.method} ${req.url})`);
14
+ }
15
+ if (isPaged && !res.data.data) {
16
+ throw new Error(`No data from API (${req.method} ${req.url})`);
17
+ }
18
+ return res;
19
+ }
20
+ const factory = {
21
+ getWithId: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
22
+ const req = {
23
+ method: 'GET',
24
+ url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
25
+ };
26
+ log.info(`API Request: ${req.method} ${process.env.ETAsINABL_API_URL}/${req.url}`);
27
+ let res;
28
+ try {
29
+ res = await etainablApi.get(req.url, options);
30
+ }
31
+ catch (e) {
32
+ if (e.response?.data)
33
+ throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
34
+ throw e;
35
+ }
36
+ console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
37
+ _handleResponse(req, res);
38
+ return res.data;
39
+ },
40
+ get: (etainablApi, endpoint, postEndpoint) => async (options = {}) => {
41
+ const req = {
42
+ method: 'GET',
43
+ url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ''}`
44
+ };
45
+ log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
46
+ let res;
47
+ try {
48
+ res = await etainablApi.get(req.url, options);
49
+ }
50
+ catch (e) {
51
+ if (e.response?.data)
52
+ throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
53
+ throw e;
54
+ }
55
+ console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
56
+ _handleResponse(req, res);
57
+ return res.data;
58
+ },
59
+ list: (etainablApi, endpoint, postEndpoint) => async (options = {}) => {
60
+ const req = {
61
+ method: 'GET',
62
+ url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ''}`
63
+ };
64
+ log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
65
+ let res;
66
+ try {
67
+ res = await etainablApi.get(req.url, options);
68
+ }
69
+ catch (e) {
70
+ if (e.response?.data)
71
+ throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
72
+ throw e;
73
+ }
74
+ console.log(`API Response: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
75
+ _handleResponse(req, res, true);
76
+ return res.data;
77
+ },
78
+ update: (etainablApi, endpoint, postEndpoint) => async (id, data, options = {}) => {
79
+ const req = {
80
+ method: 'PATCH',
81
+ url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
82
+ };
83
+ log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
84
+ let res;
85
+ try {
86
+ res = await etainablApi.patch(req.url, data, options);
87
+ }
88
+ catch (e) {
89
+ if (e.response?.data)
90
+ throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
91
+ throw e;
92
+ }
93
+ _handleResponse(req, res);
94
+ return res.data;
95
+ },
96
+ create: (etainablApi, endpoint, postEndpoint) => async (data, options = {}) => {
97
+ const req = {
98
+ method: 'POST',
99
+ url: `${endpoint}${postEndpoint ? `/${postEndpoint}` : ''}`
100
+ };
101
+ log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
102
+ let res;
103
+ try {
104
+ res = await etainablApi.post(req.url, data, options);
105
+ }
106
+ catch (e) {
107
+ if (e.response?.data)
108
+ throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
109
+ throw e;
110
+ }
111
+ _handleResponse(req, res);
112
+ return res.data;
113
+ },
114
+ remove: (etainablApi, endpoint, postEndpoint) => async (id, options = {}) => {
115
+ const req = {
116
+ method: 'DELETE',
117
+ url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`
118
+ };
119
+ let res;
120
+ log.info(`API Request: ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
121
+ try {
122
+ res = await etainablApi.delete(req.url, options);
123
+ }
124
+ catch (e) {
125
+ if (e.response?.data)
126
+ throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
127
+ throw e;
128
+ }
129
+ _handleResponse(req, res);
130
+ return res.data;
131
+ },
132
+ customWithId: (etainablApi, method, endpoint, postEndpoint) => async (id, data, options = {}) => {
133
+ const req = {
134
+ method: method,
135
+ url: `${endpoint}/${id}${postEndpoint ? `/${postEndpoint}` : ''}`,
136
+ data,
137
+ ...options
138
+ };
139
+ log.info(`API Request (Custom): ${req.method} ${process.env.ETAINABL_API_URL}/${req.url}`);
140
+ let res;
141
+ try {
142
+ res = await etainablApi.request(req);
143
+ }
144
+ catch (e) {
145
+ if (e.response?.data)
146
+ throw new Error(`API error response: ${JSON.stringify(e.response.data)}`);
147
+ throw e;
148
+ }
149
+ _handleResponse(req, res);
150
+ return res.data;
151
+ }
152
+ };
153
+ // ETN Sub Endpoints
154
+ // e.g. /assets/:id/documents/:documentId
155
+ const subFactory = {
156
+ // e.g. POST /assets/:id/documents
157
+ create: (etainablApi, endpoint, subEndpoint) => async (id, data, options = {}) => {
158
+ const subUrl = `${id}/${subEndpoint}`;
159
+ return factory.create(etainablApi, endpoint, subUrl)(data, options);
160
+ },
161
+ // e.g. PATCH /assets/:id/documents/:documentId
162
+ update: (etainablApi, endpoint, subEndpoint) => async (id, subId, data, options = {}) => {
163
+ const subUrl = `${subEndpoint}/${subId}`;
164
+ return factory.update(etainablApi, endpoint, subUrl)(id, data, options);
165
+ },
166
+ // e.g. DELETE /assets/:id/documents/:documentId
167
+ remove: (etainablApi, endpoint, subEndpoint) => async (id, subId, options = {}) => {
168
+ const subUrl = `${subEndpoint}/${subId}`;
169
+ return factory.remove(etainablApi, endpoint, subUrl)(id, options);
170
+ }
171
+ };
172
+ export default (auth, instanceOptions = {}) => {
173
+ try {
174
+ const headers = {};
175
+ if (auth.key) {
176
+ headers['x-key'] = auth.key;
177
+ }
178
+ else if (auth.token) {
179
+ headers['Authorization'] = auth.token;
180
+ }
181
+ else {
182
+ headers['x-key'] = process.env.ETAINABL_API_KEY;
183
+ }
184
+ const etainablApi = axios.create({
185
+ baseURL: process.env.ETAINABL_API_URL,
186
+ timeout: 300000,
187
+ httpsAgent: new https.Agent({ keepAlive: true }),
188
+ headers,
189
+ ...instanceOptions
190
+ });
191
+ return {
192
+ instance: etainablApi,
193
+ // accounts
194
+ getAccount: factory.getWithId(etainablApi, 'accounts'),
195
+ listAccounts: factory.list(etainablApi, 'accounts'),
196
+ updateAccount: factory.update(etainablApi, 'accounts'),
197
+ createAccount: factory.create(etainablApi, 'accounts'),
198
+ removeAccount: factory.remove(etainablApi, 'accounts'),
199
+ getAccountSchema: factory.get(etainablApi, 'accounts', 'schema'),
200
+ invalidateAccountCache: factory.customWithId(etainablApi, 'put', 'accounts', 'invalidate-cache'),
201
+ // assets
202
+ getAsset: factory.getWithId(etainablApi, 'assets'),
203
+ listAssets: factory.list(etainablApi, 'assets'),
204
+ updateAsset: factory.update(etainablApi, 'assets'),
205
+ createAsset: factory.create(etainablApi, 'assets'),
206
+ removeAsset: factory.remove(etainablApi, 'assets'),
207
+ getAssetSchema: factory.get(etainablApi, 'assets', 'schema'),
208
+ // assetGroups
209
+ getAssetGroup: factory.getWithId(etainablApi, 'asset-groups'),
210
+ listAssetGroups: factory.list(etainablApi, 'asset-groups'),
211
+ updateAssetGroup: factory.update(etainablApi, 'asset-groups'),
212
+ createAssetGroup: factory.create(etainablApi, 'asset-groups'),
213
+ removeAssetGroup: factory.remove(etainablApi, 'asset-groups'),
214
+ getAssetGroupAssets: factory.getWithId(etainablApi, 'asset-groups', 'assets'),
215
+ getAssetGroupSchema: factory.get(etainablApi, 'asset-groups', 'schema'),
216
+ // automation
217
+ getAutomation: factory.getWithId(etainablApi, 'automation'),
218
+ listAutomations: factory.list(etainablApi, 'automation'),
219
+ updateAutomation: factory.update(etainablApi, 'automation'),
220
+ createAutomation: factory.create(etainablApi, 'automation'),
221
+ removeAutomation: factory.remove(etainablApi, 'automation'),
222
+ createAutomationLog: subFactory.create(etainablApi, 'automation', 'logs'),
223
+ updateAutomationLog: subFactory.update(etainablApi, 'automation', 'logs'),
224
+ removeAutomationLog: subFactory.remove(etainablApi, 'automation', 'logs'),
225
+ // company
226
+ getCompany: factory.getWithId(etainablApi, 'companies'),
227
+ // consumption
228
+ getConsumption: factory.getWithId(etainablApi, 'consumptions'),
229
+ listConsumptions: factory.list(etainablApi, 'consumptions'),
230
+ updateConsumption: factory.update(etainablApi, 'consumptions'),
231
+ createConsumption: factory.create(etainablApi, 'consumptions'),
232
+ removeConsumption: factory.remove(etainablApi, 'consumptions'),
233
+ getConsumptionSchema: factory.get(etainablApi, 'consumptions', 'schema'),
234
+ // emails
235
+ getEmail: factory.getWithId(etainablApi, 'emails'),
236
+ listEmails: factory.list(etainablApi, 'emails'),
237
+ updateEmail: factory.update(etainablApi, 'emails'),
238
+ createEmail: factory.create(etainablApi, 'emails'),
239
+ removeEmail: factory.remove(etainablApi, 'emails'),
240
+ // emission factors
241
+ getEmissionFactor: factory.getWithId(etainablApi, 'emission-factors'),
242
+ listEmissionFactors: factory.list(etainablApi, 'emission-factors'),
243
+ updateEmissionFactor: factory.update(etainablApi, 'emission-factors'),
244
+ createEmissionFactor: factory.create(etainablApi, 'emission-factors'),
245
+ removeEmissionFactor: factory.remove(etainablApi, 'emission-factors'),
246
+ // entity
247
+ getEntity: factory.getWithId(etainablApi, 'entities'),
248
+ listEntities: factory.list(etainablApi, 'entities'),
249
+ updateEntity: factory.update(etainablApi, 'entities'),
250
+ createEntity: factory.create(etainablApi, 'entities'),
251
+ removeEntity: factory.remove(etainablApi, 'entities'),
252
+ getEntitiesSchema: factory.get(etainablApi, 'entities', 'schema'),
253
+ // logs
254
+ getLog: factory.getWithId(etainablApi, 'logs'),
255
+ listLogs: factory.list(etainablApi, 'logs'),
256
+ updateLog: factory.update(etainablApi, 'logs'),
257
+ createLog: factory.create(etainablApi, 'logs'),
258
+ removeLog: factory.remove(etainablApi, 'logs'),
259
+ // readings
260
+ getReading: factory.getWithId(etainablApi, 'readings'),
261
+ listReadings: factory.list(etainablApi, 'readings'),
262
+ updateReading: factory.update(etainablApi, 'readings'),
263
+ createReading: factory.create(etainablApi, 'readings'),
264
+ removeReading: factory.remove(etainablApi, 'readings'),
265
+ getReadingSchema: factory.get(etainablApi, 'readings', 'schema'),
266
+ // reports
267
+ getReport: factory.getWithId(etainablApi, 'reports'),
268
+ listReports: factory.list(etainablApi, 'reports'),
269
+ updateReport: factory.update(etainablApi, 'reports'),
270
+ createReport: factory.create(etainablApi, 'reports'),
271
+ removeReport: factory.remove(etainablApi, 'reports'),
272
+ sendReport: factory.customWithId(etainablApi, 'post', 'reports', 'send'),
273
+ // report templates
274
+ getReportTemplate: factory.getWithId(etainablApi, 'report-templates'),
275
+ listReportTemplates: factory.list(etainablApi, 'report-templates'),
276
+ updateReportTemplate: factory.update(etainablApi, 'report-templates'),
277
+ createReportTemplate: factory.create(etainablApi, 'report-templates'),
278
+ removeReportTemplate: factory.remove(etainablApi, 'report-templates'),
279
+ // scheduled reports
280
+ getScheduledReport: factory.getWithId(etainablApi, 'scheduled-reports'),
281
+ listScheduledReports: factory.list(etainablApi, 'scheduled-reports'),
282
+ updateScheduledReport: factory.update(etainablApi, 'scheduled-reports'),
283
+ createScheduledReport: factory.create(etainablApi, 'scheduled-reports'),
284
+ removeScheduledReport: factory.remove(etainablApi, 'scheduled-reports'),
285
+ sendScheduledReport: factory.customWithId(etainablApi, 'post', 'scheduled-reports', 'send'),
286
+ // invoices
287
+ getInvoice: factory.getWithId(etainablApi, 'invoices'),
288
+ listInvoices: factory.list(etainablApi, 'invoices'),
289
+ updateInvoice: factory.update(etainablApi, 'invoices'),
290
+ createInvoice: factory.create(etainablApi, 'invoices'),
291
+ removeInvoice: factory.remove(etainablApi, 'invoices'),
292
+ getInvoiceSchema: factory.get(etainablApi, 'invoices', 'schema'),
293
+ //suppliers
294
+ listSuppliers: factory.list(etainablApi, 'suppliers'),
295
+ getSupplierSchema: factory.get(etainablApi, 'suppliers', 'schema'),
296
+ // import templates
297
+ getImportTemplate: factory.getWithId(etainablApi, 'import-templates'),
298
+ //data imports
299
+ updateDataIngest: factory.update(etainablApi, 'data-ingests'),
300
+ createDataIngest: factory.create(etainablApi, 'data-ingests'),
301
+ listDataIngest: factory.list(etainablApi, 'data-ingests'),
302
+ };
303
+ }
304
+ catch (e) {
305
+ log.error(e);
306
+ throw e;
307
+ }
308
+ };
@@ -0,0 +1,14 @@
1
+ import moment from 'moment';
2
+ interface ConsumptionData {
3
+ date: Date;
4
+ consumption: number;
5
+ }
6
+ export declare const dayNightConsumption: (data: ConsumptionData[]) => {
7
+ dayConsumption: number;
8
+ nightConsumption: number;
9
+ consumption: number;
10
+ };
11
+ export declare const calcMaxConsumptionValue: (data: ConsumptionData[]) => number;
12
+ export declare const calcMaxDemand: (data: ConsumptionData[]) => number;
13
+ export declare const calcPeakLoad: (consumption: number, maxDemand: number, startDate: string | moment.Moment, endDate: string | moment.Moment) => number;
14
+ export {};