GQ2TJSGOGNH4PKDNN5ZWITLOE6YNAWO6TCBCQV4SN7ZRWEB6IZ5AC
72DUSPYXB64ECTFQWWD7XSY4YJUNRCFZT22AS4BDRIIUZP33CZGAC
63VXWIHIAKGK7J4VTNRUAG2V32N2QUSWFELB6GD34S54FGRWAPCQC
E4HFJ4L4PAGV7R7EXVG2B2CWDGGW7XWU4D37VURZ66HZ3ILHCFUAC
47JKAGWEDSPFXKMFOVMTEYGUFFS5SEJMNZIZPJJAWJ5U3CH5YGMQC
NAAQ2HMF7NXGJDRPXATVAGGZWL5MAIQWF4K5S23VFYBPOPZUXJZAC
X4XDT5S7ISHUZAJ4TK2FBEV55NY6CCUVZL2LN7DTBQ7AJCP5P3QAC
WYTMZJFYVKHR4QH7AV5JUNWXT6NAC5NNQNPZCQSDI6LGI7DVXFYAC
sort_price boolean = null,--true asc, false desc, null don't sort by it
sort_thc boolean = null,--in frontend, warn user that the sort does not make sense if types from more than one method of consumption(inhalation, sublingual, ingestion) are included in filter
sort_cbd boolean = null,--ditto
sort_rank boolean = null,--contingent on ranking
quantity decimal
quantity decimal,
thc_per_pack decimal,--this is a hack for edibles. since grouping by concentration per pack, it is constant regardless of per piece concentration. concentration * quantity * portion yields wrong result because range * first() is wrong! range * both would yield same number, but no way for frontend to know unless more data is sent
cbd_per_pack decimal--ditto
first(quantity)--this is used to convert mg / g to mg / piece for edibles
case
when first(type) in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then first(quantity)
else null
end as q,
case
when first(type) in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then first(portions * quantity * avg_range(thc))
else null
end as t,
case
when first(type) in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then first(portions * quantity * avg_range(cbd))
else null
end as c
product_id
product_id,
case
when type = 'capsule' then quantity
when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then portions * quantity * (avg_range(thc) + avg_range(cbd))--group by concentration per pack. change once we recommend by concentration per piece
else 0
end
type in ('flower', 'preroll', 'cartridge', 'disposable') or
thc2tc(thc, cbd) > .3 or
avg_range(thc) < case--76. need to fix
when type = 'oil' then 20
when type = 'spray' then 2
when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then 5 / first
end
(type in ('flower', 'preroll', 'cartridge', 'disposable', 'spray') or
case
when type = 'oil' then avg_range(cbd) < 76
when type = 'capsule' then avg_range(cbd) < 20
when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then c < 20
end)
type in ('flower', 'preroll', 'cartridge', 'disposable') or
thc2tc(thc, cbd) > .3 or
avg_range(thc) < case--20. need to fix
when type = 'oil' then 20
when type = 'spray' then 2
when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then 5 / first
end
(type in ('flower', 'preroll', 'cartridge', 'disposable', 'spray') or
case
when type = 'oil' then avg_range(cbd) < 20
when type = 'capsule' then avg_range(cbd) < 10
when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then c < 10
end)
when type in ('cartridge', 'disposable') then case
when type in ('cartridge', 'disposable') then avg_range(thc) < case
when frequency = 'daily' then case
when cyp2c9 = '*1/*3' then 490.9
when cyp2c9 = '*3/*3' then 409.1
end
when frequency = 'weekly' then case
when cyp2c9 = '*1/*3' then 441.8
when cyp2c9 = '*3/*3' then 339.1
else 750
end
when frequency = 'monthly' then case
when cyp2c9 = '*1/*3' then 353.1
when cyp2c9 = '*3/*3' then 184.6
else 600
end
else case--'rarely'
when cyp2c9 = '*1/*3' then 200
when cyp2c9 = '*3/*3' then 100
else 400
end
end
when type = 'capsule' then avg_range(thc) < case
when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then case
when frequency = 'daily' then case--need to divide by first, but capsule breaks it...
when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then t < case
when frequency = 'daily' then case
when x = 'focused' then species = 'hybrid' and thc2tc(thc, cbd) between .25 and .7 or species = 'sativa' and avg_range(thc) <= case
when type in ('flower', 'preroll') then 100
when type in ('disposable', 'cartridge') then 550
when type = 'oil' then 20
when type = 'spray' then 2
when type in ('capsule', 'tea', 'gummy', 'mint', 'chocolate', 'baked') then 5 / first
when x = 'focused' then species = 'hybrid' and thc2tc(thc, cbd) between .25 and .7 or species = 'sativa' and case
when type in ('flower', 'preroll') then avg_range(thc) <= 100
when type in ('disposable', 'cartridge') then avg_range(thc) <= 400
when type = 'oil' then avg_range(thc) <= 20
when type = 'spray' then true
when type = 'capsule' then avg_range(thc) <= 5
when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then t <= 5
end) from unnest(_effects) as t(x)), true)-- and
--(search is null or word_similarity(concat_ws(' ', producer, brand, product.display_name), lower(search)) > 0.5)--0.5 is too high... maybe limit instead if present?
end) from unnest(_effects) as t(x)), true) and
(search is null or search <% concat_ws(' ', producer, brand, product.display_name))
case when search is not null then word_similarity(concat_ws(' ', producer, brand, product.display_name), lower(search)) end desc nulls last,
case when sort_price then min end asc,
case when not sort_price then min end desc,
--ranking whenever that lands
case when search is not null then word_similarity(search, concat_ws(' ', producer, brand, product.display_name)) end desc,
if((parameters.store = Number(parameters.store)) && Number.isInteger(parameters.store)) {
qs += `_store_id => $${params.push(parameters.store)},`;
if(Array.isArray(parameters.stores) && (parameters.stores = parameters.stores.map(Number)).every(Number.isInteger)) {
qs += `_store_ids => $${params.push(parameters.stores)},`;
if(typeof parameters.sort_price === 'boolean') {
qs += `sort_price => $${params.push(parameters.sort_price)},`;
if(Array.isArray(parameters.thc2tc) && parameters.thc2tc.every(isFinite) && parameters.thc2tc.length === 2) {
qs += `min_thc2tc => $${params.push(parameters.thc2tc[0])},max_thc2tc => $${params.push(parameters.thc2tc[1])},`;
wack = ` where ${parameters.producers.map(p => `producer = $${params.push(p.producer)}${Array.isArray(p.brands) ? ` and brand = any($${params.push(p.brands)})`: ''}`).join(' or ')}`;
wack += ` where ${parameters.producers.map(p => `producer = $${params.push(p.producer)}${Array.isArray(p.brands) ? ` and brand = any($${params.push(p.brands)})`: ''}`).join(' or ')}`;
}
if(typeof parameters.price === 'boolean') {
wack += ` order by min_price ${parameters.price ? 'a' : 'de'}sc`;
} else if(typeof parameters.THC === 'boolean') {
wack += ` order by case when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then thc_per_pack else avg_range(thc) end ${parameters.THC ? 'a' : 'de'}sc, case when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then avg_range(thc) * quantity else null end ${parameters.THC ? 'a' : 'de'}sc`;
} else if(typeof parameters.CBD === 'boolean') {
wack += ` order by case when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then cbd_per_pack else avg_range(cbd) end ${parameters.CBD ? 'a' : 'de'}sc, case when type in ('tea', 'gummy', 'mint', 'chocolate', 'baked') then avg_range(cbd) * quantity else null end ${parameters.CBD ? 'a' : 'de'}sc`;
} else if(typeof parameters.producer === 'boolean') {//lower bad for performance. index?
wack += ` order by lower(producer) ${parameters.producer ? 'a' : 'de'}sc`;
} else if(typeof parameters.brand === 'boolean') {
wack += ` order by lower(brand) ${parameters.brand ? 'a' : 'de'}sc`;
} else if(typeof parameters.name === 'boolean') {
wack += ` order by lower(name) ${parameters.name ? 'a' : 'de'}sc`;