cap-user-info 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -29,6 +29,8 @@ associations `_toCreatedUserInfo` / `_toModifiedUserInfo` resolve via
29
29
  `UserTracked` extends `managed`, so consuming entities automatically
30
30
  inherit `createdBy` / `modifiedBy` (and the corresponding timestamps):
31
31
 
32
+ It will add a quickview to the createdBy and modifiedBy to show the user details. The details are stored from the req.user upon changes to the entity.
33
+ ![alt text](image.png)
32
34
  ```cds
33
35
  aspect UserTracked : managed { ... }
34
36
  ```
@@ -1,4 +1,4 @@
1
- import cds from '@sap/cds';
1
+ import cds from "@sap/cds";
2
2
  interface CsnEntity {
3
3
  name: string;
4
4
  kind?: string;
package/dist/handlers.js CHANGED
@@ -6,36 +6,52 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.hasUserTrackedAspect = hasUserTrackedAspect;
7
7
  exports.registerHandlers = registerHandlers;
8
8
  const cds_1 = __importDefault(require("@sap/cds"));
9
- const UPSERTED_FLAG = Symbol.for('cap-user-info.upserted-this-tx');
9
+ const UPSERTED_FLAG = Symbol.for("cap-user-info.upserted-this-tx");
10
+ const userInfoLogger = cds_1.default.log("cap.userinfo");
10
11
  function hasUserTrackedAspect(entity) {
11
- const els = entity?.elements;
12
- if (!els)
12
+ const entieies = entity?.elements;
13
+ if (!entieies)
13
14
  return false;
14
- return Boolean(els._toCreatedUserInfo && els._toModifiedUserInfo);
15
+ return Boolean(entieies._toCreatedUserInfo && entieies._toModifiedUserInfo);
15
16
  }
16
17
  async function upsertUserInfo(req) {
17
18
  const user = req.user;
18
- if (!user?.id || user.is('system-user') || user._is_anonymous)
19
+ if (!user?.id || user.is("system-user") || user._is_anonymous) {
20
+ userInfoLogger.debug(`Skipping user info upsert for system or anonymous user: ${user?.id}`);
19
21
  return;
22
+ }
20
23
  const ctx = cds_1.default.context;
21
24
  if (ctx && ctx[UPSERTED_FLAG])
22
25
  return;
23
26
  if (ctx)
24
27
  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
- });
28
+ const { UserInfo } = cds_1.default.entities("cap.userinfo");
29
+ let attr = user.attr ?? {};
30
+ if (Object.keys(attr).length === 0) {
31
+ userInfoLogger.debug(`User ${user.id} has no attr property, using dummy values for user info upsert`);
32
+ attr.email = "Dummy@example.com";
33
+ attr.givenName = user.id || "FirstName";
34
+ attr.familyName = "LastName";
35
+ }
36
+ userInfoLogger.debug(`Upserting user info for user ${user.id}: ${JSON.stringify(attr)}`);
37
+ try {
38
+ await UPSERT.into(UserInfo).entries({
39
+ ID: user.id,
40
+ Email: attr.email,
41
+ GivenName: attr.givenName,
42
+ FamilyName: attr.familyName,
43
+ });
44
+ }
45
+ catch (error) {
46
+ userInfoLogger.error(`Failed to upsert user info for user ${user.id}: ${error.message}`);
47
+ }
33
48
  }
34
49
  function registerHandlers(srv) {
35
50
  const tracked = [...srv.entities].filter(hasUserTrackedAspect);
36
51
  if (tracked.length === 0)
37
52
  return;
38
- srv.after(['CREATE', 'UPDATE'], tracked, async (_data, req) => {
53
+ userInfoLogger.debug(`Registering user info handlers for entities: ${tracked.map((e) => e.name).join(", ")}`);
54
+ srv.after(["CREATE", "UPDATE"], tracked, async (_data, req) => {
39
55
  await upsertUserInfo(req);
40
56
  });
41
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cap-user-info",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "CDS plugin: track created/modified user details on managed entities",
5
5
  "main": "cds-plugin.js",
6
6
  "repository": {