case 'add2cart':
if(isLoggedIn(ws)) {
//persist cart to db
console.log(parameters);
/*let cart = (await pool.query('select id from cart where user_account = $1 and store = $2', [ws.user_ID, parameters.store])).rows;
console.log(cart);
if(cart.length === 0) {
//great, create new cart
//append line item
} else if(rows.length === 1) {
//append line items
} else {
return;
}*/
//cart[0].id is the magic
//await
//find line item with cart id and same menu_item_with_history
//I guess we can append line item no problem...
//search for an existing cart where id = ws, store = parameters.store
//https://medium.com/@betakuang/why-postgresqls-on-conflict-cannot-find-my-partial-unique-index-552327b85e1
let cart = (await pool.query(`insert into cart (user_account, store, placed_at) values ($1, $2, null) on conflict (user_account, store) where placed_at is null do nothing returning id`, [ws.user_ID, parameters.store])).rows;
if(cart.length === 0) {//https://stackoverflow.com/a/42217872/1201238
cart = (await pool.query(`select id from cart where user_account = $1 and store = $2`, [ws.user_ID, parameters.store])).rows[0].id;
} else {
cart = cart[0].id;
}
console.log(cart);//unhandled: adding menu_item from other store and currently pending order.
await pool.query(`insert into line_item (cart, product_with_history, variant_with_history, menu_item_with_history, quantity) values ($1, $2, $3, $4, 1) on conflict (cart, product_with_history, variant_with_history, menu_item_with_history) do update set quantity = line_item.quantity + excluded.quantity returning id`, [cart, parameters.product, parameters.variant, parameters.menu_item]);//no fk is dangerous. sanetize pls
ws.send(JSON.stringify({
response_ID: request_ID
}));//do we need a try catch?
} else {
//keep cart in ws only?? idk// or keep client side only? bad for analytics though
}
break;
case 'removefromcart':
break;
case 'cart'://does this ensure the right cart is returned? not passing in store...
//todo: join with product, variant, menu_item
///I don't like group by cart.created_at, is id better?
//maybe we should include cart.placed_at
let cart = (await pool.query('select cart.id, json_agg(line_item) as line_items from cart inner join line_item on cart.id = line_item.cart where cart.user_account = $1 and cart.placed_at is null group by cart.id order by cart.id desc', [ws.user_ID])).rows;//we need store for tax
console.log('cart');
let idddd;
console.log(cart);//if there's ever more than 1 we gave fucked up
if(cart.length === 0) {
cart = 'nothing'
} else {
idddd = cart[0].id;
cart = cart[0].line_items;
}
console.log('subscribing to order/' + idddd)
ws.subscribe('order/' + idddd);
ws.send(JSON.stringify({
response_ID: request_ID,
data: cart
}));
break;
case 'reserve':
//do we want cart as parameter? hm
//update cart placed_at to now()
let kartik = (await pool.query('update cart set placed_at = now() where cart.user_account = $1 and cart.placed_at is null returning id', [ws.user_ID])).rows;
console.log('kartik');
console.log(kartik);
let blacktshirt;
if(kartik.length === 0) {
blacktshirt = 'failed to reserve';
} else if(kartik.length === 1) {
//
} else {
blacktshirt = 'multiple carts kaboom';
}
ws.send(JSON.stringify({
response_ID: request_ID,
data: blacktshirt
}));
break;
case 'list_unfullfilled_orders_store':
if(isLoggedIn(ws)) {
if(is(ws, 'god')) {
ws.send(JSON.stringify({
response_ID: request_ID,
data: (await pool.query('select cart.id, cart.placed_at, json_agg(line_item) as line_items from cart inner join line_item on cart.id = line_item.cart where cart.store = $1 and cart.placed_at is not null and cart.ready_at is null group by cart.id order by cart.id desc', [Number(parameters.store)])).rows
}));
} else {
ws.send(JSON.stringify({
response_ID: request_ID,
data: 'forbidden'
}));
}
} else {
ws.send(JSON.stringify({
response_ID: request_ID,
data: 'unauthorized'
}));
}
break;
case 'list_orders':
//only stuff that has placed_at
break;
case 'fullfill':
//update cart ready_at
console.log(parameters);
await pool.query('update cart set ready_at = now() where id = $1', [Number(parameters.cart)]);//todo, order not found, make sure this user is owner of the store, etc
ws.send(JSON.stringify({
response_ID: request_ID
}));
console.log('sending message to ' + 'order/' + parameters.cart)
ws.publish('order/' + parameters.cart, JSON.stringify({
what: 'order/' + parameters.cart,
how: 'update',
data: 'ready4pickup'
}));
break;