When an order comes in, you need to inform the staff so that they can make ice cream, and when the ice cream is made, you need to notify the customer. Also, if the ice cream melts, you need to notify the customer again if they don't pick it up for a long time.
To achieve this, you need to add a service that sends periodic warnings if a served order is not finished, and a service that sends an alert message to the customer to remind them to pick up the order.
Declare Adapter
First, let's start by connecting an external API or module like an alert. You can connect an api that sends messages or emails, but in this tutorial, we'll create an api that simply prints to the console and connect it.
Modules like service, signal, document should not be directly connected to external systems, but rather created as adapters that are injected. First, let's create an adapter in the /nest folder as follows.
Then, export the module in the /nest/index.ts file.
apps/koyo/nest/index.ts
1export * from"./alarmApi";
Why use the adapter pattern? By injecting external dependencies as adapters rather than directly importing them in services, you gain several benefits:
(1) Testability - you can easily mock or replace the adapter in tests without modifying the service code
(2) Flexibility - you can swap implementations (e.g., switch from console logging to email notifications) without changing the service logic
(3) Separation of Concerns - the service focuses on business logic while adapters handle external interactions
(4) Reusability - the same adapter can be injected and shared across multiple services
Use External API
Now, in order to inject the adapter for the alarm into the server, you need to declare and register the adapter to be injected in the option.ts file.
1import { serve } from"@akanjs/service";
2import { AlarmApi } from"@koyo/nest";
34import * as db from"../db";
56exportclassIcecreamOrderServiceextendsserve(db.icecreamOrder, ({ use }) => ({
7alarmApi: use<AlarmApi>(),
8})) {
9// ...existing code...10}
Query in Document
Now, let's add a service function that sends a warning message if a served order is not finished. For easy testing, let's send a warning message after 15 seconds if the order is served.
However, how can we query only the served icecream orders? To do this, let's create a list query function by utilizing the feature of creating a query based on the given conditions in the icecreamOrder.document file.
By declaring the byStatuses query in IcecreamOrderFilter, you can use list, find, count, sample functions according to the given query conditions. For example, you can use listByStatuses, countByStatuses functions.
Why do we receive an array of statuses instead of a single status? This is useful when we need to query multiple statuses at once. If we think there is a possibility of querying multiple statuses, it is better to receive them as an array and process them.
Use Interval
Now, let's use interval to execute the service every 15 seconds. The interval trigger is a trigger that works internally, and can be declared in IcecreamOrderInternal.