cancel
Showing results for 
Search instead for 
Did you mean: 

CAP typescript: how do I get draft data from draft enabled entity?

shf
Explorer
0 Kudos
673

Hello,

I'm using CAP with typescript and I have defined a draft-enabled entity SystemKinds. Cds-Typer is installed and Typescript types will be generated and updated each time I change my entity definition.

If I send a GET request with SystemKinds , I can see all properties of entity SystemKinds plus the draft properties IsActiveEntity, HasActiveEntity,HasDraftEntity. On sqlite database, I also see the auto-generated tables AdminConfigServices_SystemKinds_drafts and the DRAFT_DraftAdministrativeData with its properties e.g. InProcessByUser. Both tables are linked via DraftUUID.

Now, I like to display InProcessByUser in Fiori Elements list report and the date of last modification. Therefore, I try to extend my entity with these two properties. I don't know how to get access to these properties inside a READ handler. In other words, how do I get access to these auto-generated tables ? The approach with SystemKind.drafts does not work as documented in Fiori Support | capire (cloud.sap). Typescript does not know SystemKind.drafts or SystemKinds.drafts. It is not available in the type definition in folder @CDS-models.

Here is my AdminConfiguration.ts:

 

 

 

 

 

import cds from '@sap/cds';
import { SystemKind, SystemKinds }  from '#cds-models/AdminConfigurationService';

class AdminConfigurationService extends cds.ApplicationService {
  /** Registering custom event handlers */
  init() {

    // extract entities
    const { SystemKinds } = this.entities

   // Extend draft information
    this.after('READ', SystemKinds, async( response ) => { 
     // here, i like to extend response with the properties LastChangedByUser from DRAFT_DraftAdministrativeData
    // and HasDraftEntity from AdminConfigurationService_SystemKinds_drafts
    }
}​

 

 

 

 

Does anyone know how I can read draft-related administrative data?
Yours
Stephan

Accepted Solutions (0)

Answers (2)

Answers (2)

shf
Explorer
0 Kudos

Hello,

my packag.json encounters the following:

 "devDependencies": {
        "@cap-js/cds-typer": ">=0.1",
        "@cap-js/cds-types": "^0.6",
        "@cap-js/sqlite": "^1",
        "@sap/ux-specification": "UI5-1.129",
        "@types/node": "^20",
        "@sapui5/types": "^1.116",
        "@ui5/cli": "^4.0.8",
        "typescript": "^5"
    
}

 

If I do the following, I will get the DraftAdministrativeData as follows:

let AvailEntities: cds.entity[] = [SystemKinds, SystemKinds.drafts];
srv.after('READ', AvailEntities, async (response) => {
if (!response) return;
....
const currentUser = cds.context?.user.id;
.....
response.forEach((singleEntity: any) => {
....
let IsCreatedByMe : Boolean = singleEntity.DraftAdministrativeData.DraftIsCreatedByMe;
let InProcessBy : String = singleEntity.DraftAdministrativeData.InProcessByUser;
});
});

For the SystemKinds.drafts , there is no TypeScript type auto-generated. The draft administrative data can be accessed by the entity's DraftAdministrativeData property.

 

shf
Explorer
0 Kudos

Yes, you are right: 

const draftData = await SELECT.from(SystemKinds.drafts);
is giving me all properties of my entity SystemKinds plus property "HasActiveEntity" and "DraftAdministrativeData_draftUUID" of table AdminConfigServices_SystemKinds_drafts but not the properties "HasDraftEntity" and "IsActiveEntity". But this is ok and sufficiently. 
 
I was confused because of getting error messages in VSCODE around "SystemKinds.drafts" (red underlined) . If I hover over "SystemKinds.drafts", I will get typescript error, but I get no program dumps. Thus, I thought if typescript has an issue, the program will crash... 
 
But I'm still missing typescript type definition for SystemKinds.drafts in one of the .ts files in the @CDS-models folder.
 
Yours,
Stephan
 
 
0 Kudos

Hi Stephen,

I saw this git commit today on the cds-type package and felt this would be the relevant update for you.

The draft properties are added to the type definition. 

https://github.com/cap-js/cds-typer/pull/354

 

Regards,
Arun K

shf
Explorer
0 Kudos
Hi Arun, I have updated NPM with the newest version of @CDS-typer . There are not all properties available: InProcessByUser is missing.