Revert "Replaced all instances of sid and pid"

This reverts commit d7cb83ec70.
This commit is contained in:
Keith Irwin 2023-05-06 14:01:08 -06:00
parent 79da1842c8
commit a7c5fd2489
Signed by: ki9
GPG Key ID: DF773B3F4A88DA86
11 changed files with 89 additions and 83 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ node_modules/
.env
db/
docker-compose.yml
sold/*
view-wallet/*
moneropay-db/*

View File

@ -1,18 +1,18 @@
const updateCart = (product, unit, price, name, qty, weight, volume, link) => {
localStorage.setItem(`cart_${product}/${unit}`,
const updateCart = (pid, sid, price, name, qty, weight, volume, link) => {
localStorage.setItem(`cart_${pid}/${sid}`,
`{"name":"${name}","qty":"${qty}","price":"${price}","link":"${link}","weight":"${weight}","volume":"${volume}"}`
)
recountCart()
}
const addToCart = (product, unit, price, name, weight, volume, link) => {
const btnElem = document.getElementById(`add-to-cart_${product}_${unit}`)
const qtyElem = document.getElementById(`qty-select_${product}_${unit}`)
const qtyWrapElem = document.getElementById(`qty-select-wrap_${product}_${unit}`)
const minusElem = document.getElementById(`qty-minus_${product}_${unit}`)
const plusElem = document.getElementById(`qty-plus_${product}_${unit}`)
if (localStorage.getItem(`cart_${product}/${unit}`)) {
localStorage.removeItem(`cart_${product}/${unit}`)
const addToCart = (pid, sid, price, name, weight, volume, link) => {
const btnElem = document.getElementById(`add-to-cart_${pid}_${sid}`)
const qtyElem = document.getElementById(`qty-select_${pid}_${sid}`)
const qtyWrapElem = document.getElementById(`qty-select-wrap_${pid}_${sid}`)
const minusElem = document.getElementById(`qty-minus_${pid}_${sid}`)
const plusElem = document.getElementById(`qty-plus_${pid}_${sid}`)
if (localStorage.getItem(`cart_${pid}/${sid}`)) {
localStorage.removeItem(`cart_${pid}/${sid}`)
qtyElem.value = 0
minusElem.disabled = true
qtyWrapElem.style.display = 'none'
@ -21,11 +21,11 @@ const addToCart = (product, unit, price, name, weight, volume, link) => {
btnElem.style.boxShadow = '-0.2vw -0.3vh calc(0.2vw + 0.2vh) #000 inset, 0.1vw 0.1vh calc(0.15vw + 0.15vh) #ddd inset'
recountCart()
} else {
updateCart(product, unit, price, name, 1, weight, volume, link)
updateCart(pid, sid, price, name, 1, weight, volume, link)
minusElem.disabled = false
qtyElem.value = 1
if (qtyElem.value===qtyElem.max) plusElem.disabled = true
if (unit) {
if (sid) {
qtyWrapElem.style.display = 'none'
btnElem.style.display = 'block'
btnElem.innerText = '✔️ Added to cart'
@ -37,25 +37,25 @@ const addToCart = (product, unit, price, name, weight, volume, link) => {
}
}
const qtyMinus = (product, unit) => {
const qtyElem = document.getElementById(`qty-select_${product}_${unit}`)
const qtyMinus = (pid, sid) => {
const qtyElem = document.getElementById(`qty-select_${pid}_${sid}`)
if (qtyElem.value > 0) qtyElem.value--
qtyElem.onchange()
}
const qtyPlus = (product, unit) => {
const qtyElem = document.getElementById(`qty-select_${product}_${unit}`)
const qtyPlus = (pid, sid) => {
const qtyElem = document.getElementById(`qty-select_${pid}_${sid}`)
if (qtyElem.value < qtyElem.max) qtyElem.value++
qtyElem.onchange()
}
const qtyChanged = (product, unit, price, name, weight, volume, link) => {
const btnElem = document.getElementById(`add-to-cart_${product}_${unit}`)
const qtyElem = document.getElementById(`qty-select_${product}_${unit}`)
const qtyWrapElem = document.getElementById(`qty-select-wrap_${product}_${unit}`)
const minusElem = document.getElementById(`qty-minus_${product}_${unit}`)
const plusElem = document.getElementById(`qty-plus_${product}_${unit}`)
const qtyChanged = (pid, sid, price, name, weight, volume, link) => {
const btnElem = document.getElementById(`add-to-cart_${pid}_${sid}`)
const qtyElem = document.getElementById(`qty-select_${pid}_${sid}`)
const qtyWrapElem = document.getElementById(`qty-select-wrap_${pid}_${sid}`)
const minusElem = document.getElementById(`qty-minus_${pid}_${sid}`)
const plusElem = document.getElementById(`qty-plus_${pid}_${sid}`)
if (qtyElem.value==='0') {
localStorage.removeItem(`cart_${product}/${unit}`)
localStorage.removeItem(`cart_${pid}/${sid}`)
minusElem.disabled = true
qtyWrapElem.style.display = 'none'
btnElem.style.display = 'block'
@ -67,23 +67,23 @@ const qtyChanged = (product, unit, price, name, weight, volume, link) => {
if (parseFloat(qtyElem.value) > parseFloat(qtyElem.max)) qtyElem.value = qtyElem.max
if (qtyElem.value < qtyElem.max) plusElem.disabled = false
else plusElem.disabled = true
updateCart(product, unit, price, name, qtyElem.value, weight, volume, link)
updateCart(pid, sid, price, name, qtyElem.value, weight, volume, link)
}
}
// On Page Load, read from cart
const productLoaded = (product, unit) => {
const btnElem = document.getElementById(`add-to-cart_${product}_${unit}`)
const qtyElem = document.getElementById(`qty-select_${product}_${unit}`)
const qtyWrapElem = document.getElementById(`qty-select-wrap_${product}_${unit}`)
const stored = localStorage.getItem(`cart_${product}/${unit}`)
if (stored && unit!=='') {
const productLoaded = (pid, sid) => {
const btnElem = document.getElementById(`add-to-cart_${pid}_${sid}`)
const qtyElem = document.getElementById(`qty-select_${pid}_${sid}`)
const qtyWrapElem = document.getElementById(`qty-select-wrap_${pid}_${sid}`)
const stored = localStorage.getItem(`cart_${pid}/${sid}`)
if (stored && sid!=='') {
qtyWrapElem.style.display = 'none'
qtyElem.value = '0'
btnElem.style.display = 'block'
btnElem.innerText = '✔️ Added to cart'
btnElem.style.boxShadow = '-0.05vw -0.1vh calc(0.05vw + 0.1vh) #ddd inset, 0.1vw 0.1vh calc(0.15vw + 0.15vh) #000 inset'
} else if (stored && unit==='') {
} else if (stored && sid==='') {
qtyWrapElem.style.display = 'flex'
qtyElem.value = JSON.parse(stored).qty
btnElem.style.display = 'none'

View File

@ -11,15 +11,15 @@ const getUspsRates = (zip, weight, volume) => {
class CartItem { constructor(data) {
let self = this
self.name = data.name
self.product = data.product
self.unit = data.unit
self.pid = data.pid
self.sid = data.sid
self.price = data.price
self.weight = data.weight
self.volume = data.volume
self.link = data.link
self.pretty_price = formatUSD(parseFloat(data.price))
self.qty = ko.observable(data.qty)
self.maxQty = (self.unit)? 1:99
self.maxQty = (self.sid)? 1:99
self.subtotal = ko.pureComputed(() =>
self.price * self.qty()
@ -31,8 +31,8 @@ class CartItem { constructor(data) {
// Subscribe to changes in qty
self.qty.subscribe((qty) => {
if (qty===0||qty==='0')
localStorage.removeItem(`cart_${self.product}/${self.unit}`)
else localStorage.setItem(`cart_${self.product}/${self.unit}`,
localStorage.removeItem(`cart_${self.pid}/${self.sid}`)
else localStorage.setItem(`cart_${self.pid}/${self.sid}`,
`{"name":"${self.name}","qty":"${qty}","price":"${self.price}","link":"${self.link}","weight":"${self.weight}","volume":"${self.volume}"}`
)
recountCart()
@ -48,7 +48,7 @@ class Cart { constructor() {
self.items = ko.observableArray([])
self.removeItem = (item) => {
item.qty(0)
localStorage.removeItem(`cart_${item.product}/${item.unit}`)
localStorage.removeItem(`cart_${item.pid}/${item.sid}`)
}
self.itemMinus = (item) => {
const newval = Number(item.qty()) - 1
@ -301,8 +301,8 @@ class Cart { constructor() {
const split_item = i.slice(5).split('/')
self.items.push(new CartItem({
name: parsed_item.name,
product: split_item[0],
unit: split_item[1],
pid: split_item[0],
sid: split_item[1],
link: parsed_item.link,
price: parsed_item.price,
qty: parseFloat(parsed_item.qty),
@ -333,12 +333,12 @@ class Cart { constructor() {
const newVal = JSON.parse(e.newValue)
if (self.items().reduce(
(acc, cur) => {
acc.push(`cart_${cur.product}/${cur.unit}`)
acc.push(`cart_${cur.pid}/${cur.sid}`)
return acc
},[]).includes(e.key)) {
// Modify item already in cart
const item = self.items().filter((item) =>
`cart_${item.product}/${item.unit}` === e.key)[0]
`cart_${item.pid}/${item.sid}` === e.key)[0]
if (newVal) item.qty(newVal.qty)
else {
item.qty(0)
@ -349,8 +349,8 @@ class Cart { constructor() {
const split_item = e.key.slice(5).split('/')
self.items.push(new CartItem({
name: newVal.name,
product: split_item[0],
unit: split_item[1],
pid: split_item[0],
sid: split_item[1],
link: newVal.link,
price: newVal.price,
qty: newVal.qty,

View File

@ -58,7 +58,7 @@ const xmrPaidCheck = async (order) => {
require('../hooks/email-sales.js')(order)
// Remove single products from store
require('../hooks/remove-unit.js')(order)
require('../hooks/remove-sid.js')(order)
// Remove this listener
clearInterval(interval)

View File

@ -49,7 +49,7 @@ app.post('/', express.json(), async (req) => {
require('../hooks/email-sales')(order)
// Remove single products from store
require('../hooks/remove-unit')(order)
require('../hooks/remove-sid')(order)
}

View File

@ -23,8 +23,8 @@ const orderSchema = new Schema({
orderDate: { type:Date, default:Date.now, required:true },
items: [{
name: { type:String, required:true },
product: {type:Schema.Types.ObjectId, ref:'Product'},
unit: {type:Schema.Types.ObjectId, ref:'Unit'},
pid: String,
sid: String,
price: { type:Number, required:true },
weight: Number,
volume: Number,

View File

@ -64,9 +64,9 @@ module.exports = async (req, res) => {
const weightPromise = new Promise((resolve, reject) => {
let sum = 0
order.items.forEach((item) => {
//console.log(`Checking weight of products['${item.product||''}/${item.unit||''}']`)
if (products[`${item.product}/${item.unit}`].weight.lbs !== parseFloat(item.weight))
reject(`${item.product}/${item.unit} weight of ${products[`${item.product}/${item.unit}`].weight.lbs} lbs did not match the request, which gave it ${item.weight} lbs`)
//console.log(`Checking weight of products['${item.pid||''}/${item.sid||''}']`)
if (products[`${item.pid}/${item.sid}`].weight.lbs !== parseFloat(item.weight))
reject(`${item.pid}/${item.sid} weight of ${products[`${item.pid}/${item.sid}`].weight.lbs} lbs did not match the request, which gave it ${item.weight} lbs`)
else sum += parseFloat(item.weight) * parseFloat(item.qty)
})
resolve(sum)
@ -75,9 +75,9 @@ module.exports = async (req, res) => {
const volumePromise = new Promise((resolve, reject) => {
let sum = 0
order.items.forEach((item) => {
//console.log(`Checking volume of products['${item.product||''}/${item.unit||''}']`)
if (products[`${item.product||''}/${item.unit||''}`].volume.in3 !== parseFloat(item.volume))
reject(`${item.product}/${item.unit} volume of ${products[`${item.product||''}/${item.unit||''}`].volume.in3} in^3 did not match the request, which gave it ${item.volume} lbs`)
//console.log(`Checking volume of products['${item.pid||''}/${item.sid||''}']`)
if (products[`${item.pid||''}/${item.sid||''}`].volume.in3 !== parseFloat(item.volume))
reject(`${item.pid}/${item.sid} volume of ${products[`${item.pid||''}/${item.sid||''}`].volume.in3} in^3 did not match the request, which gave it ${item.volume} lbs`)
else sum += parseFloat(item.volume) * parseFloat(item.qty)
})
resolve(sum)
@ -85,14 +85,14 @@ module.exports = async (req, res) => {
const subtotalPromise = new Promise((resolve, reject) => {
const subtotal = order.items.reduce((acc,cur) => {
const product = products[`${cur.product||''}/${cur.unit||''}`]
const product = products[`${cur.pid||''}/${cur.sid||''}`]
if (product==null) {
console.error(`Could not find product: products[${cur.product||''}/${cur.unit||''}]`)
console.error(`Could not find product: products[${cur.pid||''}/${cur.sid||''}]`)
return res.status(404).send(`One of the products in your order could not be found on our end. Perhaps somebody bought it before you.`)
}
const listPrice = product.price.usd
if (parseFloat(cur.price)!==listPrice)
return reject(`Price of ${cur.product||''}/${cur.unit||''} listed as $${listPrice} but the client sent $${cur.price}`)
return reject(`Price of ${cur.pid||''}/${cur.sid||''} listed as $${listPrice} but the client sent $${cur.price}`)
else return acc + (listPrice * parseFloat(cur.qty))
}, 0)
if (subtotal===order.subtotal) resolve(subtotal)

View File

@ -68,6 +68,8 @@ services:
- "/etc/timezone:/etc/timezone:ro"
- "./_src:/app/_src"
- "./_site:/app/_site:ro"
- "./orders:/app/orders"
- "./sold:/app/sold"
- "./hooks:/app/hooks:ro"
- "./api:/app/api:ro"
- "./lib:/app/lib:ro"
@ -111,6 +113,8 @@ services:
volumes:
- "/etc/timezone:/etc/timezone:ro"
- "./_src:/app/_src"
- "./orders:/app/orders"
- "./sold:/app/sold"
- "./api:/app/api:ro"
- "./hooks:/app/hooks:ro"
- "./lib:/app/lib:ro"

View File

@ -5,7 +5,7 @@ const formatUSD = require('../lib/formatUSD')
module.exports = async (order) => {
const amt = (order.payment.method==='XMR')?`${order.amount.totalxmr} XMR`:formatUSD(order.amount.total)
const sales_mail_items_string = await order.items.reduce((acc,cur) =>
acc += `${cur.product}/${cur.unit} ${cur.qty} ${cur.weight} ${cur.volume}\n`
acc += `${cur.pid}/${cur.sid} ${cur.qty} ${cur.weight} ${cur.volume}\n`
, 'PID/SID qty lbs in³\n')
let sales_mail_res; try {
console.log(`Emailing sales team at ${process.env.SALES_EMAIL} about order ${order.id}`)

27
hooks/remove-sid.js Normal file
View File

@ -0,0 +1,27 @@
'use strict'
require('dotenv').config()
const run = require('../lib/run')
const SHOP_DIR = `${__dirname}/../_src/shop`
const SOLD_DIR = `${__dirname}/../sold`
module.exports = async (order) => {
const sold_sids_promise = new Promise(async (resolve) => {
order.items.forEach(async (item,i,arr) => {
let sids = []
if (item.sid) {
console.log(`Moving sid ${item.sid} from ${SHOP_DIR} to ${SOLD_DIR}`)
sids.push(item.sid)
run(`find ${SHOP_DIR} -type d -name ${item.sid} -exec mv {} ${SOLD_DIR}/${item.sid} \\; || true`)
}
if (i===arr.length -1) resolve(sids)
})
})
let sold_sids; try {
sold_sids = await sold_sids_promise
if (sold_sids.length>0) {
run('npx @11ty/eleventy --quiet')
//await run('git add _src/shop')
//await run(`git commit -m 'Sold ${sold_sids.join()}'`)
//run('git push')
}
} catch (err) { console.error(err) }
}

View File

@ -1,26 +0,0 @@
'use strict'
require('dotenv').config()
const run = require('../lib/run')
module.exports = async (order) => {
const sold_units_promise = new Promise(async (resolve) => {
order.items.forEach(async (item,i,arr) => {
let units = []
if (item.unit) {
console.log(`Moving unit ${item.unit} from ${SHOP_DIR} to ${SOLD_DIR}`)
units.push(item.unit)
run(`find ${SHOP_DIR} -type d -name ${item.unit} -exec mv {} ${SOLD_DIR}/${item.unit} \\; || true`)
}
if (i===arr.length -1) resolve(units)
})
})
let sold_units; try {
sold_units = await sold_units_promise
if (sold_units.length>0) {
run('npx @11ty/eleventy --quiet')
//await run('git add _src/shop')
//await run(`git commit -m 'Sold ${sold_units.join()}'`)
//run('git push')
}
} catch (err) { console.error(err) }
}