I have created a simple project to showcase the issue that I am having at repo: draft-concern.
In this example repo, I have created a very simple service with entity project that has been annotated with @odata.draft.enabled. In the same service, I have created an additional action (createDraft) to add my custom handler code.
using { Customers as schemaCustomers } from '../db/schema';
service CustomerService{
entity Customers as projection on schemaCustomers;
annotate Customers with @odata.draft.enabled;
action createDraft() returns {ID: UUID};
}
If I run a simple post command to the Customer entity set, a draft is created as expected:
curl -X POST -H "Content-Type:application/json" -d '{}' http://localhost:4004/customer/Customers
{"@odata.context":"$metadata#Customers/$entity","ID":"1a8b20eb-c044-422e-a137-d7c724c6f91d","CustomerID":null,"CustomerName":null,"IsActiveEntity":false,"HasActiveEntity":false,"HasDraftEntity":false}%
Specifically, you will see that the IsActiveEntity field is set to false since the draft has not been activated yet.
The issue I am running into in my project is when I try to replicate this behavior in a custom handler. In this case I have a very simple handler for the "createDrafts" action defined on the service.
const cds = require("@sap/cds")
module.exports = cds.service.impl( async (srv) => {
srv.on('createDraft', async (req) => {
const { Customers } = srv.entities
const { ID } = await srv.post(Customers, {})
return { ID }
})
})
When I call this action against the service, the behavior is not as expected:
curl -X POST -H "Content-Type:application/json" -d '{}' http://localhost:4004/customer/createDraft
{"@odata.context":"$metadata#CustomerService.return_CustomerService_createDraft","ID":"9d552f07-275c-4a7f-a79f-0fb7e59d94f9"}%
curl -X GET http://localhost:4004/customer/Customers
{"@odata.context":"$metadata#Customers","value":[{"ID":"9d552f07-275c-4a7f-a79f-0fb7e59d94f9","CustomerID":null,"CustomerName":null,"IsActiveEntity":true,"HasActiveEntity":false,"HasDraftEntity":false}]}%
Here you will see that the IsActiveEntity field has been set to true even though the draft has not been activated.
While in this example, I am using the post convenience method for a CAP service in my custom handler, the same results have occurred regardless of the way I have attempted a post on the Customer entity set via a custom handler.
This appears broken to me due to the fact that an direct POST command to the Customers entity set is not behaving in the same manner as the post method within the custom action of the service.
Any help on how to properly mimic the POST command within a custom handler where a draft entry gets created (with IsActiveEntity is set to false) would be truly appreciated!
cds -v
@sap/cds: 6.3.2
@sap/cds-compiler: 3.4.2
@sap/cds-dk: 4.1.5
@sap/cds-foss: 4.0.0
@sap/cds-mtx: -- missing --
Node.js: v16.18.1
draft-concern: 1.0.0
home: /Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds<br>
Request clarification before answering.
Hi the_mason_abides,
You are not using the .send API correctly. As described in the documentation you mentioned, you need to provide either data or query.
As I mention in the link I gave you, we don't recommend rewriting our draft implementation and don't provide any API to support it. You can use draft events to add your custom logic as described here https://cap.cloud.sap/docs/node.js/app-services#draft, but if you decide to reimplement our out-of-the-box draft handling - we cannot assist you.
Best regards,
Olena
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Olena,
Thanks again for your response. I am definitely not trying to re-implement the out-of-the-box draft handling. As noted in my original post, when I call the draft handling via a POST call from an external HTTP client, it behaves exactly as expected. All I am wanting to do is trigger the same POST behavior, but from within my custom code.
With regard to the srv.send API, inn the example above, my data set is empty (empty brackets). I did this as this is allowed for a POST from an external HTTP client (as demonstrated in my original post). If I pre-fill data for the srv.send call:
const cds = require("@sap/cds")
module.exports = cds.service.impl(async (srv) => {
srv.on("createDraft", async (req) => {
const { Customers } = srv.entities
//const { ID } = await srv.post(Customers, {})
const generatedID = cds.utils.uuid()
const customerID = 1234
const customerName = "Example Customer"
const ID = await srv.send("POST", Customers, { ID: generatedID, CustomerID: customerID, CustomerName: customerName })
return { ID }
})
})
I still receive the same error message from crud.js:
[INTERNAL ERROR] TypeError: Cannot read properties of undefined (reading 'INSERT')
at ApplicationService.<anonymous> (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/common/generic/crud.js:46:32)
at next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:36)
at ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:79:6)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ApplicationService.<anonymous> (/Users/stevenmason/dev/cap/draft-concern/srv/customer-service.js:12:16)
at async next (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:75:17)
at async ApplicationService.handle (/Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/lib/srv/srv-dispatch.js:73:10)
at async /Users/stevenmason/dev/cap/draft-concern/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/handlers/action.js:52:16
Am I missing something with regard to providing data to the srv.send call that is causing this issue?
Thanks again,
Steven
| User | Count |
|---|---|
| 7 | |
| 7 | |
| 5 | |
| 4 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.