priceplan.js
5.59 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('komodo-sdk/logger');
const dbKomodo = require('../../db-komodo');
const DEBUG = process.env.NODE_ENV !== 'production';
// logger.info(`PRODUCTION MODE: ${!DEBUG}`);
const router = express.Router();
module.exports = router;
const cache = {};
function lookupCorrectPriceplan(storeId, originStoreId, recursiveLevel = 0) {
return new Promise((resolve) => {
if (DEBUG) {
logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Looking up', {
storeId,
originStoreId,
recursiveLevel,
});
}
if (cache[`STORE_ID_${storeId}`]) {
logger.verbose('PRICPLAN.lookupCorrectPriceplan: Got from cache', {
storeId,
priceplan: cache[`STORE_ID_${storeId}`].priceplan,
});
resolve({
priceplan: cache[`STORE_ID_${storeId}`].priceplan,
recursiveLevel: null,
});
return;
}
const query = `
-- KOMODO MIGRATION EVO - PRICEPLAN - pageShouldBe
SELECT SQL_CACHE parent_id, priceplan_name
FROM stores USE INDEX (PRIMARY)
WHERE id = ?
LIMIT 1
`.trim();
const values = [storeId];
dbKomodo.query(query, values, async (err, result) => {
if (err) {
const fullQuery = await dbKomodo.format(query, values);
logger.warn(`PRICEPLAN.lookupCorrectPriceplan: DB ERROR. ${err.toString()}`, {
storeId, originStoreId, recursiveLevel, fullQuery,
});
resolve({
priceplan: null,
recursiveLevel,
});
return;
}
const data = result && result[0];
if (!data) {
logger.warn('PRICEPLAN.lookupCorrectPriceplan: STORE not found', { storeId, originStoreId, recursiveLevel });
resolve({
priceplan: null,
recursiveLevel,
});
return;
}
if (data.priceplan_name.toUpperCase() === 'MARKUP') {
if (!data.parent_id) {
logger.warn('PRICEPLAN.lookupCorrectPriceplan: Suitable priceplan not found', {
storeId,
originStoreId,
recursiveLevel,
});
resolve({
priceplan: null,
recursiveLevel,
});
return;
}
if (DEBUG) {
logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Priceplan is MARKUP, going to look up on parent', {
storeId,
originStoreId,
recursiveLevel,
parentId: data.parent_id,
});
}
const dataFromParent = await lookupCorrectPriceplan(
data.parent_id, originStoreId, recursiveLevel + 1,
);
resolve({
priceplan: dataFromParent.priceplan,
recursiveLevel: dataFromParent.recursiveLevel,
});
return;
}
cache[`STORE_ID_${storeId}`] = {
priceplan: data.priceplan_name,
recursiveLevel,
};
if (DEBUG) {
logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Got suitable result', {
storeId,
originStoreId,
priceplanName: data.priceplan_name,
recursiveLevel,
});
}
resolve({
priceplan: data.priceplan_name,
recursiveLevel,
});
});
});
}
async function pageShouldBe(req, res) {
if (!req.body) req.body = {};
const startTime = new Date();
const storeId = req.body.store_id || req.query.store_id;
if (!storeId) {
res.json({
error: true,
error_message: 'Undefined parameter store_id',
});
return;
}
logger.verbose('PRICEPLAN.pageShouldBe: Got a request', {
method: req.method,
storeId,
});
const data = await lookupCorrectPriceplan(storeId, storeId, 0);
const deltaTime = Number(new Date() - startTime);
logger.info(`PRICEPLAN.pageShouldBe: Lookup finished ${deltaTime} ms`, {
storeId,
newPriceplan: data && data.priceplanName,
dbPoolStatus: {
// eslint-disable-next-line no-underscore-dangle,max-len
all: (dbKomodo.pool && dbKomodo.pool._allConnections && dbKomodo.pool._allConnections.length) || 0,
// eslint-disable-next-line no-underscore-dangle,max-len
free: (dbKomodo.pool && dbKomodo.pool._freeConnections && dbKomodo.pool._freeConnections.length) || 0,
// eslint-disable-next-line no-underscore-dangle,max-len
queue: (dbKomodo.pool && dbKomodo.pool._connectionQueue && dbKomodo.pool._connectionQueue.length) || 0,
},
});
res.json({
error: false,
error_message: null,
store_id: storeId,
recursive_level: (data && data.recursiveLevel) || null,
priceplan_should_be: (data && data.priceplan) || null,
raw: data,
});
}
router.all('/should-be', bodyParser.urlencoded({ extended: false }), pageShouldBe);