Skip to content

Commit

Permalink
Update tests (#77)
Browse files Browse the repository at this point in the history
* Update attachments test
  • Loading branch information
amitsahu01 committed Sep 23, 2024
1 parent 043b1b4 commit 4dc6aea
Show file tree
Hide file tree
Showing 38 changed files with 134 additions and 4,925 deletions.
File renamed without changes.
File renamed without changes.
28 changes: 27 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
"dummy": 0
},
"dependencies": {
"@cap-js/audit-logging": "^0.8.1",
"@cap-js/change-tracking": "^1.0.7",
"@sap-cloud-sdk/connectivity": "^3.21.0",
"@sap-cloud-sdk/http-client": "^3.21.0",
"@sap-cloud-sdk/resilience": "^3.21.0",
"@sap-cloud-sdk/util": "^3.21.0",
"@sap/cds": ">=8",
"@sap/xb-msg-amqp-v100": "^0.9.58",
"express": "^4"
},
"devDependencies": {
Expand All @@ -38,7 +45,9 @@
"reset": "read -p 'This will irreversibly reset your working directory including ALL files in this git repo. Continue?' -n 1 -r && echo && if [[ $REPLY =~ ^[Yy]$ ]]; then git clean -fd && git reset --hard && npm i; fi"
},
"jest": {
"modulePathIgnorePatterns": ["<rootDir>/xmpls/"]
"modulePathIgnorePatterns": [
"<rootDir>/xmpls/"
]
},
"sapux": [
"app/incidents"
Expand All @@ -61,6 +70,23 @@
}
}
}
},
"API_BUSINESS_PARTNER": {
"kind": "odata-v2",
"model": "srv/external/API_BUSINESS_PARTNER",
"[production]": {
"credentials": {
"destination": "incidents-api-access",
"path": "/sap/opu/odata/sap/API_BUSINESS_PARTNER"
}
}
},
"messaging": {
"kind": "local-messaging",
"[production]": {
"kind": "enterprise-messaging-shared",
"format": "cloudevents"
}
}
}
},
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
128 changes: 107 additions & 21 deletions srv/services.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,123 @@
/**
* Same as Remote Service Sample. Added additional Handlers for recieving and
* handling events/messages
*/
const cds = require('@sap/cds')

class ProcessorService extends cds.ApplicationService {
/** Registering custom event handlers */
init() {
this.before('UPDATE', 'Incidents', req => this.onUpdate(req))
this.before(['CREATE', 'UPDATE'], 'Incidents', req => this.changeUrgencyDueToSubject(req.data))
return super.init()
async init() {
this.before("UPDATE", "Incidents", (req) => this.onUpdate(req));
this.before("CREATE", "Incidents", (req) => this.changeUrgencyDueToSubject(req.data));
this.on('READ', 'Customers', (req) => this.onCustomerRead(req));
this.on(['CREATE','UPDATE'], 'Incidents', (req, next) => this.onCustomerCache(req, next));
this.S4bupa = await cds.connect.to('API_BUSINESS_PARTNER');
this.remoteService = await cds.connect.to("RemoteService");

// Added Handlers for Eventing on top of remote service sample
this.messaging = await cds.connect.to('messaging');
this.messaging.on('sap.s4.beh.businesspartner.v1.BusinessPartner.Changed.v1', async ({ event, data }) => await this.onBusinessPartnerChanged(event, data))
return super.init();
}

async onBusinessPartnerChanged(event, data){
const {Customers} = this.entities;
const {BusinessPartnerAddress} = this.remoteService.entities;
console.log('<< received', event, data)
const Id = data.BusinessPartner;
const customer = await this.S4bupa.run(SELECT.one(BusinessPartnerAddress, address => {
address('*'),
address.email(emails => {
emails('*')})
}).where({BusinessPartner: Id}));
if(customer){
customer.email = customer.email[0]?.email
const result= await cds.run(UPDATE(Customers).where({ID: customer.ID}).set({email:customer.email}));
console.log("result",result);
}
}

async onCustomerCache(req, next) {
const { Customers } = this.entities;
const newCustomerId = req.data.customer_ID;
const result = await next();
const { BusinessPartner } = this.remoteService.entities;
if (newCustomerId && (newCustomerId !== "") && ((req.event == "CREATE") || (req.event == "UPDATE"))) {
console.log('>> CREATE or UPDATE customer!');

// Expands are required as the runtime does not support path expressions for remote services
const customer = await this.S4bupa.run(SELECT.one(BusinessPartner, bp => {
bp('*'),
bp.addresses(address => {
address('email', 'phoneNumber'),
address.email(emails => {
emails('email')
}),
address.phoneNumber(phoneNumber => {
phoneNumber('phone')
})
})
}).where({ ID: newCustomerId }));

if(customer) {
customer.email = customer.addresses[0]?.email[0]?.email;
customer.phone = customer.addresses[0]?.phoneNumber[0]?.phone;
delete customer.addresses;
delete customer.name;
await UPSERT.into(Customers).entries(customer);
}
}
return result;
}


async onCustomerRead(req) {
console.log('>> delegating to S4 service...', req.query);
const top = parseInt(req._queryOptions?.$top) || 100;
const skip = parseInt(req._queryOptions?.$skip) || 0;

const { BusinessPartner } = this.remoteService.entities;

// Expands are required as the runtime does not support path expressions for remote services
let result = await this.S4bupa.run(SELECT.from(BusinessPartner, bp => {
bp('*'),
bp.addresses(address => {
address('email'),
address.email(emails => {
emails('email');
});
})
}).limit(top, skip));

result = result.map((bp) => ({
ID: bp.ID,
name: bp.name,
email: bp.addresses[0]?.email[0]?.email
}));

// Explicitly set $count so the values show up in the value help in the UI
result.$count = 1000;
console.log("after result", result);
return result;
}


changeUrgencyDueToSubject(data) {
if (data) {
const incidents = Array.isArray(data) ? data : [data]
incidents.forEach(incident => {
if (incident.title?.toLowerCase().includes('urgent')) {
incident.urgency = { code: 'H', descr: 'High' }
const incidents = Array.isArray(data) ? data : [data];
incidents.forEach((incident) => {
if (incident.title?.toLowerCase().includes("urgent")) {
incident.urgency = { code: "H", descr: "High" };
}
})
});
}
}

/** Custom Validation */
async onUpdate(req) {
const { status_code } = await SELECT.one(req.subject, i => i.status_code).where({ ID: req.data.ID })
if (status_code === 'C') {
async onUpdate (req) {
const { status_code } = await SELECT.one(req.subject, i => i.status_code).where({ID: req.data.ID})
if (status_code === 'C')
return req.reject(`Can't modify a closed incident`)
}
}
}

module.exports = { ProcessorService }


// ------------------------------------------------------------------------------------------------------------------------------------------------------------
// For demo purposess only...
const _require = id => {try{ return require(id) } catch(e) { if (e.code !== 'MODULE_NOT_FOUND') throw e }}
cds.once("served", ()=> _require('./alert-notifications')?.prototype.init.call(cds.services.ProcessorService))
module.exports = { ProcessorService }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file removed xmpls/SolarPanelReport.pdf
Binary file not shown.
61 changes: 0 additions & 61 deletions xmpls/alert-notifications.js

This file was deleted.

7 changes: 0 additions & 7 deletions xmpls/attachments.cds

This file was deleted.

83 changes: 0 additions & 83 deletions xmpls/attachments.test.js

This file was deleted.

Loading

0 comments on commit 4dc6aea

Please sign in to comment.