Code examples · SAP BTP · Advanced
SAP CAP Node.js: a bound action to approve an order and a function for the open total
The problem
An order service must let users create orders, but the status must never be set directly.
How we solve it
- 1Data modeldb/schema.cds
- 2Servicesrv/shop-service.cds
- 3Handlersrv/shop-service.js
- 4Call itHTTP
The result
An order is created as OPEN, approved once, refused the second time with 409, and the open total reflects only open orders.
1. The requirement
An order service must let users create orders, but the status must never be set directly. Only an Approve button may change an open order to approved, it must refuse orders that are not open, and another call must return the total amount of all open orders.
2. How we design it
The data model has an order with the status field. In the service the status is read-only, so a normal create or update cannot change it. The change happens in a bound action approve, which is an operation on one order, and an unbound function openTotal that works on the whole set. Both are implemented in a JavaScript class that extends the CAP application service. The action reads the targeted order, checks its status, updates it and returns it.
Why we decided it this way
- 1A bound action for a state changeApproving is a business event on one order. An action can check rules and returns the updated order, while a PATCH of the status field cannot.
- 2@readonly on the status fieldIt keeps clients from writing the status through the normal create and update calls.
- 3req.subject to find the targeted orderThe framework gives a reference to the one order the action was called on, so the handler does not parse the URL.
- 4req.reject with a status codeA clear HTTP status (404, 409) tells the caller what was wrong without a stack trace.
Before you start
- A current Node.js LTS version (22 or later for the current @sap/cds 10; check the CAP release notes for the version you install) and the CAP command line: npm install -g @sap/cds-dk. Create the project with cds init shop.
3. The code, step by step
Step 1 of 4: Model the order
An order with an ID, administrative fields and a status that starts as OPEN.
// Order entity
namespace shop;
using { cuid, managed } from '@sap/cds/common';
entity Orders : cuid, managed {
customer : String(80);
amount : Decimal(15, 2);
status : String(10) default 'OPEN';
}
Part by part (plain text)
- Order entity. cuid adds a generated UUID key called ID. managed adds createdAt, createdBy, modifiedAt and modifiedBy and fills them automatically. The status gets the default value OPEN, so every new order starts open.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
If it does not work
4. The final outcome
An order is created as OPEN, approved once, refused the second time with 409, and the open total reflects only open orders.
What you should see
POST Orders -> 201, status OPEN POST Orders(...)/approve -> 200, status APPROVED POST Orders(...)/approve -> 409 Order is already APPROVED GET openTotal() -> value = total amount of OPEN orders
Try it and check the result
- Run cds watch in the project folder. Open http://localhost:4004 and the Orders link to see the service.
- Run the three requests in order. The second approve call must return 409.
- Try to send a PATCH with { "status": "APPROVED" } to an open order: the status stays OPEN because of @readonly.
- Call approve with an ID that does not exist: the answer is 404.
Common mistakes
- Do not rely on @readonly alone for authorization. Who may approve is a separate rule: add @requires or @restrict to the action.
- Check the status in the handler even if the UI hides the button. A direct HTTP call bypasses the UI.
- Read and update in two statements can race when two users approve at once. For critical flows add a condition to the update (where status = 'OPEN') and check the number of affected rows.
- Template literals inside the handler need the backticks of JavaScript. A normal quote will not insert the status into the message.