Augur

Pricing

Fetching, caching, and displaying customer-specific pricing.

Pricing Guide

The pricing service calculates customer-specific prices from P21 pricing libraries, quantity breaks and customer rules. You send a customer ID, item ID and quantity, and it returns the price data (TPriceData).

Getting a Price

In a component

import { useQuery } from "@tanstack/react-query";
import { derivePrice } from "@simpleapps-com/augur-utils";
import { q } from "@/lib/augur-query";
import { Price } from "@/lib/price";

function ItemPrice({ itemId, customerId }: { itemId: string; customerId: number }) {
  const { data: priceData, isLoading } = useQuery(q.pricing.priceEngine.list({ itemId, customerId, quantity: 1 }));
  if (isLoading || !priceData) return <Price loading />;
  const price = derivePrice({ priceData, quantity: 1 });
  return <Price value={price.state === "priced" ? price.unitPrice : 0} />;
}

q is the server query proxy. derivePrice (augur-utils) applies quantity breaks and says whether the item is priced; an unpriced item shows the Price component's zeroLabel ("Call for Price").

On the server

import { getSiteActions } from "@simpleapps-com/augur-server";

const priceData = await getSiteActions().pricing.getItemPrice("ITEM-123", customerId, 1);

getItemPrice(itemId, customerId?, quantity = 1) falls back to the site's defaultCustomerId (or DEFAULT_CUSTOMER_ID), sends edgeCache: 2 and caches the result in Redis for an hour (redisTtl).

API route

For non-React consumers, expose pricing as a route:

// app/api/pricing/[itemId]/route.ts
import { getSiteActions } from "@simpleapps-com/augur-server";

export async function GET(req: Request, { params }: { params: Promise<{ itemId: string }> }) {
  const { itemId } = await params;
  const customerId = await customerIdFromSession(req); // your session lookup
  const price = await getSiteActions().pricing.getItemPrice(itemId, customerId);
  return Response.json(price, {
    headers: { "Cache-Control": "max-age=600, stale-while-revalidate=3600" },
  });
}

Bulk Pricing

For search results and listings, price many items in one call:

const prices = await getSiteActions().pricing.batchGetItemPrices(["ITEM-1", "ITEM-2", "ITEM-3"], customerId, 1);
// prices["ITEM-1"] is ITEM-1's TPriceData; an item whose lookup failed is left out

Fallback to single lookups

In product cards, use the batch result when it is there and fetch singly otherwise:

function ProductCard({ item, batchedPriceData }) {
  const { data: single } = useQuery({
    ...q.pricing.priceEngine.list({ itemId: item.itemId, customerId, quantity: 1 }),
    enabled: !batchedPriceData,
  });
  const priceData = batchedPriceData ?? single;
  const price = priceData ? derivePrice({ priceData, quantity: 1 }) : null;
  return <Price value={price?.state === "priced" ? price.unitPrice : 0} uom={item.unitOfMeasure} />;
}

Cart Pricing

useCartPricing() (augur-hooks) prices every cart line at its own quantity, so quantity breaks apply, and returns each line's price and the subtotal. It reads the customer from the provider's auth, else site.defaults.customerId.

Displaying Prices

Use createPrice from augur-web for consistent formatting:

// lib/price.ts
import { createPrice } from "@simpleapps-com/augur-web/price";

export const { Price, formatPrice } = createPrice({
  precision: 2,
  zeroLabel: "Call for Price",
  classNames: {
    uom: "text-sm uppercase text-primary",
    separator: "text-primary",
  },
});

Price Component

<Price value={unitPrice} uom="EACH" />
<Price loading />
// Renders "$42.50/EACH", "Call for Price" for 0, or a skeleton

formatPrice Function

For cart totals, line items and any formatted string:

formatPrice(42.5); // "$42.50"
formatPrice(unitPrice, { quantity: 5 }); // the extended price

In a server component, use createFormatPrice from @simpleapps-com/augur-web/price-format, which has no React dependency.

useFormatPrice Hook

The hook-based formatter from augur-hooks reads its locale, currency, precision and zero label from the site config:

import { useFormatPrice } from "@simpleapps-com/augur-hooks";

const { formatPrice } = useFormatPrice();

Money Math

Every product or sum of prices MUST go through augur-utils' cleanMoney, and rounding to cents for a payload MUST use roundHalfAway(value, 2) (why).

Server-Side Prefetching

// app/items/[itemId]/page.tsx
import { dehydrate, HydrationBoundary } from "@tanstack/react-query";
import { createAugurOptions, getQueryClient } from "@simpleapps-com/augur-hooks/server";
import { site } from "@/lib/augur-site";

export default async function ItemPage({ params }: { params: Promise<{ itemId: string }> }) {
  const { itemId } = await params;
  const queryClient = getQueryClient();
  await queryClient.prefetchQuery(
    createAugurOptions(site.client).pricing.priceEngine.list({ itemId, customerId, quantity: 1 }),
  );

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <ItemDetail itemId={itemId} />
    </HydrationBoundary>
  );
}

The same call through the client proxy builds the same key, so the price hydrates without a second fetch.

Caching Strategy

Layer TTL Notes
Cloudflare edge 2 hours edgeCache: 2 from the site's pricing actions
Redis 1 hour Pricing actions' redisTtl; proxy reads use the tier's
React Query 1 min stale, 5 min gc The semiStatic tier pricing falls in
API route 10 min + 1 hour SWR HTTP cache headers