namespace my.bookshop;
using { managed ,cuid} from '@sap/cds/common';
entity Books: cuid,managed {
booknumber : Integer;
title : String;
stock : Integer;
repositoryId : String;
folderId: String;
}
using my.bookshop as my from '../db/data-model';
service CatalogService {
entity Books as projection on my.Books;
}
using {CatalogService as my} from './cat-service';
annotate my.Books with @odata.draft.enabled;
annotate my.Books with @(UI : {
SelectionFields : [ booknumber, title ],
LineItem : [
{
Value : booknumber,
Label : 'ID'
},
{
Value : title,
Label : 'Title'
}
],
Facets : [{
$Type : 'UI.CollectionFacet',
ID : 'General',
Label : 'Book Info',
Facets : [{
$Type : 'UI.ReferenceFacet',
Target : '@UI.FieldGroup#Main',
Label : 'Main Facet'
}]
}],
FieldGroup #Main : {Data : [
{Value : booknumber},
{Value : title}
]}
});
const cds = require('@sap/cds')
const axios = require('axios').default;
const FormData = require('form-data');
const VCAP_SERVICES = JSON.parse(process.env.VCAP_SERVICES);
// Access the sdm credentials (Document management, Integration option instance)
const sdmCredentials = VCAP_SERVICES.sdm[0].credentials
const _fetchJwtToken = async function (oauthUrl, oauthClient, oauthSecret) {
// This is to get the oauth token , which is used to create the folder ID
return new Promise((resolve, reject) => {
const tokenUrl = oauthUrl + '/oauth/token?grant_type=client_credentials&response_type=token'
const config = {
headers: {
Authorization: "Basic " + Buffer.from(oauthClient + ':' + oauthSecret).toString("base64")
}
}
axios.get(tokenUrl, config)
.then(response => {
resolve(response.data.access_token)
})
.catch(error => {
reject(error)
})
})
}
// This is to create a folder in the repository for every new book that is getting created.
// So basically we create a new folder for every book id and user can add their respective attachments in that folder.
const _createFolder = async function (sdmUrl, jwtToken, repositoryId, rootFolderId, forlderName) {
return new Promise((resolve, reject) => {
const folderCreateURL = sdmUrl + "browser/" + repositoryId + "/root"
const formData = new FormData();
formData.append("objectId", rootFolderId);
formData.append("cmisaction", "createFolder");
formData.append("propertyId[0]", "cmis:name");
formData.append("propertyValue[0]", forlderName);
formData.append("propertyId[1]", "cmis:objectTypeId");
formData.append("propertyValue[1]", "cmis:folder");
formData.append("succinct", 'true');
let headers = formData.getHeaders();
headers["Authorization"] = "Bearer " + jwtToken;
const config = {
headers: headers
}
axios.post(folderCreateURL, formData, config)
.then(response => {
resolve(response.data.succinctProperties["cmis:objectId"])
})
.catch(error => {
reject(error)
})
})
}
module.exports = cds.service.impl(async (service) => {
// This will be called whenever a new draft book is getting created
service.before("NEW", 'Books', async (context) => {
// Fill the repositoryId
context.data.repositoryId = "3a6fbabb-1c19-4014-80cd-e9d4443fd311";
const connJwtToken = await _fetchJwtToken(sdmCredentials.uaa.url, sdmCredentials.uaa.clientid, sdmCredentials.uaa.clientsecret)
// Creating the folder id and fill it
context.data.folderId = await _createFolder(sdmCredentials.endpoints.ecmservice.url, connJwtToken, context.data.repositoryId, "SYNsY7aoCEVirXnHScSBm3SQsSAvCy8zsAkZJjAjUE8", context.data.ID);
});
});
https://api-sdm-di.cfapps.eu10.hana.ondemand.com/browser/3a6fbabb-1c19-4014-80cd-e9d4443fd311/root?o...
"use strict";
const cds = require("@sap/cds");
const proxy = require("@sap/cds-odata-v2-adapter-proxy");
cds.on("bootstrap", app => app.use(proxy()));
module.exports = cds.server;
"@sap/cds-odata-v2-adapter-proxy": "^1.4.48",
"axios": "^0.20.0",
"form-data": "^3.0.0",
{
"name": "approuter",
"description": "Node.js based application router service for html5-apps",
"engines": {
"node": "^8.0.0 || ^10.0.0"
},
"dependencies": {
"@sap/approuter": "8.5.1"
},
"devDependencies": {
"@sap/html5-repo-mock": "1.6.0"
},
"scripts": {
"start": "node node_modules/@sap/approuter/approuter.js"
}
}
{
"welcomeFile": "/communitydmcapbooksapp/index.html",
"authenticationMethod": "route",
"routes": [
{
"source": "^/v2/catalog/(.*)$",
"authenticationType": "xsuaa",
"destination": "srv-api",
"csrfProtection": false
}
]
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
14 | |
12 | |
8 | |
8 | |
7 | |
7 | |
7 | |
6 | |
5 | |
5 |