const cds = require('@sap/cds')
cds.once('served', () => {
for(let srv of cds.services){
//for each served services
var entitiesToAudit = [];
for(let entity of srv.entities){
if(entity['@audit']){
console.log('Audit needed for ' , entity.name );
entitiesToAudit.push(entity);
}
}
srv.before("UPDATE" , entitiesToAudit , async (req)=>{
//incoming payload
let data = req.data;
let dataKeys = Object.keys(data);
//keys of the entity
let entityKeys = Object.keys(req.target.keys);
let keyData = {};
entityKeys.forEach((k)=>{
keyData[k] = data[k];
});
//entity itself
let target = req.target;
//read the exisitng record from DB via service
var originalData = await srv.read(target).byKey(keyData);
// do comparison field by field
var changeLog = {};
dataKeys.forEach((dk)=>{
if(originalData[dk]!= data[dk]){
changeLog[dk] = {
oldValue : originalData[dk] ,
newValue : data[dk]
};
}
});
var changeLogWithSignature = {
user : req.user.id ,
at : new Date() ,
changeLog : changeLog
} ;
//log the data on console, Kibana , or write it to DB / File
return data ;
});
}
});
[cds] - loaded plugin: { impl: 'audit-plugin/cds-plugin' }
[cds] - loaded model from 2 file(s):
srv/Service.cds
db/Database.cds
[2023-05-10 12:07:21] INFO the build results of the "simple-cds-srv" module will be packaged and saved in the "/home/user/projects/CAP-Apps/simple-cds/.simple-cds_mta_build_tmp/simple-cds-srv" folder
[2023-05-10 12:07:21] ERROR could not package the "simple-cds-srv" module when archiving: could not read the "/home/user/projects/CAP-Apps/simple-cds/gen/srv/node_modules/audit-plugin" symbolic link: stat /home/user/projects/CAP-Apps/simple-cds/gen/srv/audit-plugin: no such file or directory
make: *** [Makefile_20230510120706.mta:37: simple-cds-srv] Error 1
Error: could not build the MTA project: could not execute the "make -f Makefile_20230510120706.mta p=cf mtar= strict=true mode=" command: exit status 2
build-parameters:
before-all:
- builder: custom
commands:
- npx -p @sap/cds-dk cds build --production
- npx copyfiles -f audit-plugin/*.* gen/srv/audit-plugin/ -a
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
8 | |
6 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
3 |