chore(seed): seed an active flash sale
Add one active session for the demo shop with a discounted activity item on one of its SKUs, idempotent by English label, so the live mall has a discoverable flash sale to check out.
This commit is contained in:
@@ -368,6 +368,47 @@ const POINTS_PRODUCTS = [
|
||||
console.log(`points products ready: ${POINTS_PRODUCTS.length} (${created} new)`);
|
||||
}
|
||||
|
||||
// 7. an active flash sale for the demo shop (idempotent by English label).
|
||||
{
|
||||
const owner = ownerTokens.get("demo-store");
|
||||
const listed = await call("GET", "/shop/flash-sales", { token: owner });
|
||||
if (listed.status !== 200) fail("list flash sales", listed);
|
||||
const known = new Set(listed.data.map((s) => s.label.en));
|
||||
if (!known.has("Weekend flash sale")) {
|
||||
const products = await call("GET", "/shop/products?per_page=100", { token: owner });
|
||||
if (products.status !== 200) fail("list shop products", products);
|
||||
const sku = (products.data.items ?? [])
|
||||
.flatMap((p) => p.skus ?? [])
|
||||
.find((s) => s.active && s.stock > 0);
|
||||
if (!sku) fail("find a flash-sale SKU", { status: 0, data: "no active SKU" });
|
||||
|
||||
const now = Date.now();
|
||||
const session = await call("POST", "/shop/flash-sales", {
|
||||
token: owner,
|
||||
body: {
|
||||
label: { en: "Weekend flash sale", zh: "周末秒杀" },
|
||||
starts_at: new Date(now - 3600 * 1000).toISOString(),
|
||||
ends_at: new Date(now + 7 * 24 * 3600 * 1000).toISOString(),
|
||||
enabled: true,
|
||||
},
|
||||
});
|
||||
if (session.status !== 201) fail("seed flash session", session);
|
||||
|
||||
const item = await call("POST", `/shop/flash-sales/${session.data.id}/items`, {
|
||||
token: owner,
|
||||
body: {
|
||||
sku_id: sku.id,
|
||||
sale_price_minor: Math.max(1, Math.round(sku.price_minor * 0.6)),
|
||||
currency: sku.currency,
|
||||
reserved_stock: 20,
|
||||
per_customer_limit: 2,
|
||||
},
|
||||
});
|
||||
if (item.status !== 201) fail("seed flash item", item);
|
||||
}
|
||||
console.log("flash sale ready");
|
||||
}
|
||||
|
||||
console.log("\nDemo data seeded.");
|
||||
console.log(" platform admin : admin@vmall.local / admin1234 (apps/admin :3002)");
|
||||
console.log(" shop owner : shop@vmall.local / shop12345 (apps/shop-admin :3001)");
|
||||
|
||||
Reference in New Issue
Block a user