ZBUC3VBT42SDAG42OCUX7MTT6GNPX2GEGWU2QIUYAY4NMX33IGLAC SOWY545GNK34CPNCPETJOTSVHIJ42JHGITWSTDINO6EZILHYTWUQC IBEKBCAZIEVRFQIMTLM2O7TUULNEEAFH6O7OYWEL6BWGRBJL3CRAC 5XHBBUBSPTNNPEZZVELCS5AEAKQVHEMLAI2URKBMMBUGWTPARFVQC NDMM44EV2XD5RP7J4Q25ZX52LUP7WOMTKO3C2PDXW7IWFEVWUW6QC E4HFJ4L4PAGV7R7EXVG2B2CWDGGW7XWU4D37VURZ66HZ3ILHCFUAC SE6MCCXTIXSGMAH5EL7EPDXIQKV6THCCY2H4OQJJQOHCUQAWE5VQC ER4O22CFHMFRSJRCDYNJVT3U7Y3LMTP3SY5TEASJI5H6WLSV6K6QC MVOZW3BZR7N5OIYNDZ2F7EQEOJSNJZDWYETC5U4K3TNRD2U3I7TQC CHOON7UFJCWBEFPYPXHT2PPBSF6MPSZPWELPH2HRCJ6ZKQBDWKYAC PK7ZUXD7ELS4V6SNYYFWP45JKWKJOVWBGFDRSFY3LP47IU2M55BAC Y2ZGX4FCDVEXTYPOQNAZ5GKM2GBSF6B6EFN4EAP2V5XGHKFNCMSQC --ios means insert or selectcreate or replace function ios_store_id(_name text, _url text, _region citext, _country citext, out _store_id integer) as $$beginloopselect store_idfrom storewheretype = 'online' andname = _name andurl = _url andregion = _region andcountry = _countryinto _store_id;exit when found;insert into store (name,url,region,country,type) values (_name,_url,_region,_country,'online') on conflict do nothingreturning store_idinto _store_id;exit when found;end loop;end$$ language plpgsql;create or replace function ios_product_id(_producer text, _brand text, _display_name text, _joined_name citext, _type product_type, _strain strain, _strain_family citext, _description text, _show boolean, _terpenes citext[], _dosage decimal, out _product_id integer) as $$beginloopselect product_idfrom productwhereproducer is not distinct from _producer andbrand is not distinct from _brand andjoined_name is not distinct from _joined_name andtype = _typeinto _product_id;exit when found;insert into product (producer,brand,display_name,joined_name,type,strain,strain_family_id,description,show,terpenes,dosage) values (_producer,_brand,_display_name,_joined_name,_type,_strain,(select strain_family_id from strain_family where joined_name = _strain_family),_description,_show,_terpenes,_dosage) on conflict do nothingreturning product_idinto _product_id;exit when found;end loop;end$$ language plpgsql;create or replace function ios_variant_id(_product_id integer, _portions integer, _quantity integer, _flavor citext, _gram_equivalency decimal, out _variant_id integer, out g integer) as $$beginloopselectvariant_id,gram_equivalencyfrom variantwhereproduct_id = _product_id andquantity = _quantity andportions = _portions andflavor is not distinct from _flavorinto _variant_id, g;exit when found;insert into variant (product_id,portions,quantity,flavor,gram_equivalency) values (_product_id,_portions,_quantity,_flavor,_gram_equivalency) on conflict do nothingreturning variant_idinto _variant_id;exit when found;end loop;end$$ language plpgsql;
async return_key(query, values) {//this function should diereturn (await postgres.query({text: query,values: values,rowMode: 'array'})).rows;}//abstract out db call out of ocs.ca.js before working on other stores
async images(pics/*type set*/, productID) {for(let pic of pics) {let product_images = (await postgres.query(`select product_image_id, case when now() - system_time_start > '30 days' then destination else null end as destination from product_image where product_id = $1 and source = $2`, [productID, pic])).rows;if(product_images.length === 0) {let destination;if(process.env.CLOUDINARY_URL === 'fake') {//https://stackoverflow.com/a/13108449/let name = pic.split('/');name = name.pop() || name.pop();name += name.substring(name.lastIndexOf('.'));destination = "https://res.cloudinary.com/the-eternal-tao/image/upload/" + name;} else {let response = (await exec('./image.sh ' + pic)).stdout.trim();if(response.startsWith('/tmp/')) {destination = (await cloudinary.uploader.upload(response, {public_id: response.substring(5), unique_filename: false})).secure_url;} else {continue;}}let img_insert = (await postgres.query(`insert into product_image (product_id,source,destination) values ($1,$2,$3) on conflict do nothingreturning ID`,[productID, pic, destination])).rows;if(img_insert.length === 0) {console.error("how the FUCK", productID, pic, destination);} else if(img_insert.length > 1) {console.error('kys');}} else if(product_images.length === 1) {if(product_images[0].destination !== null && process.env.CLOUDINARY_URL !== 'fake') {let response = (await exec(`./image.sh ${pic} ${product_images[0].destination}`)).stdout.trim();if(response.startsWith('/tmp/')) {await postgres.query('update product_image set destination = $1 where product_image_id = $2', [(await cloudinary.uploader.upload(response, {public_id: response.substring(5), unique_filename: false})).secure_url,product_images[0].product_image_id]);}}} else {console.error('data integrity issue', product_images);//product match, source match, but destinations are different, which should be unpossible}}}
let storeID = await return_key(`insert into store (name,URL,region,country) values ($1, $2, $3, $4) on conflict do nothingreturning ID`,['Ontario Cannabis Store',url,'ontario','canada']);if(storeID.length === 0) {storeID = await return_key(`selectIDfromstorewherename = $1 andURL = $2 andregion = $3 andcountry = $4 andaddress is null andaddress2 is null andcity is null andpostal_code is null andcoordinate is null andtimezone is null`,['Ontario Cannabis Store',url,'ontario','canada']);if(storeID.length === 0) {console.error("why can't I find the store???");process.exit(1);}}storeID = storeID[0][0];
let storeID = (await postgres.query(`select ios_store_id('Ontario Cannabis Store', $1, 'ontario', 'canada')`, [url])).rows[0].ios_store_id;
terpenes]let productID = await return_key(`insert into product (producer,brand,display_name,joined_name,type,strain,strain_family,description,show,terpenes${brand === 'dosist' ? ',dosage' : ''}) values ($1, $2, $3, $4, $5, $6, (select id from strain_family where joined_name = $7), $8, true, $9${brand === 'dosist' ? `,$${ppams.push('.00225')}` : ''}) on conflict do nothingreturning ID`,ppams);if(productID.length === 0) {//console.log(productID, `${url}/products/${e[i][0].handle}`) //on empty db, useful to find products that are actually variants of something we inserted earlier. good job ocs dumbfucksproductID = await return_key(`selectIDfromproductwhereproducer is not distinct from $1 andbrand is not distinct from $2 andjoined_name is not distinct from $3 andtype = $4`,[producer, brand, join_name, e[i][0].type]);if(productID.length === 0) {console.error("why can't I find the product???");process.exit(1);}}productID = productID[0][0];
terpenes,brand === 'dosist' ? '.00225' : null])).rows[0].ios_product_id;
let pics = new Set((await request({url: `${url}/products/${e[i][0].handle}.json`,headers: {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"}}).then(JSON.parse)).product.images.map(({src}) => {let questionIndex = src.indexOf('?');if(questionIndex === -1) {return src;} else {return src.substring(0, questionIndex);}}));for(let pic of pics) {let product_images = (await postgres.query(`select id, case when now() - system_time_start > '30 days' then destination else null end as destination from product_image where product = $1 and source = $2`, [productID, pic])).rows;if(product_images.length === 0) {let destination;if(process.env.CLOUDINARY_URL === 'fake') {//https://stackoverflow.com/a/13108449/let name = pic.split('/');name = name.pop() || name.pop();name += name.substring(name.lastIndexOf('.'));destination = "https://res.cloudinary.com/the-eternal-tao/image/upload/" + name;
images(new Set((await request({url: `${url}/products/${e[i][0].handle}.json`,headers: {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"}}).then(JSON.parse)).product.images.map(({src}) => {let questionIndex = src.indexOf('?');if(questionIndex === -1) {return src;
let response = (await exec('./image.sh ' + pic)).stdout.trim();if(response.startsWith('/tmp/')) {destination = (await cloudinary.uploader.upload(response, {public_id: response.substring(5), unique_filename: false})).secure_url;} else {continue;}
return src.substring(0, questionIndex);
let img_insert = await return_key(`insert into product_image (product,source,destination) values ($1,$2,$3) on conflict do nothingreturning ID`,[productID, pic, destination]);if(img_insert.length === 0) {console.error("how the FUCK", productID, pic, destination);} else if(img_insert.length > 1) {console.error('kys');}} else if(product_images.length === 1) {if(product_images[0].destination !== null && process.env.CLOUDINARY_URL !== 'fake') {let response = (await exec(`./image.sh ${pic} ${product_images[0].destination}`)).stdout.trim();if(response.startsWith('/tmp/')) {await postgres.query('update product_image set destination = $1 where id = $2', [(await cloudinary.uploader.upload(response, {public_id: response.substring(5), unique_filename: false})).secure_url,product_images[0].id]);}}} else {console.error('data integrity issue', product_images);//product match, source match, but destinations are different, which should be unpossible}}
})),productID);
let variantID = await return_key(`insert into variant (product,portions,quantity,flavor,gram_equivalency) values ($1, $2, $3, $4, $5)on conflict do nothingreturning ID`,[productID, portions, quantity, e[i][j].options.flavour, gram_equivalency]);if(variantID.length === 0) {let parameters = [productID, quantity];variantID = await return_key(`selectID,gram_equivalencyfromvariantwhereproduct = $1 andquantity = $2 andportions = $${parameters.push(portions)} andflavor ${e[i][j].options.flavour === undefined ? 'is null' : '= $' + parameters.push(e[i][j].options.flavour)}`,parameters);if(gram_equivalency !== variantID[0][1]) {console.log('warning: gram_equivalency does not match', gram_equivalency, variantID[0][1], productID, variantID[0][0], `${url}/products/${e[i][j].handle}?variant=${e[i][j].objectID}`);}if(variantID.length === 0) {console.error("why can't I find the variant???");process.exit(1);}}variantID = variantID[0][0];
console.log('gram equivalency', gram_equivalency);let variantID = (await postgres.query(`select ios_variant_id($1, $2, $3, $4, $5)`, [productID,portions,quantity,e[i][j].options.flavour,gram_equivalency])).rows[0];console.log('vid', variantID);// => '(1,)'