NF2TA3BN5FXWG6LFHHI3IDBVRPMLVRNUUZBE3NWIBASMSKEQPC4QC 445Y34GSMQIF44IXOLYUL2AYYLVBHVGRP2QO7AJW3S4BP7H4UXDAC OGAHYK56XUYXXLLBRO5BPMBURNE673CQYGX4LBGLF5VWDY572B2AC 63VXWIHIAKGK7J4VTNRUAG2V32N2QUSWFELB6GD34S54FGRWAPCQC E4HFJ4L4PAGV7R7EXVG2B2CWDGGW7XWU4D37VURZ66HZ3ILHCFUAC NDMM44EV2XD5RP7J4Q25ZX52LUP7WOMTKO3C2PDXW7IWFEVWUW6QC ONTV36PSUYFGQS4OJVRW4JO3SA6XMTUN3G4HIO6YVCOLUV5DXXPAC WYTMZJFYVKHR4QH7AV5JUNWXT6NAC5NNQNPZCQSDI6LGI7DVXFYAC CPNWTEF4ZZHBAB565BALMX5QO2CR3Q32YUHE3FMWJO3O25DZNU5AC UCY5PVFCT2QIOGR47KFS4KBF2ZPHRBOAIOH6PJEVWCHLMBRBLOMQC BPHFBV52V4OA6WLKXGFK7Q4IM5EUS2SKAW5ZGSA6K3NJFJJVATIAC P7PFZMZQ5S7GZRJVYE6ACAIDKVTD6YHOOGBHZ52MBSGAIDDGJTBQC VIGS24FLOPPTBLIBZIB2LFMYCDZ3AO4FBXYADAKQMJFNPM4JHAGQC 5FCMFAPUTCCO2JILRWLC4PHOSHYFCQNH3D7PKLA267MHVZADOX2AC WZKS2NSIL7VST55EQ7QEF2YG2X3L2Y6W54AIG463ZRH462QR2NWQC $$ language sql;create or replace function get_product_dynamic (_variant_id integer) returns table (cbd numrange,thc numrange,min_price decimal(5, 2),max_price decimal(5, 2),stores json) as $$selectrange_merge(cbd),--todo: if min(range) === max(range), return a singular value. is this even possible?range_merge(thc),min(price),max(price),json_agg(json_build_object('store', json_build_object(--distance--closed or open. if closed, when is it opening next?'id', store_id,'name', store.name,'image', (select store_image.url from store_image where store_image.store_id = store_id limit 1)--order by rank someday),'cbd', cbd,'thc', thc,'price', price::text,--otherwise 32.40 will become 32.4 ... fuck json'stock', stock,'url', store.url || path,'menu_item', menu_item_id) order by type)--display online stores lastfrommenu_itemleft outer join store using (store_id)wherevariant_id = _variant_id
create or replace function get_menu_item (_variant_id integer,_store_id integer) returns table (cbd numrange,thc numrange,price decimal(5, 2),stock smallint,path text,menu_item integer) as $$selectcbd,thc,price,stock,path,menu_item_idfrom menu_itemwherevariant_id = _variant_id andstore_id = _store_id$$ language sql;create or replace function menu_item_loader (_store_id integer) returns table (id integer,type product_type,producer text,brand text,name text,species species,strain text,description text,terpenes citext[],variants json) as $$selectproduct_id,type,producer,brand,product.display_name,species,strain.display_name,description,terpenes,json_aggfromproductleft outer join strain using (strain_id)left outer join (selectproduct_id,json_agg(json_strip_nulls(json_build_object('id', variant_id,'flavor', flavor,'gram_equivalency', gram_equivalency,'portions', portions,'quantity', quantity,'checked', price is not null,'menu_item', menu_item_id,'min_thc', lower(thc),'max_thc', upper(thc),'min_cbd', lower(cbd),'max_cbd', upper(cbd),'price', price,'stock', stock,'featured', featured)) order by portions, quantity)fromvariantleft outer join (selectmenu_item_id,variant_id,cbd,thc,price,stock,featuredfrom menu_itemwhere store_id = _store_id) as menu_item_filtered using (variant_id)group byproduct_id) as variant_agg using (product_id)$$ language sql;
query += `and store = $${params.push(s)}`;
response = (await pool.query(`select * from get_menu_item($1, $${params.push(s)})`, params)).rows;} else {response = (await pool.query(`select * from get_product_dynamic($1)`, params)).rows;
//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(`selectrange_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'online', URL is not null and address is null and address2 is null and city is null and postal_code is null and coordinate is null and timezone is null),'cbd', cbd,'thc', thc,'price', price,'stock', stock,'url', store.URL || path,'menu_item', menu_item.id) order by (store.address) asc) as storesfrommenu_itemleft outer join store on menu_item.store = store.idwherevariant = $1${query}`,params)).rows;
data: (await pool.query(`selectproduct.id,type,producer,brand,product.display_name as name,strain,strain_family.display_name as strain_family,product.description,terpenes,variant_agg.variantsfromproductleft outer join strain_family on strain_family.id = product.strain_familyleft outer join (selectproduct,json_agg(json_strip_nulls(json_build_object('id', variant.id,'flavor', variant.flavor,'gram_equivalency', variant.gram_equivalency,'portions', variant.portions,'quantity', variant.quantity,'checked', menu_item_filtered.price is not null,'menu_item', menu_item_filtered.id,'min_thc', lower(menu_item_filtered.thc),'max_thc', upper(menu_item_filtered.thc),'min_cbd', lower(menu_item_filtered.cbd),'max_cbd', upper(menu_item_filtered.cbd),'price', menu_item_filtered.price,'stock', menu_item_filtered.stock,'featured', menu_item_filtered.featured)) order by variant.portions nulls first, variant.quantity) as variantsfromvariantleft outer join (select id, variant, cbd, thc, price, stock, featured from menu_item where store = $1) as menu_item_filtered on variant.id = menu_item_filtered.variantgroup byproduct) as variant_agg on product.id = variant_agg.product`,[parameters.store])).rows
data: (await pool.query(`select * from menu_item_loader($1)`, [parameters.store])).rows