Skip to content

Commit f727bca

Browse files
committed
remove stock limits from made-to-order shop
1 parent 15dab19 commit f727bca

3 files changed

Lines changed: 5 additions & 12 deletions

File tree

src/routes/shop.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ export function shopRoutes({ store, notifier }) {
4646
const id = `${req.user.id}_${product.id}`;
4747
const current = await store.get("cart", id);
4848
const requested = Math.max(1, Math.min(10, Number.parseInt(req.body.quantity || "1", 10) || 1));
49-
const quantity = Math.min(10, product.stock, (current?.quantity ?? 0) + requested);
50-
if (quantity < 1) {
51-
setFlash(res, "error", `${product.name} is out of stock.`);
52-
return res.redirect("/app/shop");
53-
}
49+
const quantity = Math.min(10, (current?.quantity ?? 0) + requested);
5450
await store.put("cart", id, { id, userId: req.user.id, productId: product.id, quantity });
5551
setFlash(res, "success", `${product.name} added to your cart.`);
5652
res.redirect(req.get("referer")?.includes("/app/shop/cart") ? "/app/shop/cart" : "/app/shop");
@@ -80,7 +76,7 @@ export function shopRoutes({ store, notifier }) {
8076
if (!product) return res.sendStatus(404);
8177
const quantity = Number.parseInt(req.body.quantity, 10);
8278
if (!Number.isInteger(quantity) || quantity <= 0) await store.delete("cart", id);
83-
else await store.put("cart", id, { id, userId: req.user.id, productId: product.id, quantity: Math.min(10, product.stock, quantity) });
79+
else await store.put("cart", id, { id, userId: req.user.id, productId: product.id, quantity: Math.min(10, quantity) });
8480
res.redirect("/app/shop/cart");
8581
});
8682

@@ -97,7 +93,6 @@ export function shopRoutes({ store, notifier }) {
9793
if (!currentCart.items.length) errors.push("Your cart is empty.");
9894
if (currentCart.total > req.user.hertz) errors.push("You do not have enough hertz for this order.");
9995
currentCart.items.forEach((item) => {
100-
if (item.quantity > item.stock) errors.push(`${item.name} only has ${item.stock} left.`);
10196
});
10297
if (errors.length) {
10398
return res.status(422).render("shop/cart", {
@@ -143,8 +138,6 @@ export function shopRoutes({ store, notifier }) {
143138
for (let index = 0; index < freshCart.items.length; index += 1) {
144139
const item = freshCart.items[index];
145140
const product = freshProducts[index];
146-
product.stock -= item.quantity;
147-
await store.put("product", product.id, product);
148141
}
149142
user.hertz = Math.max(0, Math.round((Number(user.hertz) - freshCart.total) * 100) / 100);
150143
user.updatedAt = timestamp;

views/admin/shop.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<input name="name" placeholder="Product name" required>
1111
<input name="category" placeholder="Category" required>
1212
<input type="number" name="price" min="0" placeholder="Hertz" required>
13-
<input type="number" name="stock" min="0" placeholder="Stock" required>
13+
1414
<div class="shop-image-upload" data-shop-image-upload><span>Product image</span><input type="hidden" name="image" value="" data-shop-image-url><input type="file" accept="image/jpeg,image/png,image/webp,image/gif" data-shop-image-file <%= imageUploadsConfigured ? '' : 'disabled' %>><small data-shop-image-status><%= imageUploadsConfigured ? 'Choose a JPG, PNG, WebP, or GIF up to 8 MB.' : 'Image uploads need R2 configuration.' %></small><div class="shop-image-preview" data-shop-image-preview></div></div>
1515
<input type="number" name="sort_order" value="100" placeholder="Sort">
1616
<textarea name="description" placeholder="Description" required></textarea>
@@ -25,7 +25,7 @@
2525
<img src="<%= item.image || '/assets/radio_img.png' %>" alt="">
2626
<label>Name<input name="name" value="<%= item.name %>" required></label>
2727
<label>Category<input name="category" value="<%= item.category %>" required></label>
28-
<div class="form-grid"><label>Price<input type="number" name="price" min="0" value="<%= item.price %>" required></label><label>Stock<input type="number" name="stock" min="0" value="<%= item.stock %>" required></label></div>
28+
<div class="form-grid"><label>Price<input type="number" name="price" min="0" value="<%= item.price %>" required></label></div>
2929
<div class="shop-image-upload" data-shop-image-upload><span>Replace product image</span><input type="hidden" name="image" value="<%= item.image || '/assets/radio_img.png' %>" data-shop-image-url><input type="file" accept="image/jpeg,image/png,image/webp,image/gif" data-shop-image-file <%= imageUploadsConfigured ? '' : 'disabled' %>><small data-shop-image-status><%= imageUploadsConfigured ? 'Choose a new image, or leave this empty to keep the current one.' : 'Image uploads need R2 configuration.' %></small><div class="shop-image-preview" data-shop-image-preview></div></div>
3030
<label>Description<textarea name="description" rows="3"><%= item.description %></textarea></label>
3131
<label>Sort order<input type="number" name="sort_order" value="<%= item.sortOrder %>"></label>

views/shop/index.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div class="appbody">
55
<div class="shoptools"><span class="filter active">ALL / <%= products.length %></span><a class="filter" href="/app/shop/orders">ORDER LOG</a></div>
66
<% if (!products.length) { %><div class="empty-screen"><div><h2>NO STOCK ON THIS BAND</h2><p>Check back later.</p></div></div><% } else { %><div class="products">
7-
<% products.forEach((product) => { %><article class="product"><div class="product-img"><img src="<%= product.image || '/assets/radio_img.png' %>" alt=""></div><span class="eyebrow"><%= product.category %> / <%= product.stock %> AVAILABLE</span><h3><%= product.name %></h3><p><%= product.description %></p><% if (product.category === 'radios') { %><p class="product-note">READINESS CHECK REQUIRED</p><% } %><div class="product-foot"><b><%= Number(product.price).toFixed(3) %> Hz</b><form method="post" action="/app/shop/cart/<%= product.id %>"><input type="hidden" name="_csrf" value="<%= csrfToken %>"><input type="hidden" name="quantity" value="1"><button class="add" type="submit" <%= product.stock < 1 ? 'disabled' : '' %>><%= product.stock < 1 ? 'OUT' : 'ADD / +' %></button></form></div></article><% }) %>
7+
<% products.forEach((product) => { %><article class="product"><div class="product-img"><img src="<%= product.image || '/assets/radio_img.png' %>" alt=""></div><span class="eyebrow"><%= product.category %> / AVAILABLE</span><h3><%= product.name %></h3><p><%= product.description %></p><% if (product.category === 'radios') { %><p class="product-note">READINESS CHECK REQUIRED</p><% } %><div class="product-foot"><b><%= Number(product.price).toFixed(3) %> Hz</b><form method="post" action="/app/shop/cart/<%= product.id %>"><input type="hidden" name="_csrf" value="<%= csrfToken %>"><input type="hidden" name="quantity" value="1"><button class="add" type="submit" <%= product.stock < 1 ? 'disabled' : '' %>><%= product.stock < 1 ? 'OUT' : 'ADD / +' %></button></form></div></article><% }) %>
88
</div><% } %>
99
</div>
1010
</section>

0 commit comments

Comments
 (0)