mirror of
https://github.com/bitbaum/petvity.git
synced 2026-12-04 23:08:12 +01:00
The public storefront was a brochure. It shipped a Product/Offer JSON-LD so search engines treat it as purchasable, then the only buy path was "Sign up to buy" -> /register?returnTo=/portal/shop, which both forced registration before a first purchase and dropped the product the visitor came for. Cold traffic could not buy anything. A visitor can now add to cart on the catalogue and the product page, check out in one page with an email and an address, and read their order back from a receipt link. No account anywhere in that path. orders.userId becomes nullable, alongside guestEmail and an unguessable publicToken. A CHECK constraint enforces exactly one of userId / guestEmail, because an order with no reachable buyer cannot be repaired after the fact. The token is the guest's only handle on their order: it is emailed, never listed, and authorises resuming payment and cancelling while pending. Order placement moves to placeOrder() in lib/domain/orders.ts so the authed cart and the guest checkout share one implementation -- the stock reservation and its compensation must not exist twice. setOrderStatus() joins it for the same reason: a cancellation that forgot to restore stock destroys inventory, and that happens when one route remembers and another does not. Two joins were fixed before any UI existed. app/api/orders/seller and app/api/admin/orders inner-joined users on orders.userId; with a nullable column that drops every guest order from the views of the people who have to ship them -- real revenue, invisible, nothing appearing broken. Both are now leftJoin + COALESCE onto the address the buyer typed. Also fixed while reviewing: - toLocale() used `value in LOCALE_CONFIG`, true for inherited keys like "constructor", and the result goes straight into a URL path. - orderShippingSchema accepted any two letters as a country, so "XX" could be stored and printed on an undeliverable label. Checked against a new ISO-3166 SSOT whose names come from Intl.DisplayNames, so all nine locales are translated with no message files to maintain. - Guest input (shippingName, notes) reaches a seller's inbox unauthenticated; email templates now escape it. Guards, not just fixes: locale parity across all nine message files is now a test rather than a convention, and the prod walkthrough places a real guest order and asserts the seller can see the buyer's email -- the check that goes red if anyone reintroduces an inner join there. The public endpoints are deliberately outside PRIVATE_API_PREFIXES; a session guard there would silently delete the feature. They are rate limited per client, and the payment and cancel routes require the order token. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
"use client";
|
|
|
|
import { Plus, Minus, Check, ShoppingCart } from "lucide-react";
|
|
import { useTranslations } from "next-intl";
|
|
import { addToCart, setCartQuantity, useCart } from "@/lib/shop/cart";
|
|
import { MAX_ITEM_QUANTITY } from "@/lib/config/products";
|
|
|
|
/**
|
|
* The one control that puts something in the guest cart.
|
|
*
|
|
* Once an item is in the cart it turns into a stepper in place, so the shopper
|
|
* never has to open a drawer to find out whether the click worked or to buy a
|
|
* second one.
|
|
*/
|
|
export default function AddToCartButton({
|
|
productId,
|
|
outOfStock = false,
|
|
stock = null,
|
|
size = "md",
|
|
}: {
|
|
productId: string;
|
|
outOfStock?: boolean;
|
|
/** Finite stock caps the stepper; null means unlimited. */
|
|
stock?: number | null;
|
|
size?: "sm" | "md";
|
|
}) {
|
|
const t = useTranslations("public");
|
|
const cart = useCart();
|
|
const line = cart.find((l) => l.productId === productId);
|
|
|
|
if (outOfStock) {
|
|
return (
|
|
<span className={size === "sm" ? "badge badge-neutral" : "btn-outline opacity-60"}>
|
|
{t("shopOutOfStock")}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
const ceiling = Math.min(stock ?? MAX_ITEM_QUANTITY, MAX_ITEM_QUANTITY);
|
|
|
|
if (!line) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={() => addToCart(productId)}
|
|
className={size === "sm" ? "btn-primary btn-primary-sm" : "btn-primary"}
|
|
>
|
|
<ShoppingCart className={size === "sm" ? "w-3.5 h-3.5" : "w-4 h-4"} />
|
|
{t("cartAdd")}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="flex items-center gap-2">
|
|
<div className="flex items-center gap-1">
|
|
<button
|
|
type="button"
|
|
aria-label={t("cartDecrease")}
|
|
onClick={() => setCartQuantity(productId, line.quantity - 1)}
|
|
className="w-9 h-9 rounded-full border border-[var(--border)] flex items-center justify-center text-[var(--ink2)] hover:border-[var(--teal)] hover:text-[var(--teal)] transition-colors"
|
|
>
|
|
<Minus className="w-3.5 h-3.5" />
|
|
</button>
|
|
<span className="w-7 text-center text-sm font-semibold text-[var(--ink)]">
|
|
{line.quantity}
|
|
</span>
|
|
<button
|
|
type="button"
|
|
aria-label={t("cartIncrease")}
|
|
disabled={line.quantity >= ceiling}
|
|
onClick={() => setCartQuantity(productId, line.quantity + 1)}
|
|
className="w-9 h-9 rounded-full border border-[var(--border)] flex items-center justify-center text-[var(--ink2)] hover:border-[var(--teal)] hover:text-[var(--teal)] transition-colors disabled:opacity-40 disabled:hover:border-[var(--border)] disabled:hover:text-[var(--ink2)]"
|
|
>
|
|
<Plus className="w-3.5 h-3.5" />
|
|
</button>
|
|
</div>
|
|
<span className="inline-flex items-center gap-1 text-xs font-medium text-[var(--green-text)]">
|
|
<Check className="w-3.5 h-3.5" />
|
|
{t("cartInCart")}
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|