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.
Add dictionary entries with helpful error messages for inventory operations:
apps/koyo/lib/inventory/inventory.dictionary.ts

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 Revert 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 comes the magic - connecting 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. Perfect 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
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. This creates a complete inventory management system!
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:
🔄loadTodaysInventory
Called in useEffect to load inventory data when the component mounts. Shows a loading spinner until data is ready.
🚫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
Finally, put it all together in the main page with both inventory dashboard and order management:
apps/koyo/app/[lang]/page.tsx
🎉 What You've Accomplished:
  • Created a reusable Stock scalar for inventory items
  • Built an Inventory module with daily records
  • Implemented stock usage and refill business logic
  • Connected inventory to order creation flow
  • Created visual dashboard with real-time updates
  • Disabled out-of-stock options in customer UI
In the next tutorial, we'll explore Insight - a powerful feature for aggregating and analyzing 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
Official Akan.js Consulting onAkansoft
Copyright © 2025 Akan.js. All rights reserved.
System managed bybassman