let params = [parameters.store, parameters.updated[i].menu_item];
delete parameters.updated[i].menu_item;
//todo: https://stackoverflow.com/a/18146482
let query = `update menu_item set ${Object.keys(parameters.updated[i]).map(k => `${k} = $${params.push(parameters.updated[i][k])}`).join(', ')} where store = $1 and id = $2`
console.log(query);
if(Number.isInteger(Number(parameters.updated[i].menu_item))) {
let params = [parameters.store, parameters.updated[i].menu_item];
let what = [];
if(isFinite(parameters.updated[i].price)) {
what.push(`price = $${params.push(parameters.updated[i].price)}`);
}
if(Number.isInteger(Number(parameters.updated[i].stock))) {
what.push(`stock = $${params.push(parameters.updated[i].stock)}`);
}
if(typeof parameters.updated[i].featured === 'boolean') {
what.push(`featured = $${params.push(parameters.updated[i].featured)}`);
}
if(isFinite(parameters.updated[i].min_cbd) && isFinite(parameters.updated[i].max_cbd)) {
what.push(`cbd = $${params.push(`[${parameters.updated[i].min_cbd},${parameters.updated[i].max_cbd}]`)}`);
} else if(isFinite(parameters.updated[i].min_cbd)) {
what.push(`cbd = numrange($${params.push(parameters.updated[i].min_cbd)}, upper(cbd), '[]')`);
} else if(isFinite(parameters.updated[i].max_cbd)) {
what.push(`cbd = numrange(lower(cbd), $${params.push(parameters.updated[i].max_cbd)}, '[]')`);
}
if(isFinite(parameters.updated[i].min_thc) && isFinite(parameters.updated[i].max_thc)) {
what.push(`thc = $${params.push(`[${parameters.updated[i].min_thc},${parameters.updated[i].max_thc}]`)}`);
} else if(isFinite(parameters.updated[i].min_thc)) {
what.push(`thc = numrange($${params.push(parameters.updated[i].min_thc)}, upper(thc), '[]')`);
} else if(isFinite(parameters.updated[i].max_thc)) {
what.push(`thc = numrange(lower(thc), $${params.push(parameters.updated[i].max_thc)}, '[]')`);
}
if(what.length > 0) {
await pool.query(`update menu_item set ${what.join(', ')} where store_id = $1 and menu_item_id = $2`, params);
}
}