cap-user-info 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jakob Marius Kjaer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # cap-user-info
2
+
3
+ CDS plugin that tracks created/modified user details on managed entities.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install cap-user-info
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```cds
14
+ using { UserTracked, UserInfo } from 'cap-user-info';
15
+
16
+ entity MyEntity : cuid, UserTracked {
17
+ // your fields
18
+ }
19
+ ```
20
+
21
+ The plugin auto-registers `.after("CREATE" | "UPDATE")` handlers on every
22
+ application-service entity that includes the `UserTracked` aspect. Each
23
+ handler UPSERTs the current user (`req.user`) into `UserInfo`. The
24
+ associations `_toCreatedUserInfo` / `_toModifiedUserInfo` resolve via
25
+ `createdBy` / `modifiedBy`.
26
+
27
+ ## How it works
28
+
29
+ `UserTracked` extends `managed`, so consuming entities automatically
30
+ inherit `createdBy` / `modifiedBy` (and the corresponding timestamps):
31
+
32
+ ```cds
33
+ aspect UserTracked : managed { ... }
34
+ ```
35
+
36
+ This means no extra `managed` declaration is required on the consuming
37
+ entity — including `UserTracked` is enough.
package/cds-plugin.js ADDED
@@ -0,0 +1,9 @@
1
+ const cds = require('@sap/cds');
2
+
3
+ cds.on('served', async () => {
4
+ const { registerHandlers } = require('./dist/handlers.js');
5
+ for (const srv of cds.services) {
6
+ if (!(srv instanceof cds.ApplicationService)) continue;
7
+ registerHandlers(srv);
8
+ }
9
+ });
@@ -0,0 +1,53 @@
1
+ using { managed } from '@sap/cds/common';
2
+ namespace cap.userinfo;
3
+
4
+ @UI.FieldGroup #contactDetails: {Data: [
5
+ {
6
+ $Type: 'UI.DataField',
7
+ Value: GivenName,
8
+ },
9
+ {
10
+ $Type: 'UI.DataField',
11
+ Value: FamilyName,
12
+ },
13
+ {
14
+ $Type: 'UI.DataField',
15
+ Value: Email,
16
+ }
17
+ ]}
18
+ @UI.QuickViewFacets : [{
19
+ $Type : 'UI.ReferenceFacet',
20
+ Target: '@UI.FieldGroup#contactDetails',
21
+ Label : 'Contact Details',
22
+ }]
23
+ @UI.HeaderInfo : {
24
+ ImageUrl : '',
25
+ TypeImageUrl: 'sap-icon://customer',
26
+ Title : {
27
+ Label: 'Name',
28
+ Value: FullName,
29
+ },
30
+ TypeName : '',
31
+ }
32
+ @cds.autoexpose
33
+ entity UserInfo {
34
+ @UI.Hidden
35
+ key ID : UUID;
36
+
37
+ @assert.unique: true @title: 'Email Address'
38
+ @Communication: {IsEmailAddress: true}
39
+ Email : String;
40
+
41
+ GivenName : String @title: 'Given Name';
42
+ FamilyName : String @title: 'Family Name';
43
+ FullName : String = concat(
44
+ GivenName, ' ', FamilyName
45
+ );
46
+ }
47
+
48
+ aspect UserTracked : managed {
49
+ _toCreatedUserInfo : Association to one UserInfo
50
+ on _toCreatedUserInfo.ID = $self.createdBy;
51
+ _toModifiedUserInfo : Association to one UserInfo
52
+ on _toModifiedUserInfo.ID = $self.modifiedBy;
53
+ }
@@ -0,0 +1,9 @@
1
+ import cds from '@sap/cds';
2
+ interface CsnEntity {
3
+ name: string;
4
+ kind?: string;
5
+ elements?: Record<string, any>;
6
+ }
7
+ export declare function hasUserTrackedAspect(entity: CsnEntity): boolean;
8
+ export declare function registerHandlers(srv: cds.ApplicationService): void;
9
+ export {};
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.hasUserTrackedAspect = hasUserTrackedAspect;
7
+ exports.registerHandlers = registerHandlers;
8
+ const cds_1 = __importDefault(require("@sap/cds"));
9
+ const UPSERTED_FLAG = Symbol.for('cap-user-info.upserted-this-tx');
10
+ function hasUserTrackedAspect(entity) {
11
+ const els = entity?.elements;
12
+ if (!els)
13
+ return false;
14
+ return Boolean(els._toCreatedUserInfo && els._toModifiedUserInfo);
15
+ }
16
+ async function upsertUserInfo(req) {
17
+ const user = req.user;
18
+ if (!user?.id || user.is('system-user') || user._is_anonymous)
19
+ return;
20
+ const ctx = cds_1.default.context;
21
+ if (ctx && ctx[UPSERTED_FLAG])
22
+ return;
23
+ if (ctx)
24
+ ctx[UPSERTED_FLAG] = true;
25
+ const { UserInfo } = cds_1.default.entities('cap.userinfo');
26
+ const attr = user.attr ?? {};
27
+ await UPSERT.into(UserInfo).entries({
28
+ ID: user.id,
29
+ Email: attr.email,
30
+ GivenName: attr.givenName,
31
+ FamilyName: attr.familyName
32
+ });
33
+ }
34
+ function registerHandlers(srv) {
35
+ const tracked = [...srv.entities].filter(hasUserTrackedAspect);
36
+ if (tracked.length === 0)
37
+ return;
38
+ srv.after(['CREATE', 'UPDATE'], tracked, async (_data, req) => {
39
+ await upsertUserInfo(req);
40
+ });
41
+ }
package/index.cds ADDED
@@ -0,0 +1 @@
1
+ using from './db/user-info';
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "cap-user-info",
3
+ "version": "0.1.0",
4
+ "description": "CDS plugin: track created/modified user details on managed entities",
5
+ "main": "cds-plugin.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/yourusername/cap-user-info.git"
9
+ },
10
+ "keywords": [
11
+ "cds",
12
+ "cap",
13
+ "plugin",
14
+ "user-info",
15
+ "created-by",
16
+ "modified-by"
17
+ ],
18
+ "author": "Jakob Kjaer",
19
+ "files": [
20
+ "cds-plugin.js",
21
+ "db/",
22
+ "index.cds",
23
+ "dist/",
24
+ "README.md"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsc",
28
+ "prepare": "tsc",
29
+ "test": "node --import tsx --test-isolation=none --test 'test/**/*.test.ts'"
30
+ },
31
+ "peerDependencies": {
32
+ "@sap/cds": ">=8"
33
+ },
34
+ "devDependencies": {
35
+ "@cap-js/cds-test": "^1.0.1",
36
+ "@cap-js/cds-types": "^0.15.0",
37
+ "@cap-js/sqlite": "^2.1.0",
38
+ "@sap/cds": "^9",
39
+ "tsx": "^4",
40
+ "typescript": "^5"
41
+ }
42
+ }