-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlnrTools.js
388 lines (342 loc) · 11.4 KB
/
lnrTools.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
export class SmartConnectClient {
constructor({alias, rootOrgId, username, password} = {}) {
this.defaults = {};
if (alias !== undefined && rootOrgId !== undefined) {
this.defaults.companyid = rootOrgId;
this.defaults.alias = alias;
this.url = `https://${alias}.grants.tn.gov`;
console.log(this.url);
} else {
this.url = '';
}
if (username !== undefined && password !== undefined) {
this.defaults.username = username;
this.defaults.password = password;
}
}
#ssEndpoints = {
report: '/API/2/report/',
company: '/API/2/company/',
user: '/API/2/user/',
systemVariables: '/API/2/sysvar/',
level1: '/API/2/levelone/',
level2: '/API/2/leveltwo/',
level3: '/API/2/levelthree/',
transaction: '/API/2/transactions/',
functions: '/API/2/functions/',
};
async #sendData(endpoint, token, parameters) {
const merged = { ...this.defaults, ...parameters };
merged.apitoken = token;
if (merged.criteria !== undefined) merged.criteria = JSON.stringify(merged.criteria);
if (merged.sortby !== undefined) merged.sortby = JSON.stringify(merged.sortby);
if (merged.jsonrset !== undefined) merged.jsonrset = JSON.stringify(merged.jsonrset);
endpoint = `${this.url}${endpoint}`;
console.log(`Endpoint: ${endpoint}`);
console.log(`Request: ${JSON.stringify(merged)}`);
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(merged),
});
const responseJson = await response.json();
console.log(`Response: ${JSON.stringify(responseJson)}`);
return responseJson;
}
async #getLevelRecord(level, token, recordid) {
const params = {
recordid,
};
return await this.#sendData(level, token, params);
}
async getL1Record(token, recordid) {
return await this.#getLevelRecord(this.#ssEndpoints.level1, token, recordid);
}
async getL2Record(token, recordid) {
return await this.#getLevelRecord(this.#ssEndpoints.level2, token, recordid);
}
async getL3Record(token, recordid) {
return await this.#getLevelRecord(this.#ssEndpoints.level3, token, recordid);
}
async #listRecords(endpoint, token, parameters) {
return await this.#sendData(endpoint, token, parameters);
}
async listL1Records(token, parameters = {}) {
return await this.#listRecords(this.#ssEndpoints.level1, token, parameters);
}
async listL2Records(token, parameters = {}) {
return await this.#listRecords(this.#ssEndpoints.level2, token, parameters);
}
async listL3Records(token, parameters = {}) {
return await this.#listRecords(this.#ssEndpoints.level3, token, parameters);
}
async #updateRecord(endpoint, token, fieldData) {
const merged = {
jsonrset: [
{
...fieldData,
},
],
};
return await this.#sendData(endpoint, token, merged);
}
async updateL1Record(token, fieldData) {
return await this.#updateRecord(this.#ssEndpoints.level1, token, fieldData);
}
async updateL2Record(token, fieldData) {
return await this.#updateRecord(this.#ssEndpoints.level2, token, fieldData);
}
async updateL3Record(token, fieldData) {
return await this.#updateRecord(this.#ssEndpoints.level3, token, fieldData);
}
async createProviderConsumer(token, rolename, consumerid, providerid) {
const newRecord = {
jsonrset: [
{
recordid: '0',
rolename,
consumerid,
providerid,
},
],
};
return await this.#sendData(this.#ssEndpoints.level1, token, newRecord);
}
async #listConsumersToLevelProvider(level, token, providerid, criteria) {
const parameters = {
criteria: [
{
andor: '(',
field: 'providerid',
operator: '=',
value: providerid,
},
...criteria,
],
};
return await this.#sendData(level, token, parameters);
}
async listConsumersToL1Provider(token, providerid, criteria = []) {
return await this.#listConsumersToLevelProvider(this.#ssEndpoints.level1, token, providerid, criteria);
}
async listConsumersToL2Provider(token, providerid, criteria = []) {
return await this.#listConsumersToLevelProvider(this.#ssEndpoints.level2, token, providerid, criteria);
}
async listConsumersToL3Provider(token, providerid, criteria = []) {
return await this.#listConsumersToLevelProvider(this.#ssEndpoints.level3, token, providerid, criteria);
}
async #listLevelProvidersToConsumer(level, token, consumerid, criteria) {
const parameters = {
criteria: [
{
andor: '(',
field: 'consumerid',
operator: '=',
value: consumerid,
},
...criteria,
],
};
return await this.#sendData(level, token, parameters);
}
async listL1ProvidersToConsumer(token, consumerid, criteria = []) {
return await this.#listLevelProvidersToConsumer(this.#ssEndpoints.level1, token, consumerid, criteria);
}
async listL2ProvidersToConsumer(token, consumerid, criteria = []) {
return await this.#listLevelProvidersToConsumer(this.#ssEndpoints.level2, token, consumerid, criteria);
}
async listL3ProvidersToConsumer(token, consumerid, criteria = []) {
return await this.#listLevelProvidersToConsumer(this.#ssEndpoints.level3, token, consumerid, criteria);
}
}
export class Formatter {
static numToWords(numericalRepresentation) {
numericalRepresentation = numericalRepresentation.trim().replace(/\$|,/g, '');
numericalRepresentation = Number.parseFloat(numericalRepresentation);
if (Number.isNaN(numericalRepresentation)) {
throw new Error("Input must be a valid number.");
}
if (numericalRepresentation < 0) {
throw new Error("Cannot convert negative value.");
}
let dollarPart = 0;
let centPart = 0;
if (Number.isInteger(numericalRepresentation)) {
dollarPart = numericalRepresentation;
} else {
const numberAsString = numericalRepresentation.toString();
let [dollarString, centString] = numberAsString.split(".");
dollarPart = Number.parseInt(dollarString);
centString =
centString.length > 1
? `${centString[0]}${centString[1]}`
: `${centString}0`;
centPart = Number.parseInt(centString);
}
return `${Formatter.#convertIntegerValue(dollarPart)} Dollars and ${Formatter.#convertIntegerValue(centPart)} Cents`;
}
static monthsAndDays(beginDate, endDate) {
function calcMonthsAndDays(begin, end) {
begin = new Date(begin);
end = new Date(end);
const years = end.getFullYear() - begin.getFullYear();
const months = end.getMonth() - begin.getMonth();
let days = end.getDate() - begin.getDate();
let totalMonths = years * 12 + months;
if (days < 0) {
days = begin.getDate() + days;
totalMonths--;
}
let text = `${Formatter.#convertIntegerValue(totalMonths)} Months`;
if (days > 0) text = `${text} and ${Formatter.#convertIntegerValue(days)} Days`;
return {
months: totalMonths,
days,
text
}
}
const begin = new Date(`${beginDate}T12:00:00`);
const end = new Date(`${endDate}T12:00:00`);
const daysPlusOne = calcMonthsAndDays(begin, new Date(end).setDate(end.getDate() + 1));
if (daysPlusOne.days === 0) return daysPlusOne;
return calcMonthsAndDays(begin, end);
}
static #convertIntegerValue(numericalRepresentation) {
if (numericalRepresentation === 0) {
return "Zero";
} else if (numericalRepresentation < 10) {
return Formatter.#convertIntegerValueLessThan10(numericalRepresentation);
} else if (numericalRepresentation < 20) {
return Formatter.#convertIntegerValueLessThan20(numericalRepresentation);
} else if (numericalRepresentation < 100) {
return Formatter.#convertIntegerValueGreaterThan20AndLessThan100(
numericalRepresentation
);
} else {
return Formatter.#convertValue100AndOver(numericalRepresentation);
}
}
static #convertIntegerValueLessThan10(digit) {
switch (digit) {
case 1:
return "One";
case 2:
return "Two";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
}
}
static #convertIntegerValueLessThan20(numericalRepresentation) {
switch (numericalRepresentation) {
case 10:
return "Ten";
case 11:
return "Eleven";
case 12:
return "Twelve";
case 13:
return "Thirteen";
case 14:
return "Fourteen";
case 15:
return "Fifteen";
case 16:
return "Sixteen";
case 17:
return "Seventeen";
case 18:
return "Eighteen";
case 19:
return "Nineteen";
}
}
static #convertIntegerValueGreaterThan20AndLessThan100(
numericalRepresentation
) {
let valueInWords = "";
if (numericalRepresentation < 30) {
valueInWords = "Twenty";
} else if (numericalRepresentation < 40) {
valueInWords = "Thirty";
} else if (numericalRepresentation < 50) {
valueInWords = "Fourty";
} else if (numericalRepresentation < 60) {
valueInWords = "Fifty";
} else if (numericalRepresentation < 70) {
valueInWords = "Sixty";
} else if (numericalRepresentation < 80) {
valueInWords = "Seventy";
} else if (numericalRepresentation < 90) {
valueInWords = "Eighty";
} else {
valueInWords = "Ninety";
}
const remainder = numericalRepresentation % 10;
if (remainder > 0) {
valueInWords += `-${Formatter.#convertIntegerValueLessThan10(remainder)}`;
}
return valueInWords;
}
static #convertValue100AndOver(numericalRepresentation) {
const HUNDRED = "Hundred";
const THOUSAND = "Thousand";
const MILLION = "Million";
const BILLION = "Billion";
const TRILLION = "Trillion";
const QUADRILLION = "Quadrillion";
const ONE_HUNDRED = 100;
const ONE_THOUSAND = 1000;
const ONE_MILLION = 1000000;
const ONE_BILLION = 1000000000;
const ONE_TRILLION = 1000000000000;
const ONE_QUADRILLION = 1000000000000000;
const suffix = {
[HUNDRED]: ONE_HUNDRED,
[THOUSAND]: ONE_THOUSAND,
[MILLION]: ONE_MILLION,
[BILLION]: ONE_BILLION,
[TRILLION]: ONE_TRILLION,
[QUADRILLION]: ONE_QUADRILLION,
};
let scale;
if (numericalRepresentation < ONE_THOUSAND) {
scale = HUNDRED;
} else if (numericalRepresentation < ONE_MILLION) {
scale = THOUSAND;
} else if (numericalRepresentation < ONE_BILLION) {
scale = MILLION;
} else if (numericalRepresentation < ONE_TRILLION) {
scale = BILLION;
} else if (numericalRepresentation < ONE_QUADRILLION) {
scale = TRILLION;
} else {
scale = QUADRILLION;
}
let valueInWords = `${Formatter.#convertIntegerValue(
Math.floor(numericalRepresentation / suffix[scale])
)} ${scale}`;
const remainder = numericalRepresentation % suffix[scale];
if (remainder > 0) {
if (remainder < 100) {
valueInWords += ` and ${Formatter.#convertIntegerValue(remainder)}`;
} else {
valueInWords += `, ${Formatter.#convertIntegerValue(remainder)}`;
}
}
return valueInWords;
}
}