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