# Several shops on one screen

Source: https://developers.weerom.com/docs/use-cases/several-shops

> One key for each shop and one loop over them, for an owner with two shops or a group's dashboard.

**For** an owner with two or more shops on WeerOm, or a group or franchise that wants one dashboard with every shop's day on it.

A key belongs to one shop. So the dashboard holds one read key per shop and runs the same requests for each: today's revenue, today's bookings, the payment methods. It shows the shops side by side and adds them up.

| What | You need |
| --- | --- |
| Keys | **Read key** One read key for each shop, each made by an administrator of that shop. |
| Endpoints | `GET /shop`, `GET /payments`, `GET /bookings`, for each key. |
| Dashboard | Your own page or tool that shows the numbers. |

Each shop must be on the Premium plan. A key of a shop that leaves Premium answers 403 `plan_required`, and the loop should show that shop as paused instead of stopping.

## The loop

```js title="shops.js"
const BASE = "https://api.weerom.com/v1";

// One read key for each shop, from your secret store.
const KEYS = [process.env.WEEROM_KEY_NOORD, process.env.WEEROM_KEY_ZUID];

async function weerom(key, path) {
  const response = await fetch(`${BASE}${path}`, {
    headers: { Authorization: `Bearer ${key}` },
  });
  const json = await response.json();
  if (!response.ok) throw new Error(`${response.status} ${json.error.code}`);
  return json;
}

const addDays = (day, days) => {
  const date = new Date(`${day}T00:00:00Z`);
  date.setUTCDate(date.getUTCDate() + days);
  return date.toISOString().slice(0, 10);
};

async function today(key) {
  const shop = await weerom(key, "/shop");
  const dayIn = new Intl.DateTimeFormat("en-CA", { timeZone: shop.time_zone });
  const day = dayIn.format(new Date());
  // A day wider on both sides, then keep today on this shop's own clock.
  const from = `${addDays(day, -1)}T00:00:00Z`;
  const until = `${addDays(day, 2)}T00:00:00Z`;

  const totals = { bookings: 0, byMethod: {}, cancelled: 0, revenueCents: 0, tipsCents: 0 };

  let cursor = null;
  do {
    const query = new URLSearchParams({ limit: "100", paid_after: from, paid_before: until });
    if (cursor) query.set("cursor", cursor);
    const page = await weerom(key, `/payments?${query}`);
    for (const payment of page.data) {
      if (dayIn.format(new Date(payment.paid_at)) !== day) continue;
      totals.revenueCents += payment.amount_cents;
      totals.tipsCents += payment.tip_cents;
      const method = payment.method ?? "not noted";
      totals.byMethod[method] = (totals.byMethod[method] ?? 0) + payment.amount_cents;
    }
    cursor = page.has_more ? page.next_cursor : null;
  } while (cursor);

  cursor = null;
  do {
    const query = new URLSearchParams({ limit: "100", starts_after: from, starts_before: until });
    if (cursor) query.set("cursor", cursor);
    const page = await weerom(key, `/bookings?${query}`);
    for (const booking of page.data) {
      if (dayIn.format(new Date(booking.starts_at)) !== day) continue;
      if (booking.status === "cancelled") totals.cancelled += 1;
      else totals.bookings += 1;
    }
    cursor = page.has_more ? page.next_cursor : null;
  } while (cursor);

  return { currency: shop.currency, name: shop.name, ...totals };
}

const shops = await Promise.allSettled(KEYS.map(today));
for (const result of shops) {
  if (result.status === "rejected") {
    console.log(`A shop did not answer: ${result.reason.message}`);
    continue;
  }
  const shop = result.value;
  console.log(`${shop.name}: €${shop.revenueCents / 100}, ${shop.bookings} bookings, ${shop.cancelled} cancelled`);
}
```

The shops run side by side with `Promise.allSettled`, so one shop with a removed key does not hide the others. Each shop answers in its own `currency` and its own `time_zone`. Shops in Belgium and Portugal have different clocks, and the loop works out "today" for each.

## How often

Every 5 minutes for a screen on the wall. Each shop costs 3 requests a run on a normal day: the shop, one page of payments, one page of bookings. The limit of 120 a minute is per key, so ten shops do not share one budget.

## Limits

- **One key per shop.** There is no group key that reads several shops. Someone with administrator rights in each shop makes that shop's key, and each one can be removed on its own.
- **Names do not line up.** Staff and service ids belong to one shop. The same person working in two shops is two staff members with two ids. Match them by name if you must.
- **Webhooks name no shop.** A webhook message carries the booking or the payment, not the shop it came from. Give each shop its own address, such as `https://example.com/weerom/noord` and `https://example.com/weerom/zuid`, and check each message with that shop's secret. See [Webhooks](https://developers.weerom.com/docs/webhooks).
- **Today's VAT rate.** For a dashboard that splits out VAT, `net_cents` and `vat_cents` use each shop's rate as it is set today.
