-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (137 loc) · 6.27 KB
/
index.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
import { minutesToSeconds, secondsToMillis } from '@cityssm/to-millis';
import NodeCache from 'node-cache';
import _extendGpInvoice from './diamond/extendGpInvoice.js';
import _getCashReceiptByDocumentNumber from './diamond/getCashReceiptByDocumentNumber.js';
import _getAccountByAccountIndex from './gp/getAccountByAccountIndex.js';
import _getCustomerByCustomerNumber from './gp/getCustomerByCustomerNumber.js';
import _getInvoiceByInvoiceNumber from './gp/getInvoiceByInvoiceNumber.js';
import _getInvoiceDocumentTypes from './gp/getInvoiceDocumentTypes.js';
import _getItemByItemNumber from './gp/getItemByItemNumber.js';
import _getItemsByLocationCodes from './gp/getItemsByLocationCodes.js';
import _getVendorByVendorId from './gp/getVendorByVendorId.js';
const defaultOptions = {
cacheTTL: minutesToSeconds(3),
documentCacheTTL: minutesToSeconds(1)
};
function getInvoiceCacheKey(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName) {
return `${(invoiceDocumentTypeOrAbbreviationOrName ?? '').toString()}::${invoiceNumber}`;
}
export class DynamicsGP {
#mssqlConfig;
#options;
#accountCache;
#customerCache;
#invoiceCache;
#itemCache;
#vendorCache;
#invoiceDocumentTypesCache = [];
#invoiceDocumentTypesCacheExpiryMillis = 0;
#diamondCashReceiptCache;
#diamondInvoiceCache;
constructor(mssqlConfig, options) {
this.#mssqlConfig = mssqlConfig;
this.#options = { ...defaultOptions, ...options };
this.#accountCache = new NodeCache({ stdTTL: this.#options.cacheTTL });
this.#customerCache = new NodeCache({ stdTTL: this.#options.cacheTTL });
this.#itemCache = new NodeCache({ stdTTL: this.#options.cacheTTL });
this.#vendorCache = new NodeCache({ stdTTL: this.#options.cacheTTL });
this.#invoiceCache = new NodeCache({
stdTTL: this.#options.documentCacheTTL
});
this.#diamondCashReceiptCache = new NodeCache({
stdTTL: this.#options.documentCacheTTL
});
this.#diamondInvoiceCache = new NodeCache({
stdTTL: this.#options.documentCacheTTL
});
}
clearCaches() {
this.#accountCache.flushAll();
this.#customerCache.flushAll();
this.#invoiceCache.flushAll();
this.#itemCache.flushAll();
this.#vendorCache.flushAll();
this.#vendorCache.flushAll();
this.#invoiceDocumentTypesCache = [];
this.#invoiceDocumentTypesCacheExpiryMillis = 0;
this.#diamondCashReceiptCache.flushAll();
this.#diamondInvoiceCache.flushAll();
}
async getAccountByAccountIndex(accountIndex) {
let account = this.#accountCache.get(accountIndex) ?? undefined;
if (account === undefined) {
account = await _getAccountByAccountIndex(this.#mssqlConfig, accountIndex);
this.#accountCache.set(accountIndex, account);
}
return account;
}
async getCustomerByCustomerNumber(customerNumber) {
let customer = this.#customerCache.get(customerNumber) ?? undefined;
if (customer === undefined) {
customer = await _getCustomerByCustomerNumber(this.#mssqlConfig, customerNumber);
this.#customerCache.set(customerNumber, customer);
}
return customer;
}
async #getInvoiceByInvoiceNumber(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName, skipCache = false) {
const cacheKey = getInvoiceCacheKey(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName);
let invoice = this.#invoiceCache.get(cacheKey) ?? undefined;
if (invoice === undefined || skipCache) {
invoice = await _getInvoiceByInvoiceNumber(this.#mssqlConfig, invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName);
if (!skipCache) {
this.#invoiceCache.set(cacheKey, invoice);
}
}
return invoice;
}
async getInvoiceByInvoiceNumber(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName) {
return await this.#getInvoiceByInvoiceNumber(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName, false);
}
async getInvoiceDocumentTypes() {
if (this.#invoiceDocumentTypesCacheExpiryMillis < Date.now()) {
this.#invoiceDocumentTypesCache = await _getInvoiceDocumentTypes(this.#mssqlConfig);
this.#invoiceDocumentTypesCacheExpiryMillis =
Date.now() + secondsToMillis(this.#options.cacheTTL);
}
return this.#invoiceDocumentTypesCache;
}
async getItemByItemNumber(itemNumber) {
let item = this.#itemCache.get(itemNumber) ?? undefined;
if (item === undefined) {
item = await _getItemByItemNumber(this.#mssqlConfig, itemNumber);
this.#itemCache.set(itemNumber, item);
}
return item;
}
async getItemsByLocationCodes(locationCodes = ['']) {
return await _getItemsByLocationCodes(this.#mssqlConfig, locationCodes);
}
async getVendorByVendorId(vendorId) {
let vendor = this.#vendorCache.get(vendorId) ?? undefined;
if (vendor === undefined) {
vendor = await _getVendorByVendorId(this.#mssqlConfig, vendorId);
this.#vendorCache.set(vendorId, vendor);
}
return vendor;
}
async getDiamondCashReceiptByDocumentNumber(documentNumber) {
let receipt = this.#diamondCashReceiptCache.get(documentNumber);
if (receipt === undefined) {
receipt = await _getCashReceiptByDocumentNumber(this.#mssqlConfig, documentNumber);
this.#diamondCashReceiptCache.set(documentNumber, receipt);
}
return receipt;
}
async getDiamondExtendedInvoiceByInvoiceNumber(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName) {
const cacheKey = getInvoiceCacheKey(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName);
let diamondInvoice = this.#diamondInvoiceCache.get(cacheKey) ?? undefined;
if (diamondInvoice === undefined) {
const gpInvoice = await this.#getInvoiceByInvoiceNumber(invoiceNumber, invoiceDocumentTypeOrAbbreviationOrName);
if (gpInvoice !== undefined) {
diamondInvoice = await _extendGpInvoice(this.#mssqlConfig, gpInvoice);
this.#diamondInvoiceCache.set(cacheKey, diamondInvoice);
}
}
return diamondInvoice;
}
}