PQTLWOFEMHL6BXIVUAYBHF5Z4F4CK7AD2QS4NOWGAFGV63EWKJNAC
BGFJK74YUJLEGVJMDJ77J24BTCO2DEJSKZIP57HTL4CUFIZ2B2QQC
OLMU4W2JCB5WXCGSS4ZD4LPLXVLFX3KCTOCI4VLNF765ZXHOGPDQC
JAAJEH57DRFXCZGAPOQLI4MN7N57OG52E7TQCC4FFJSOPDX4M65QC
NBGKNI25T2AKNWDH5Z6U6JDUHLQPNRL6GQFF5VS37OOHONSP4CKQC
NDMM44EV2XD5RP7J4Q25ZX52LUP7WOMTKO3C2PDXW7IWFEVWUW6QC
E4HFJ4L4PAGV7R7EXVG2B2CWDGGW7XWU4D37VURZ66HZ3ILHCFUAC
BUCBGGRUTH7HNAADSU24U4BPHPUY7QYB3KRZPIIDUERZTL3YNTZQC
72DUSPYXB64ECTFQWWD7XSY4YJUNRCFZT22AS4BDRIIUZP33CZGAC
QSEGNTGEAE4GCH6VRPHJL2JJI3WFXBDO4VJ2LTEFYHYK4576ZEHAC
ZBUC3VBT42SDAG42OCUX7MTT6GNPX2GEGWU2QIUYAY4NMX33IGLAC
SE6MCCXTIXSGMAH5EL7EPDXIQKV6THCCY2H4OQJJQOHCUQAWE5VQC
CHOON7UFJCWBEFPYPXHT2PPBSF6MPSZPWELPH2HRCJ6ZKQBDWKYAC
Y2ZGX4FCDVEXTYPOQNAZ5GKM2GBSF6B6EFN4EAP2V5XGHKFNCMSQC
54TMOAIBD5UBRSWPGW45YZO4VMVKSP7YYKIQ7H4LWSVCN2E3QA4QC
WYTMZJFYVKHR4QH7AV5JUNWXT6NAC5NNQNPZCQSDI6LGI7DVXFYAC
63VXWIHIAKGK7J4VTNRUAG2V32N2QUSWFELB6GD34S54FGRWAPCQC
EGNRBQUMOZHAF5B7KCMTAPMB4H3USK4MK33Q6T5V26D6HOJNW6LQC
create or replace function ios_variant_id(_product_id integer, _portions smallint, _quantity decimal, _flavor citext, _gram_equivalency decimal, out _variant_id integer, out g decimal) as $$
create or replace function ios_variant_id(_product_id integer, _portions smallint, _quantity decimal, _flavor citext = null, _gram_equivalency decimal = null, out _variant_id integer, out g decimal) as $$
let gram_equivalency = new Big(quickview.variants.find(v => v.id == e[i][j].objectID).metafields.variant.gram_equivalency);
let gram_equivalency = e[i][0].type === 'oil' || e[i][0].type === 'spray' || e[i][0].type === 'capsule' ? new Big(quickview.variants.find(v => v.id == e[i][j].objectID).metafields.variant.gram_equivalency) : null;
let [variantID, g] = (await postgres.query(`select ios_variant_id($1, $2, $3, $4, $5)`, [
productID,
portions,
quantity,
e[i][j].options.flavour,
gram_equivalency.toString()
])).rows[0].ios_variant_id.slice(1, -1).split(',');
if(g.length && !gram_equivalency.eq(g)) {
let params = [productID, portions, quantity]
let [variantID, g] = (await postgres.query(
`select ios_variant_id($1, $2, $3${e[i][j].options.flavour ? `, $${params.push(e[i][j].options.flavour)}` : ''}${gram_equivalency === null ? '' : `, $${params.push(gram_equivalency.toString())}`})`,
params
)).rows[0].ios_variant_id.slice(1, -1).split(',');
if(g.length && gram_equivalency !== null && !gram_equivalency.eq(g)) {
} else if(parameters.type === 'preroll') {
//gram equivalency = portions * quantity
values = parameters.variants.reduce((acc, v) => {
if(values.length) {
query += `, insert${++q} as (insert into variant (product_id, quantity) select product_id, x::numeric from insert1 cross join (values ${values.join(',')}) as t(x) on conflict do nothing)`;
}
} else if(parameters.type === 'preroll' || parameters.type === 'tea' || parameters.type === 'gummy' || parameters.type === 'mint' || parameters.type === 'chocolate' || parameters.type === 'baked') {
let values = parameters.variants.reduce((acc, v) => {
acc.push(`((select product_id from insert1), $${params.push(v.portions)}, $${params.push(v.quantity)}, $${params.push((new Big(v.quantity)).times(v.portions).toString())})`);
acc.push(`($${params.push(v.portions)}, $${params.push(v.quantity)})`);
} else if(parameters.type === 'capsule') {
//null quantity possible
values = parameters.variants.reduce((acc, v) => {
if(isFinite(v.gram_equivalency) && Number.isInteger(Number(v.portions))) {
acc.push(`((select product_id from insert1), $${params.push(v.portions)}, ${isFinite(v.quantity) ? '$' + params.push(v.quantity) : 'null'}, $${params.push(v.gram_equivalency)})`);
if(values.length) {
query += `, insert${++q} as (insert into variant (product_id, portions, quantity) select product_id, x::smallint, y::numeric from insert1 cross join (values ${values.join(',')}) as t(x, y) on conflict do nothing)`;
}
} else if(parameters.type === 'cartridge' || parameters.type === 'disposable') {
let values = parameters.variants.reduce((acc, v) => {
if(isFinite(v.quantity)) {
acc.push(`($${params.push(v.quantity)}, ${v.flavor ? '$' + params.push(v.flavor) : 'null'})`);
} else {
//default if falsy portions
values = parameters.variants.reduce((acc, v) => {
if(isFinite(v.gram_equivalency) && isFinite(v.quantity)) {
acc.push(`((select product_id from insert1), ${v.portions ? '$' + params.push(v.portions) : 'default'}, $${params.push(v.quantity)}, $${params.push(v.gram_equivalency)}${parameters.type === 'disposable' || parameters.type === 'cartridge' ? `, ${v.flavor ? '$' + params.push(v.flavor) : 'null'}` : ''})`);
if(values.length) {
query += `, insert${++q} as (insert into variant (product_id, quantity, flavor) select product_id, x::numeric, y from insert1 cross join (values ${values.join(',')}) as t(x, y) on conflict do nothing)`;
}
} else if (parameters.type === 'oil' || parameters.type === 'spray') {
let values = parameters.variants.reduce((acc, v) => {
if(isFinite(v.gram_equivalency) && Number.isInteger(Number(v.quantity))) {
acc.push(`($${params.push(v.quantity)}, $${params.push(v.gram_equivalency)})`);
}
if(values.length) {
query += `, insert${++q} as (insert into variant (product_id, portions, quantity, gram_equivalency${parameters.type === 'disposable' || parameters.type === 'cartridge' ? ', flavor' : ''}) values ${values.join(', ')} on conflict do nothing)`;
if(values.length) {
query += `, insert${++q} as (insert into variant (product_id, quantity, gram_equivalency) select product_id, y::numeric, z::numeric from insert1 cross join (values ${values.join(',')}) as t(y, z) on conflict do nothing)`;
}
} else if(parameters.type === 'capsule') {
let values = parameters.variants.reduce((acc, v) => {
if(isFinite(v.gram_equivalency) && Number.isInteger(Number(v.portions))) {
acc.push(`($${params.push(v.portions)}, ${isFinite(v.quantity) ? '$' + params.push(v.quantity) : 'null'}, $${params.push(v.gram_equivalency)})`);
}
return acc;
}, []);
if(values.length) {
query += `, insert${++q} as (insert into variant (product_id, portions, quantity, gram_equivalency) select product_id, x::smallint, y::numeric, z::numeric from insert1 cross join (values ${values.join(',')}) as t(x, y, z) on conflict do nothing)`;
}
query += `, insert${++q} as (insert into product_image (product_id, destination) values ${parameters.images.map(i => `((select product_id from insert1), $${params.push(i)})`).join(', ')} on conflict do nothing)`;
query += `, insert${++q} as (insert into product_image (product_id, destination) select product_id, x from insert1 cross join (values ${parameters.images.map(i => `(${params.push(i)})`).join(',')}) as t(x) on conflict do nothing)`;