Skip to content
Agentpost robot in sunglasses on a red circleAgentpost

Label stock conditions

Use condition labels to keep samples separate from your other stock at the same place. Keep the labels your app uses in one constant so every write and read spells them the same way. Scopes explains how a label picks a balance.

Before you start

This page's code continues the script of the pages above: run its blocks after theirs, in the same file. To run them as a script of their own, first copy each helper from the page that defines it, and set each other value, such as a key or an ID, to one you already have.

1. Add unlabeled stock at a place

Use api and the dev secret key key for a project that adopted ResourceShark. Create a place and a resource kept by place. Add six items with placeId and leave out condition to use the unlabeled balance. Run the blocks in order on your server.

const conditionExample = crypto.randomUUID();
const { place: sampleShelf } = await api('POST', '/v1/resources/places', key, {
  key: `sample-shelf-${conditionExample}`,
  name: 'Sample shelf',
});
const { resource: sampleStock } = await api(
  'POST',
  '/v1/resources/resources',
  key,
  {
    key: `sample-stock-${conditionExample}`,
    name: 'Coffee bags',
    unit: { code: 'bag', singular: 'bag', plural: 'bags', precision: 0 },
    settings: { time: 'none', identity: 'count', supplyExpiry: 'none' },
    scope: { places: true, holders: false },
    tracking: 'none',
    shortage: 'deny',
  },
);
const unlabeledStockScope = { placeId: sampleShelf.id };
await api(
  'POST',
  '/v1/resources/supply',
  key,
  {
    resourceId: sampleStock.id,
    scope: unlabeledStockScope,
    kind: 'added',
    quantity: 6,
    evidence: { kind: 'receipt', ref: conditionExample },
    sourceKey: `sample-receipt:${conditionExample}`,
  },
  { 'Idempotency-Key': `sample-supply-${conditionExample}` },
);

2. Move stock into an app label

Define sample once in your app’s stockConditions constant. Use it as the destination condition in a transfer of two items. Give both scopes the same placeId to keep the stock at the same place.

const stockConditions = { sample: 'sample' } as const;
await api(
  'POST',
  '/v1/resources/transfers',
  key,
  {
    resourceId: sampleStock.id,
    from: unlabeledStockScope,
    to: { ...unlabeledStockScope, condition: stockConditions.sample },
    quantity: 2,
    sourceKey: `label-samples:${conditionExample}`,
  },
  { 'Idempotency-Key': `transfer-to-sample-${conditionExample}` },
);

3. See what a misspelling does

In this example, send a second transfer with the misspelling smaple. ResourceShark accepts it because it fits the label format. Each spelling gets its own balance. A label with a space in the middle answers invalid_request.

const misspelledSample = 'smaple';
await api(
  'POST',
  '/v1/resources/transfers',
  key,
  {
    resourceId: sampleStock.id,
    from: unlabeledStockScope,
    to: { ...unlabeledStockScope, condition: misspelledSample },
    quantity: 1,
    sourceKey: `misspell-sample:${conditionExample}`,
  },
  { 'Idempotency-Key': `transfer-to-smaple-${conditionExample}` },
);

Check it worked

Read the three balances at the same place. The unlabeled balance has three items, sample has two, and smaple has one.

async function stockAtCondition(condition?: string) {
  const query = new URLSearchParams(unlabeledStockScope);
  if (condition !== undefined) query.set('condition', condition);
  const { balance } = await api(
    'GET',
    `/v1/resources/resources/${sampleStock.id}/balance?${query}`,
    key,
  );
  return balance.onHand;
}

console.log(
  await stockAtCondition(),
  await stockAtCondition(stockConditions.sample),
  await stockAtCondition(misspelledSample),
); // 3 2 1

To fix a label on existing stock, transfer it from the misspelled condition to the intended one. The stock reference has the transfer request, and the availability reference has the balance read.