-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsession-context.js
32 lines (29 loc) · 1.05 KB
/
session-context.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
const cds = require('@sap/cds/lib')
class SessionContext {
constructor(ctx) {
Object.defineProperty(this, 'ctx', { value: ctx })
}
get '$user.id'() {
return (super['$user.id'] = this.ctx.user?.id || 'anonymous')
}
get '$user.locale'() {
return (super['$user.locale'] = this.ctx.locale || cds.env.i18n.default_language)
}
// REVISIT: should be decided in spec meeting for definitive name
get $now() {
return (super.$now = (this.ctx.timestamp || new Date()).toISOString())
}
}
class TemporalSessionContext extends SessionContext {
get '$valid.from'() {
return (super['$valid.from'] = this.ctx._?.['VALID-FROM'] ?? this.ctx._?.['VALID-AT'] ?? new Date().toISOString())
}
get '$valid.to'() {
return (super['$valid.to'] =
this.ctx._?.['VALID-TO'] ??
this.ctx._?.['VALID-AT']?.replace(/(\dZ?)$/, d => parseInt(d[0]) + 1 + d[1] || '') ??
new Date().toISOString().replace(/(\dZ?)$/, d => parseInt(d[0]) + 1 + d[1] || ''))
}
}
// REVISIT: only set temporal context if required!
module.exports = TemporalSessionContext