@@ -5,6 +5,8 @@ auto_validation: true
55time : 15
66tags : [ tutorial>beginner, software-product>sap-btp--cloud-foundry-environment]
77primary_tag : software-product-function>sap-cloud-application-programming-model
8+ author_name : Alessandro Biagi
9+ author_profile : https://github.com/ale-biagi
810---
911
1012## Prerequisites
@@ -194,6 +196,8 @@ In the **Terminal** type `cd ..` and press **Enter** to go back to the **project
194196
195197![ Figure 9 – Change back to project root] ( change-to-project.png )
196198
199+ > ** NOTE** : newer versions of CDS have handed over to ** SAP Cloud SDK** the creation of HTTP clients for making HTTP requests to external services, using the ` @sap-cloud-sdk/http-client ` node package. So, if you jump-started your CAP project, ** before such CDS update** , that dependency might not have been included in your ` package.json ` file, and, thus, not installed when you ran ` npm install ` . Before testing the handler, please verify that you have ` @sap-cloud-sdk/http-client ` listed in the dependencies of your ` package.json ` and, if not, run: ` npm install @sap-cloud-sdk/http-client ` .
200+
197201Once again type ` cds watch ` and press ** Enter** . Then ` CTRL+Click ` on the ` http://localhost:4004 ` link to launch the ** application home page** .
198202
199203![ Figure 10 – Application home page] ( service-endpoints.png )
@@ -214,6 +218,7 @@ Open the `lib/hanlers.js` file, then copy and paste the **following code** over
214218
215219``` JavasScript
216220const cds = require('@sap/cds');
221+ const namespace = 'sfsf.projman.model.db.';
217222
218223let userService = null;
219224let assService = null;
@@ -250,11 +255,11 @@ function removeColumnsFromOrderBy(query, columnNames) {
250255
251256// Helper for employee create execution
252257async function executeCreateEmployee(req, userId) {
253- const employee = await cds.tx(req).run(SELECT.one.from('Employee').columns(['userId']).where({ userId: { '=': userId } }));
258+ const employee = await cds.tx(req).run(SELECT.one.from(namespace + 'Employee').columns(['userId']).where({ userId: { '=': userId } }));
254259 if (!employee) {
255260 const sfsfUser = await userService.tx(req).run(SELECT.one.from('User').columns(['userId', 'username', 'defaultFullName', 'email', 'division', 'department', 'title']).where({ userId: { '=': userId } }));
256261 if (sfsfUser) {
257- await cds.tx(req).run(INSERT.into('Employee').entries(sfsfUser));
262+ await cds.tx(req).run(INSERT.into(namespace + 'Employee').entries(sfsfUser));
258263 }
259264 }
260265}
@@ -263,7 +268,7 @@ async function executeCreateEmployee(req, userId) {
263268async function executeUpdateEmployee(req, entity, entityID, userId) {
264269 // Need to check whether column has changed
265270 const column = 'member_userId';
266- const query = SELECT.one.from(entity).columns([column]).where({ ID: { '=': entityID } });
271+ const query = SELECT.one.from(namespace + entity).columns([column]).where({ ID: { '=': entityID } });
267272 const item = await cds.tx(req).run(query);
268273 if (item && item[column] != userId) {
269274 // Member has changed, then:
@@ -279,7 +284,7 @@ async function executeUpdateEmployee(req, entity, entityID, userId) {
279284// Helper for assignment creation
280285async function createAssignment(req, entity, entityID, userId) {
281286 const columns = m => { m.member_userId`as userId`, m.parent(p => { p.name`as name`, p.description`as description`, p.startDate`as startDate`, p.endDate`as endDate` }), m.role(r => { r.name`as role` }) };
282- const item = await cds.tx(req).run(SELECT.one.from(entity).columns(columns).where({ ID: { '=': entityID } }));
287+ const item = await cds.tx(req).run(SELECT.one.from(namespace + entity).columns(columns).where({ ID: { '=': entityID } }));
283288 if (item) {
284289 const assignment = {
285290 userId: userId,
@@ -291,15 +296,15 @@ async function createAssignment(req, entity, entityID, userId) {
291296 console.log(assignment);
292297 const element = await assService.tx(req).run(INSERT.into('Background_SpecialAssign').entries(assignment));
293298 if (element) {
294- await cds.tx(req).run(UPDATE.entity(entity).with({ hasAssignment: true }).where({ ID: entityID }));
299+ await cds.tx(req).run(UPDATE.entity(namespace + entity).with({ hasAssignment: true }).where({ ID: entityID }));
295300 }
296301 }
297302 return req;
298303}
299304
300305// Helper for cascade deletion
301306async function deepDelete(tx, ID, childEntity) {
302- return await tx.run(DELETE.from(childEntity).where({ parent_ID: { '=': ID } }));
307+ return await tx.run(DELETE.from(namespace + childEntity).where({ parent_ID: { '=': ID } }));
303308}
304309
305310/*** HANDLERS ***/
@@ -368,7 +373,7 @@ async function deleteChildren(req) {
368373 await deepDelete(cds.tx(req), req.data.ID, 'Activity');
369374 await deepDelete(cds.tx(req), req.data.ID, 'Member');
370375 } else {
371- const item = await cds.tx(req).run(SELECT.one.from(req.entity).columns(['parent_ID']).where({ ID: { '=': req.data.ID } }));
376+ const item = await cds.tx(req).run(SELECT.one.from(namespace + req.entity).columns(['parent_ID']).where({ ID: { '=': req.data.ID } }));
372377 if (item) {
373378 await deepDelete(cds.tx(req), item.parent_ID, 'Activity');
374379 }
@@ -383,8 +388,8 @@ async function deleteChildren(req) {
383388async function deleteUnassignedEmployees(data, req) {
384389 try {
385390 // Build clean-up filter
386- const members = SELECT.distinct.from('Member').columns(['member_userId as userId']);
387- const unassigned = SELECT.distinct.from('Employee').columns(['userId']).where({ userId: { 'NOT IN': members } });
391+ const members = SELECT.distinct.from(namespace + 'Member').columns(['member_userId as userId']);
392+ const unassigned = SELECT.distinct.from(namespace + 'Employee').columns(['userId']).where({ userId: { 'NOT IN': members } });
388393
389394 // Get the unassigned employees for deletion
390395 let deleted = await cds.tx(req).run(unassigned);
@@ -394,7 +399,7 @@ async function deleteUnassignedEmployees(data, req) {
394399
395400 // Clean-up Employees
396401 for (var i = 0; i < deleted.length; i++) {
397- const clean_up = DELETE.from('Employee').where({ userId: { '=': deleted[i].userId } });
402+ const clean_up = DELETE.from(namespace + 'Employee').where({ userId: { '=': deleted[i].userId } });
398403 await cds.tx(req).run(clean_up);
399404 }
400405 return data;
@@ -412,7 +417,7 @@ async function beforeSaveProject(req) {
412417 req.data.team.forEach(member => { users.push({ ID: member.ID, member_userId: member.member_userId }); });
413418
414419 // Get current members
415- let members = await cds.tx(req).run(SELECT.from('Member').columns(['ID', 'member_userId']).where({ parent_ID: { '=': req.data.ID } }));
420+ let members = await cds.tx(req).run(SELECT.from(namespace + 'Member').columns(['ID', 'member_userId']).where({ parent_ID: { '=': req.data.ID } }));
416421 if (members) {
417422 // Make sure result is an array
418423 members = (members.length === undefined) ? [members] : members;
@@ -425,7 +430,7 @@ async function beforeSaveProject(req) {
425430 });
426431 for (var i = 0; i < deleted.length; i++) {
427432 // Delete members' activities
428- await cds.tx(req).run(DELETE.from('Activity').where({ assignedTo_ID: { '=': deleted[i].ID } }));
433+ await cds.tx(req).run(DELETE.from(namespace + 'Activity').where({ assignedTo_ID: { '=': deleted[i].ID } }));
429434 if (req.data.activities) {
430435 let idx = 0;
431436 do {
@@ -469,7 +474,7 @@ async function afterSaveProject(data, req) {
469474 try {
470475 if (data.team) {
471476 // Look for members with unassigned elementId
472- let unassigned = await cds.tx(req).run(SELECT.from('Member').columns(['ID', 'member_userId']).where({ parent_ID: { '=': data.ID }, and: { hasAssignment: { '=': false } } }));
477+ let unassigned = await cds.tx(req).run(SELECT.from(namespace + 'Member').columns(['ID', 'member_userId']).where({ parent_ID: { '=': data.ID }, and: { hasAssignment: { '=': false } } }));
473478 if (unassigned) {
474479 // Make sure result is an array
475480 unassigned = (unassigned.length === undefined) ? [unassigned] : unassigned;
0 commit comments