YouBothAgent▾
You — Business rules and flows you own. Read these yourself.
Both — Know the idea; your agent follows the details.
Agent — Conventions and references your agent follows. Look up as needed.
Introduction▾
Tutorials▾
Core Concepts▾
Architecture▾

Use Setting Module

Now the ice cream shop can receive and process orders, and with many customers coming in, it's easy to run out of stock. Let's find out how to handle this situation.
You need to add a service that checks the remaining ice cream and toppings every morning, sets the inventory status in the system, and automatically disables orders when the inventory is depleted.

Create Scalar

First, let's create a Scalar to represent individual stock items. Think of a Scalar as a reusable data building block - like Lego pieces that can be combined into larger structures. Unlike a full Model that has its own database collection, a Scalar is embedded within other models. In our case, the Stock scalar will be used inside the Inventory model.
Use the CLI to generate the scalar structure:
Terminal
Now define the Stock scalar with the item type and quantity tracking:
apps/koyo/lib/__scalar/stock/stock.constant.ts
Let's understand the Stock scalar structure:
  • StockType: An enum combining yogurt ice cream with all available toppings. This allows tracking inventory for all product types in one system.
  • totalQty / currentQty: Track both the starting amount and current remaining quantity. This helps calculate usage and identify when restocking is needed.
Add dictionary entries for the scalar. Notice how we reuse the topping translations from the icecreamOrder dictionary:
apps/koyo/lib/__scalar/stock/stock.dictionary.ts

Create Inventory

Now let's create an Inventory module to manage daily stock records. Think of this like the daily inventory sheet that a shop manager fills out each morning - it contains all the stock items and their quantities for that specific day.
Create the inventory module using the CLI:
Terminal
Define the Inventory model with embedded Stock scalars:
apps/koyo/lib/inventory/inventory.constant.ts
Key features of the Inventory model:
stocks: field([Stock])
An array of Stock scalars. This is where our reusable Scalar shines - we embed multiple Stock objects directly in the Inventory document.
at
A date field that defaults to midnight of the current day. This allows creating one inventory record per day and easily finding today's inventory. It is listed in LightInventory too, so a list card can show it.
Add dictionary entries with helpful error messages for inventory operations:
apps/koyo/lib/inventory/inventory.dictionary.ts
The generated Unit and Template still read the name field the module started with. Inventory records come from refill, not from a form, so leave the Template empty and show the date on the Unit card:
apps/koyo/lib/inventory/Inventory.Unit.tsx
apps/koyo/lib/inventory/Inventory.Template.tsx

Business Logic

Now let's implement the core business logic for inventory management. Just like a real ice cream shop needs rules for using stock and refilling supplies, we need to encode these operations in our document and service layers.
First, define the document methods for stock operations and daily inventory generation:
apps/koyo/lib/inventory/inventory.document.ts
Let's understand the key document methods:
useStock / useStocks
Decrements stock quantity when orders are placed. Validates that stock exists and has sufficient quantity, throwing Err errors with dictionary messages if not.
refill
Restocks all items to their default quantities. Smart enough to only add what's needed - if you have 3 of 10 toppings left, it adds 7 more.
generateTodaysInventory
Automatically creates a new inventory record for today if one doesn't exist. Returns existing inventory if already created - ensuring one record per day.
Now create the service layer to expose these operations:
apps/koyo/lib/inventory/inventory.service.ts

Connect Service

Now let's connect the inventory system to our existing ice cream order flow. When a customer places an order, the system should automatically deduct the used ingredients from inventory. This is like how a real POS system updates stock counts in real-time as sales are made.
Inject the InventoryService into IcecreamOrderService and use the _preCreate hook to deduct stock:
apps/koyo/lib/icecreamOrder/icecreamOrder.service.ts
Key aspects of this integration:
  • service<srv.InventoryService>(): Dependency injection allows IcecreamOrderService to access InventoryService methods
  • _preCreate: A lifecycle hook that runs before creating a new order. Used for validation and side effects like inventory deduction.
  • Usage Calculation: The order size determines yogurt usage, and each topping uses 1 unit from inventory.

Connect Signal

To allow the frontend to interact with inventory, we need API endpoints. Staff should be able to view today's inventory and refill stock when running low. Let's create the signal endpoints and frontend store.
Create the inventory endpoints:
apps/koyo/lib/inventory/inventory.signal.ts
refillTodaysInventory changes data, so it is a mutation, not a query. Both endpoints name their guards; with no sign-in yet, they are Public like the order actions.
Add dictionary entries for the endpoints:
apps/koyo/lib/inventory/inventory.dictionary.ts
Now create the frontend store to manage inventory state:
apps/koyo/lib/inventory/inventory.store.ts

Interact on UI

Now let's bring everything together in the UI. The customer-facing order form needs to check inventory and disable options that are out of stock. Staff also need a dashboard to monitor inventory levels and refill when needed.
First, update the order template to check inventory before displaying options:
apps/koyo/lib/icecreamOrder/IcecreamOrder.Template.tsx
Key features of the inventory-aware template:
st.use.todaysInventory()
The form only reads the store, and shows a spinner until the value is there. The Inventory zones you mount below are what call loadTodaysInventory - a mount-time fetch inside a Template is what akan quality ssr reports as client-mount-load.
Out of Stock Check
If yogurt ice cream is completely out of stock, shows a friendly message instead of the form. No point ordering if we can't make it!
disabled: !isInStock
Each size and topping option checks if sufficient stock exists. Disabled options are grayed out but still visible, so customers know what's normally available.
Add the isInStock helper method to the Inventory constant:
apps/koyo/lib/inventory/inventory.constant.ts
Now let's create utility components for staff to refill inventory:
apps/koyo/lib/inventory/Inventory.Util.tsx
Create a visual dashboard showing stock levels with color-coded status indicators:
apps/koyo/lib/inventory/Inventory.View.tsx
Add helper methods to the Stock scalar for status calculation:
apps/koyo/lib/__scalar/stock/stock.constant.ts
Create a Zone component for real-time inventory monitoring:
apps/koyo/lib/inventory/Inventory.Zone.tsx
Today draws the dashboard; Sync only keeps today's inventory loaded. The kiosk's order page renders the same Template, so mount Sync there, or its form waits on a spinner forever:
apps/koyo/page/icecreamOrder/new.tsx
Finally, put it all together in the main page with both inventory dashboard and order management:
apps/koyo/page/_index.tsx
In the next tutorial, we'll explore Insight, which aggregates and analyzes data across your models. This will allow you to create analytics dashboards and gain business intelligence from your ice cream shop data.

Released under the MIT License

Connect your AI to these docs

MCPhttps://akanjs.com/mcp
Copyright © 2026 Akan.js All rights reserved.System managed bybassman