40 lines
2.0 KiB
JavaScript
40 lines
2.0 KiB
JavaScript
// Seeds the local dev stack with sample users, orders and submissions (run via `src/dev/devenv.sh seed`).
|
|
// Goes through the public API + Mailpit like a real user, so it only works against a stack with Mailpit,
|
|
// and refuses any BASE_URL that is not localhost.
|
|
import { BASE_URL, ADMIN_EMAIL, api, createOrder, signIn, submit } from "../e2e/lib.mjs";
|
|
|
|
if (!/^https?:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/.test(BASE_URL)) {
|
|
console.error(`Refusing to seed ${BASE_URL}: dev seed data is for local stacks only.`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const SEED_TITLE = "Burger Friday";
|
|
|
|
const admin = await signIn(ADMIN_EMAIL);
|
|
const existing = await api("/api/orders/me?limit=100", { userId: admin.userId });
|
|
if (existing.orders?.some((o) => o.title === SEED_TITLE)) {
|
|
console.log("Seed data already present, nothing to do.");
|
|
process.exit(0);
|
|
}
|
|
|
|
const [alice, bob, carol] = await Promise.all(["alice", "bob", "carol"].map((n) => signIn(`${n}@example.com`)));
|
|
|
|
// Open order with a mix of submission states
|
|
const open = await createOrder(admin.userId, { title: SEED_TITLE, description: "Order by **11:30**, pickup at noon." });
|
|
const a = await submit(alice.userId, open.id, { burger: "Classic", sides: ["Fries"] });
|
|
await submit(bob.userId, open.id, { burger: "Veggie", sides: ["Fries", "Side salad"] });
|
|
await submit(carol.userId, open.id, { burger: "Cheeseburger" });
|
|
await api(`/api/orders/${open.id}/admin/submissions/${a.id}/status`, {
|
|
userId: admin.userId,
|
|
method: "PUT",
|
|
body: { accepted: true, paid: true },
|
|
});
|
|
|
|
// Closed order, owned by alice, where the admin participated
|
|
const closed = await createOrder(alice.userId, { title: "Pizza Wednesday", description: "Done." });
|
|
await submit(admin.userId, closed.id, { burger: "Classic" });
|
|
await submit(bob.userId, closed.id, { burger: "Veggie", sides: ["Side salad"] });
|
|
await api(`/api/orders/${closed.id}/admin/status`, { userId: alice.userId, method: "PUT", body: { closed: true } });
|
|
|
|
console.log(`Seeded. Admin ${ADMIN_EMAIL}, users alice/bob/carol@example.com. Open ${BASE_URL}/order/${open.id}`);
|