Adding Custom Logic
This commit is contained in:
23
srv/services.js
Normal file
23
srv/services.js
Normal file
@@ -0,0 +1,23 @@
|
||||
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", "Incidents", (req) => this.changeUrgencyDueToSubject(req.data));
|
||||
|
||||
return super.init();
|
||||
}
|
||||
|
||||
changeUrgencyDueToSubject(data) {
|
||||
let urgent = data.title?.match(/urgent/i)
|
||||
if (urgent) data.urgency_code = 'H'
|
||||
}
|
||||
|
||||
/** Custom Validation */
|
||||
async onUpdate (req) {
|
||||
let closed = await SELECT.one(1) .from (req.subject) .where `status.code = 'C'`
|
||||
if (closed) req.reject `Can't modify a closed incident!`
|
||||
}
|
||||
}
|
||||
module.exports = { ProcessorService }
|
||||
Reference in New Issue
Block a user