Skip to content

Commit a1bc496

Browse files
authored
feat: common log entry fields can be provided manually (#65)
closes #64
1 parent 6dc9a52 commit a1bc496

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55
The format is based on [Keep a Changelog](http://keepachangelog.com/).
66

7+
## Version 0.5.0 - 2023-11-22
8+
9+
### Added
10+
11+
- Common log entry fields `uuid`, `tenant`, `user` and `time` can be provided manually
12+
713
## Version 0.4.0 - 2023-10-24
814

915
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cap-js/audit-logging",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "CDS plugin providing integration to the SAP Audit Log service as well as out-of-the-box personal data-related audit logging based on annotations.",
55
"repository": "cap-js/audit-logging",
66
"author": "SAP SE (https://www.sap.com)",

srv/service.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ module.exports = class AuditLogService extends Base {
88

99
// add common audit log entry fields
1010
this.before('*', req => {
11-
const { tenant, user, timestamp: time } = cds.context
12-
Object.assign(req.data, { uuid: cds.utils.uuid(), tenant, user: user.id, time })
11+
const { tenant, user, timestamp } = cds.context
12+
req.data.uuid ??= cds.utils.uuid()
13+
req.data.tenant ??= tenant
14+
req.data.user ??= user.id
15+
req.data.time ??= timestamp
1316
})
1417

1518
// call OutboxService's init

test/api/api.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,33 @@ describe('AuditLogService API', () => {
9494
await audit.log('foo', { data_subject: { ID: { bar: 'baz' } } })
9595
expect(_logs).toContainMatchObject({ data_subject: { ID: { bar: 'baz' } } })
9696
})
97+
98+
describe('common log entry fields', () => {
99+
test('are automatically filled', async () => {
100+
await cds.tx({ tenant: 'bar' }, async () => {
101+
const audit = await cds.connect.to('audit-log')
102+
await audit.log('foo', {})
103+
})
104+
expect(_logs).toContainMatchObject({
105+
uuid: expect.any(String),
106+
tenant: 'bar',
107+
user: 'anonymous',
108+
time: expect.any(Date)
109+
})
110+
})
111+
112+
test('can be provided manually', async () => {
113+
const time = new Date('2021-01-01T00:00:00.000Z')
114+
await cds.tx({ tenant: 'bar' }, async () => {
115+
const audit = await cds.connect.to('audit-log')
116+
await audit.log('foo', { uuid: 'baz', tenant: 'baz', user: 'baz', time })
117+
})
118+
expect(_logs).toContainMatchObject({
119+
uuid: 'baz',
120+
tenant: 'baz',
121+
user: 'baz',
122+
time
123+
})
124+
})
125+
})
97126
})

0 commit comments

Comments
 (0)