Commit 05dc9e8bca60664a07de565e326032c51c22ec76
1 parent
69b18c7f0e
Exists in
master
More debug
Showing 1 changed file with 17 additions and 12 deletions Inline Diff
lib/webservice/router/priceplan.js
1 | const express = require('express'); | 1 | const express = require('express'); |
2 | const bodyParser = require('body-parser'); | 2 | const bodyParser = require('body-parser'); |
3 | const logger = require('komodo-sdk/logger'); | 3 | const logger = require('komodo-sdk/logger'); |
4 | const dbKomodo = require('../../db-komodo'); | 4 | const dbKomodo = require('../../db-komodo'); |
5 | 5 | ||
6 | const DEBUG = process.env.NODE_ENV !== 'production'; | 6 | const DEBUG = process.env.NODE_ENV !== 'production'; |
7 | logger.info(`PRODUCTION MODE: ${!DEBUG}`); | 7 | logger.info(`PRODUCTION MODE: ${!DEBUG}`); |
8 | 8 | ||
9 | const router = express.Router(); | 9 | const router = express.Router(); |
10 | module.exports = router; | 10 | module.exports = router; |
11 | 11 | ||
12 | const cache = {}; | 12 | const cache = {}; |
13 | 13 | ||
14 | function lookupCorrectPriceplan(storeId, originStoreId, recursiveLevel = 0) { | 14 | function lookupCorrectPriceplan(storeId, originStoreId, recursiveLevel = 0) { |
15 | return new Promise((resolve) => { | 15 | return new Promise((resolve) => { |
16 | if (DEBUG) { | 16 | if (DEBUG) { |
17 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Looking up', { | 17 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Looking up', { |
18 | storeId, | 18 | storeId, |
19 | originStoreId, | 19 | originStoreId, |
20 | recursiveLevel, | 20 | recursiveLevel, |
21 | }); | 21 | }); |
22 | } | 22 | } |
23 | 23 | ||
24 | if (cache[`STORE_ID_${storeId}`]) { | 24 | if (cache[`STORE_ID_${storeId}`]) { |
25 | logger.verbose('PRICPLAN.lookupCorrectPriceplan: Got from cache', { | 25 | logger.verbose('PRICPLAN.lookupCorrectPriceplan: Got from cache', { |
26 | storeId, | 26 | storeId, |
27 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, | 27 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, |
28 | }); | 28 | }); |
29 | resolve({ | 29 | resolve({ |
30 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, | 30 | priceplan: cache[`STORE_ID_${storeId}`].priceplan, |
31 | recursiveLevel: null, | 31 | recursiveLevel: null, |
32 | }); | 32 | }); |
33 | 33 | ||
34 | return; | 34 | return; |
35 | } | 35 | } |
36 | 36 | ||
37 | const query = ` | 37 | const query = ` |
38 | -- KOMODO MIGRATION EVO - PRICEPLAN - pageShouldBe | 38 | -- KOMODO MIGRATION EVO - PRICEPLAN - pageShouldBe |
39 | SELECT SQL_CACHE parent_id, priceplan_name | 39 | SELECT SQL_CACHE parent_id, priceplan_name |
40 | FROM stores USE INDEX (PRIMARY) | 40 | FROM stores USE INDEX (PRIMARY) |
41 | WHERE id = ? | 41 | WHERE id = ? |
42 | LIMIT 1 | 42 | LIMIT 1 |
43 | `.trim(); | 43 | `.trim(); |
44 | 44 | ||
45 | const values = [storeId]; | 45 | const values = [storeId]; |
46 | 46 | ||
47 | dbKomodo.query(query, values, async (err, result) => { | 47 | dbKomodo.query(query, values, async (err, result) => { |
48 | if (err) { | 48 | if (err) { |
49 | const fullQuery = await dbKomodo.format(query, values); | 49 | const fullQuery = await dbKomodo.format(query, values); |
50 | logger.warn(`PRICEPLAN.lookupCorrectPriceplan: DB ERROR. ${err.toString()}`, { | 50 | logger.warn(`PRICEPLAN.lookupCorrectPriceplan: DB ERROR. ${err.toString()}`, { |
51 | storeId, originStoreId, recursiveLevel, fullQuery, | 51 | storeId, originStoreId, recursiveLevel, fullQuery, |
52 | }); | 52 | }); |
53 | 53 | ||
54 | resolve({ | 54 | resolve({ |
55 | priceplan: null, | 55 | priceplan: null, |
56 | recursiveLevel, | 56 | recursiveLevel, |
57 | }); | 57 | }); |
58 | return; | 58 | return; |
59 | } | 59 | } |
60 | 60 | ||
61 | const data = result && result[0]; | 61 | const data = result && result[0]; |
62 | 62 | ||
63 | if (!data) { | 63 | if (!data) { |
64 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: STORE not found', { storeId, originStoreId, recursiveLevel }); | 64 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: STORE not found', { storeId, originStoreId, recursiveLevel }); |
65 | resolve({ | 65 | resolve({ |
66 | priceplan: null, | 66 | priceplan: null, |
67 | recursiveLevel, | 67 | recursiveLevel, |
68 | }); | 68 | }); |
69 | return; | 69 | return; |
70 | } | 70 | } |
71 | 71 | ||
72 | if (data.priceplan_name.toUpperCase() === 'MARKUP') { | 72 | if (data.priceplan_name.toUpperCase() === 'MARKUP') { |
73 | if (!data.parent_id) { | 73 | if (!data.parent_id) { |
74 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: Suitable priceplan not found', { | 74 | logger.warn('PRICEPLAN.lookupCorrectPriceplan: Suitable priceplan not found', { |
75 | storeId, | 75 | storeId, |
76 | originStoreId, | 76 | originStoreId, |
77 | recursiveLevel, | 77 | recursiveLevel, |
78 | }); | 78 | }); |
79 | 79 | ||
80 | resolve({ | 80 | resolve({ |
81 | priceplan: null, | 81 | priceplan: null, |
82 | recursiveLevel, | 82 | recursiveLevel, |
83 | }); | 83 | }); |
84 | return; | 84 | return; |
85 | } | 85 | } |
86 | 86 | ||
87 | if (DEBUG) { | 87 | if (DEBUG) { |
88 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Priceplan is MARKUP, going to look up on parent', { | 88 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Priceplan is MARKUP, going to look up on parent', { |
89 | storeId, | 89 | storeId, |
90 | originStoreId, | 90 | originStoreId, |
91 | recursiveLevel, | 91 | recursiveLevel, |
92 | parentId: data.parent_id, | 92 | parentId: data.parent_id, |
93 | }); | 93 | }); |
94 | } | 94 | } |
95 | const dataFromParent = await lookupCorrectPriceplan( | 95 | const dataFromParent = await lookupCorrectPriceplan( |
96 | data.parent_id, originStoreId, recursiveLevel + 1, | 96 | data.parent_id, originStoreId, recursiveLevel + 1, |
97 | ); | 97 | ); |
98 | 98 | ||
99 | resolve({ | 99 | resolve({ |
100 | priceplan: dataFromParent.priceplan, | 100 | priceplan: dataFromParent.priceplan, |
101 | recursiveLevel: dataFromParent.recursiveLevel, | 101 | recursiveLevel: dataFromParent.recursiveLevel, |
102 | }); | 102 | }); |
103 | return; | 103 | return; |
104 | } | 104 | } |
105 | 105 | ||
106 | cache[`STORE_ID_${storeId}`] = { | 106 | cache[`STORE_ID_${storeId}`] = { |
107 | priceplan: data.priceplan_name, | 107 | priceplan: data.priceplan_name, |
108 | recursiveLevel, | 108 | recursiveLevel, |
109 | }; | 109 | }; |
110 | 110 | ||
111 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Got suitable result', { | 111 | if (DEBUG) { |
112 | storeId, | 112 | logger.verbose('PRICEPLAN.lookupCorrectPriceplan: Got suitable result', { |
113 | originStoreId, | 113 | storeId, |
114 | priceplanName: data.priceplan_name, | 114 | originStoreId, |
115 | recursiveLevel, | 115 | priceplanName: data.priceplan_name, |
116 | }); | 116 | recursiveLevel, |
117 | }); | ||
118 | } | ||
117 | 119 | ||
118 | resolve({ | 120 | resolve({ |
119 | priceplan: data.priceplan_name, | 121 | priceplan: data.priceplan_name, |
120 | recursiveLevel, | 122 | recursiveLevel, |
121 | }); | 123 | }); |
122 | }); | 124 | }); |
123 | }); | 125 | }); |
124 | } | 126 | } |
125 | 127 | ||
126 | async function pageShouldBe(req, res) { | 128 | async function pageShouldBe(req, res) { |
127 | if (!req.body) req.body = {}; | 129 | if (!req.body) req.body = {}; |
128 | 130 | ||
131 | const startTime = new Date(); | ||
132 | |||
129 | const storeId = req.body.store_id || req.query.store_id; | 133 | const storeId = req.body.store_id || req.query.store_id; |
130 | if (!storeId) { | 134 | if (!storeId) { |
131 | res.json({ | 135 | res.json({ |
132 | error: true, | 136 | error: true, |
133 | error_message: 'Undefined parameter store_id', | 137 | error_message: 'Undefined parameter store_id', |
134 | }); | 138 | }); |
135 | return; | 139 | return; |
136 | } | 140 | } |
137 | 141 | ||
138 | if (DEBUG) { | 142 | logger.verbose('PRICEPLAN.pageShouldBe: Got a request', { |
139 | logger.verbose('PRICEPLAN.pageShouldBe: Got a request', { | 143 | method: req.method, |
140 | method: req.method, | 144 | storeId, |
141 | storeId, | 145 | }); |
142 | }); | ||
143 | } | ||
144 | 146 | ||
145 | const data = await lookupCorrectPriceplan(storeId, storeId, 0); | 147 | const data = await lookupCorrectPriceplan(storeId, storeId, 0); |
146 | 148 | ||
149 | const deltaTime = Number(new Date() - startTime); | ||
150 | logger.info(`PRICEPLAN.pageShouldBe: Lookup finished ${deltaTime} ms`, { storeId }); | ||
151 | |||
147 | res.json({ | 152 | res.json({ |
148 | error: false, | 153 | error: false, |
149 | error_message: null, | 154 | error_message: null, |
150 | store_id: storeId, | 155 | store_id: storeId, |
151 | recursive_level: (data && data.recursiveLevel) || null, | 156 | recursive_level: (data && data.recursiveLevel) || null, |
152 | priceplan_should_be: (data && data.priceplan) || null, | 157 | priceplan_should_be: (data && data.priceplan) || null, |
153 | raw: data, | 158 | raw: data, |
154 | }); | 159 | }); |
155 | } | 160 | } |
156 | 161 |