ONTV36PSUYFGQS4OJVRW4JO3SA6XMTUN3G4HIO6YVCOLUV5DXXPAC
KRP35NGEHSKKLQP522S7LNZN37U7Z23Q7CDTWQMRKS2NHSA6CKXQC
2ZHTBPOJQI4FS3OQJXUDBD62HVFPGSKI633IXXKAGPGQLURMKVPAC
Q3A33UTWKXZATZKX3LQ42MUAWFTWDZDMNVMLKWGC3QF6L6HBLUQAC
E4HFJ4L4PAGV7R7EXVG2B2CWDGGW7XWU4D37VURZ66HZ3ILHCFUAC
ZI2RJOZ2HXBHX7L54BTSDKESY6NG5KBJLVHCNY7K7ZLGIUOIHQUQC
WYTMZJFYVKHR4QH7AV5JUNWXT6NAC5NNQNPZCQSDI6LGI7DVXFYAC
UCY5PVFCT2QIOGR47KFS4KBF2ZPHRBOAIOH6PJEVWCHLMBRBLOMQC
DCYC55MAVFDM43TEEUMAHLPOHNQ3EJ5PEVCA7H6ZTKNQHJZK7XJAC
EGNRBQUMOZHAF5B7KCMTAPMB4H3USK4MK33Q6T5V26D6HOJNW6LQC
BUCBGGRUTH7HNAADSU24U4BPHPUY7QYB3KRZPIIDUERZTL3YNTZQC
EDVJECDIUUOSU4TS2DCL25MJDR5RQG7PQVTYEM6R3B4SFGKSX6MAC
case 'product':
//join with variant, menu items
try {
let params = [parameters.id];
let poop = 1;
let str = '';
if(parameters.variant !== undefined) {
params.push(parameters.variant);
++poop;
str += ' and variant.id = $' + poop;
case 'product_static':
let p = Number(parameters.id);
let data = 'invalid id';
if(Number.isInteger(p)) {
let params = [p];
let s = Number(parameters.store);
let query = '';
if(Number.isInteger(s)) {
query += `, 'disabled', (select bool_and(store != $${params.push(s)}) from menu_item where menu_item.variant = variant.id)`;
if(parameters.store !== undefined) {
params.push(parameters.store);
++poop;
str += ' and menu_item.store = $' + poop;
let response = (await pool.query(`
select
producer,
display_brand as brand,
product.display_name as name,
type,
strain,
(select strain_family.display_name from strain_family where strain_family.id = product.strain_family) as strain_family,
concentration_unit,
measurement_unit,
coalesce((select json_agg(destination) from product_image where product_image.product = product.id), '[]') as images,
coalesce((select json_agg(json_build_object(
'id', id,
'portions', portions,
'quantity', quantity${query}
)) from variant where variant.product = product.id), '[]') as variants
from
product
where
product.id = $1
`,
params
)).rows;
if(response.length === 0) {
data = 'product not found';
} else {
data = response[0];
console.log(str);
ws.send(JSON.stringify({
response_ID: request_ID,//https://stackoverflow.com/questions/1067016/join-queries-vs-multiple-queries
//should I query product, then query variants, then query stores that have this? not sure if it's faster or slower until more data...
//basically I'm not sure if I'm abusing first aggregate function too much...
//https://dba.stackexchange.com/questions/42998/are-individual-queries-faster-than-joins
///https://dba.stackexchange.com/questions/76973/what-is-faster-one-big-query-or-many-small-queries
//todo: show terpenes, flavor. deal with online store being link.
data: (await pool.query(`
select
product.id,
product.type,
display_brand as brand,
producer,
product.display_name as name,
strain,
strain_family.display_name as strain_family,
concentration_unit,
measurement_unit,
range_merge(menu_item.cbd) as cbd,
range_merge(menu_item.thc) as thc,
min(price) as price_min,
max(price) as price_max,
json_agg(product_image.destination) as images,
(select coalesce(json_agg(i), '[]') from (select id, portions, quantity from variant where product.id = variant.product) as i) as variants,
(select coalesce(json_agg(i), '[]') from (select distinct on(store.id) store.id, store.url, store.name from store inner join menu_item on store.id = menu_item.store inner join variant on variant.id = menu_item.variant inner join product on product.id = variant.product where product.id = $1) as i) as stores
from
product
left outer join strain_family on product.strain_family = strain_family.id
left outer join variant on variant.product = product.id
left outer join menu_item on menu_item.variant = variant.id
left outer join product_image on (product.id = product_image.product)--and product-image.featured = true
where
product.id = $1${str}
group by
product.id,
strain_family.id
--order by
--product_image.rank
`, params)).rows[0]
}));
} catch(e) {
//at the very least, return proper error for not found
console.error(e);
ws.send(JSON.stringify({
response_ID: request_ID,
data: 'check log'
}));
case 'product_dynamic': {
let v = Number(parameters.variant);
let data = 'invalid id';
if(Number.isInteger(v)) {
let params = [v];
let s = Number(parameters.store);
let query = '';
if(Number.isInteger(s)) {
query += `and store = $${params.push(s)}`;
}
//todo: if min(range) === max(range), return a singular value
//distance
//closed or open. if closed, when is it opening next?
let response = (await pool.query(`
select
range_merge(cbd) as cbd,
range_merge(thc) as thc,
min(price) as price_min,
max(price) as price_max,
json_agg(json_build_object(
'store', json_build_object(
'id', store.id,
'name', store.name,
'image', (select store_image.url from store_image where store_image.store = store.id limit 1)--order by rank someday
),
'cbd', cbd,
'thc', thc,
'price', price,
'stock', stock,
'url', store.URL || path
) order by (store.address) asc) as stores
from
menu_item
left outer join store on menu_item.store = store.id
where
variant = $1
${query}
`,
params
)).rows;
if(response.length === 0) {
data = 'product not found';
} else {
data = response[0];
}
}
ws.send(JSON.stringify({
response_ID: request_ID,
data: data
}));
break;}