learnshopify.dev/Workspace
code reading

Sarah's endpoint for the orders/create subscription. Trace what it does, and in what order.

import express from "express";
import { enqueue } from "./queue.js";

const app = express();

// Raw body: the bytes must stay untouched for signature checks.
app.use("/webhooks/orders-create", express.raw({ type: "application/json" }));

app.post("/webhooks/orders-create", (req, res) => {
  const topic = req.headers["x-shopify-topic"]; // "orders/create"
  const shop = req.headers["x-shopify-shop-domain"]; // "northwind-coffee.myshopify.com"
  const webhookId = req.headers["x-shopify-webhook-id"];

  // TODO(sarah): HMAC verification before we trust any of this. Next PR.

  // Acknowledge first. Everything below the .end() runs after
  // Shopify already has its answer.
  res.status(200).end();

  const order = JSON.parse(req.body.toString("utf8"));

  enqueue("order-sync", {
    webhookId,
    shop,
    orderId: order.id,
    orderNumber: order.name,
    lines: order.line_items.map((line) => ({
      sku: line.sku,
      quantity: line.quantity,
    })),
  });
});

app.listen(3000);
1. Why does the handler send `res.status(200).end()` before it parses the order?
2. Why does the handler pass `webhookId` into the queue job?
3. Diego also wants each line's live warehouse stock level. Where does it come from?
4. With the app installed on three stores, how does the handler know which one sent the order?
5. What does this subscription buy over polling the Orders API every minute?
0 / 1 task solved