-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcds-plugin.js
41 lines (33 loc) · 1.23 KB
/
cds-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const cds = require('@sap/cds');
const propertyIsFiltered = (req, property) => {
const where = req.query?.SELECT?.where ?? [];
for (let filter of where) {
if (filter.ref?.includes(property)) {
return true;
}
}
return false;
}
cds.once('served', () => {
for (let srv of cds.services) {
const softDeleteEntities = [];
for (let entity of srv.entities) {
if (entity.includes.includes('softDelete')) {
softDeleteEntities.push(entity);
}
}
srv.before('READ', softDeleteEntities, async (req) => {
// if there is an filter applied with deleted at, do not apply default filter
if (!propertyIsFiltered(req, 'deletedAt')) {
req.query.where({ deletedAt: null });
}
});
srv.on('DELETE', softDeleteEntities, async (req) => {
await srv.transaction(req).run(
UPDATE.entity(req.target).data({ deletedAt: new Date().toString(), deletedBy: req.user.id }).where(req.data)
);
});
// Shift on handler to first position, because otherwise it won't be called
srv._handlers.on.unshift(srv._handlers.on.pop());
}
});