@cap-js/change-tracking 1.0.8 → 1.1.1

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +34 -2
  3. package/_i18n/i18n.properties +4 -0
  4. package/_i18n/i18n_ar.properties +55 -0
  5. package/_i18n/i18n_bg.properties +55 -0
  6. package/_i18n/i18n_cs.properties +55 -0
  7. package/_i18n/i18n_da.properties +55 -0
  8. package/_i18n/i18n_de.properties +55 -51
  9. package/_i18n/i18n_el.properties +55 -0
  10. package/_i18n/i18n_en.properties +5 -1
  11. package/_i18n/i18n_en_US_saptrc.properties +55 -0
  12. package/_i18n/i18n_es.properties +55 -51
  13. package/_i18n/i18n_es_MX.properties +55 -0
  14. package/_i18n/i18n_fi.properties +55 -0
  15. package/_i18n/i18n_fr.properties +55 -51
  16. package/_i18n/i18n_he.properties +55 -0
  17. package/_i18n/i18n_hr.properties +55 -0
  18. package/_i18n/i18n_hu.properties +55 -0
  19. package/_i18n/i18n_it.properties +55 -51
  20. package/_i18n/i18n_ja.properties +11 -3
  21. package/_i18n/i18n_kk.properties +55 -0
  22. package/_i18n/i18n_ko.properties +55 -0
  23. package/_i18n/i18n_ms.properties +55 -0
  24. package/_i18n/i18n_nl.properties +55 -0
  25. package/_i18n/i18n_no.properties +55 -0
  26. package/_i18n/i18n_pl.properties +55 -51
  27. package/_i18n/i18n_pt.properties +55 -51
  28. package/_i18n/i18n_ro.properties +55 -0
  29. package/_i18n/i18n_ru.properties +55 -51
  30. package/_i18n/i18n_sh.properties +55 -0
  31. package/_i18n/i18n_sk.properties +55 -0
  32. package/_i18n/i18n_sl.properties +55 -0
  33. package/_i18n/i18n_sv.properties +55 -0
  34. package/_i18n/i18n_th.properties +55 -0
  35. package/_i18n/i18n_tr.properties +55 -0
  36. package/_i18n/i18n_uk.properties +55 -0
  37. package/_i18n/i18n_vi.properties +55 -0
  38. package/_i18n/i18n_zh_CN.properties +10 -6
  39. package/_i18n/i18n_zh_TW.properties +55 -0
  40. package/cds-plugin.js +1 -1
  41. package/index.cds +4 -3
  42. package/lib/change-log.js +26 -9
  43. package/lib/entity-helper.js +21 -1
  44. package/lib/format-options.js +66 -0
  45. package/lib/localization.js +55 -43
  46. package/package.json +12 -12
@@ -1,49 +1,32 @@
1
1
  const cds = require("@sap/cds/lib");
2
2
  const LOG = cds.log("change-log");
3
- const { getNameFromPathVal, getDBEntity } = require("./entity-helper");
3
+ const { formatOptions } = require("./format-options");
4
+ const { getNameFromPathVal, getDBEntity, splitPath } = require("./entity-helper");
4
5
 
5
6
  const MODIF_I18N_MAP = {
6
- create: "{i18n>ChangeLog.modification.create}",
7
- update: "{i18n>ChangeLog.modification.update}",
8
- delete: "{i18n>ChangeLog.modification.delete}",
7
+ create: "ChangeLog.modification.create",
8
+ update: "ChangeLog.modification.update",
9
+ delete: "ChangeLog.modification.delete",
9
10
  };
10
11
 
11
- const _getLocalization = function (locale, i18nKey) {
12
- //
13
- //
14
- //
15
- //
16
- // REVISIT!
17
- // REVISIT!
18
- // REVISIT!
19
- // REVISIT!
20
- // REVISIT!
21
- //
22
- //
23
- //
24
- //
25
- return JSON.parse(cds.localize(cds.model, locale, JSON.stringify(i18nKey)));
26
- };
27
-
28
- const _localizeModification = function (change, locale) {
12
+ const _localizeModification = function (change) {
29
13
  if (change.modification && MODIF_I18N_MAP[change.modification]) {
30
- change.modification = _getLocalization(locale, MODIF_I18N_MAP[change.modification]);
14
+ change.modification = cds.i18n.labels.for(MODIF_I18N_MAP[change.modification]);
31
15
  }
32
16
  };
33
17
 
34
- const _localizeDefaultObjectID = function (change, locale) {
18
+ const _localizeDefaultObjectID = function (change) {
35
19
  if (!change.objectID) {
36
20
  change.objectID = change.entity ? change.entity : "";
37
21
  }
38
22
  if (change.objectID && change.serviceEntityPath && !change.parentObjectID && change.parentKey) {
39
- const path = change.serviceEntityPath.split('/');
23
+ const path = splitPath(change.serviceEntityPath);
40
24
  const parentNodePathVal = path[path.length - 2];
41
25
  const parentEntityName = getNameFromPathVal(parentNodePathVal);
42
26
  const dbEntity = getDBEntity(parentEntityName);
43
27
  try {
44
- const labelI18nKey = dbEntity['@Common.Label'] || dbEntity['@title'];
45
- const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
46
- change.parentObjectID = labelI18nValue ? labelI18nValue : dbEntity.name;
28
+ const labelI18nKey = getTranslationKey(dbEntity['@Common.Label'] || dbEntity['@title']);
29
+ change.parentObjectID = cds.i18n.labels.for(labelI18nKey) || labelI18nKey || dbEntity.name;
47
30
  } catch (e) {
48
31
  LOG.error("Failed to localize parent object id", e);
49
32
  throw new Error("Failed to localize parent object id", e);
@@ -51,13 +34,11 @@ const _localizeDefaultObjectID = function (change, locale) {
51
34
  }
52
35
  };
53
36
 
54
- const _localizeEntityType = function (change, locale) {
37
+ const _localizeEntityType = function (change) {
55
38
  if (change.entity) {
56
39
  try {
57
40
  const labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity);
58
- const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
59
-
60
- change.entity = labelI18nValue ? labelI18nValue : change.entity;
41
+ change.entity = labelI18nKey || change.entity;
61
42
  } catch (e) {
62
43
  LOG.error("Failed to localize entity type", e);
63
44
  throw new Error("Failed to localize entity type", e);
@@ -66,9 +47,7 @@ const _localizeEntityType = function (change, locale) {
66
47
  if (change.serviceEntity) {
67
48
  try {
68
49
  const labelI18nKey = _getLabelI18nKeyOnEntity(change.serviceEntity);
69
- const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
70
-
71
- change.serviceEntity = labelI18nValue ? labelI18nValue : change.serviceEntity;
50
+ change.serviceEntity = labelI18nKey || change.serviceEntity;
72
51
  } catch (e) {
73
52
  LOG.error("Failed to localize service entity", e);
74
53
  throw new Error("Failed to localize service entity", e);
@@ -76,7 +55,13 @@ const _localizeEntityType = function (change, locale) {
76
55
  }
77
56
  };
78
57
 
79
- const _localizeAttribute = function (change, locale) {
58
+ const getTranslationKey = (value) => {
59
+ if (typeof value != 'string') return value;
60
+ const result = value.match(/(?<=\{@?(i18n>)).*(?=\})/g)
61
+ return result ? result[0] : value
62
+ }
63
+
64
+ const _localizeAttribute = function (change) {
80
65
  if (change.attribute && change.serviceEntity) {
81
66
  try {
82
67
  const serviceEntity = cds.model.definitions[change.serviceEntity];
@@ -85,8 +70,7 @@ const _localizeAttribute = function (change, locale) {
85
70
  const element = serviceEntity.elements[change.attribute];
86
71
  if (element.isAssociation) labelI18nKey = _getLabelI18nKeyOnEntity(element.target);
87
72
  }
88
- const labelI18nValue = labelI18nKey ? _getLocalization(locale, labelI18nKey) : null;
89
- change.attribute = labelI18nValue ? labelI18nValue : change.attribute;
73
+ change.attribute = labelI18nKey || change.attribute;
90
74
  } catch (e) {
91
75
  LOG.error("Failed to localize change attribute", e);
92
76
  throw new Error("Failed to localize change attribute", e);
@@ -98,16 +82,44 @@ const _getLabelI18nKeyOnEntity = function (entityName, /** optinal */ attribute)
98
82
  let def = cds.model.definitions[entityName];
99
83
  if (attribute) def = def?.elements[attribute]
100
84
  if (!def) return "";
101
- return def['@Common.Label'] || def['@title'] || def['@UI.HeaderInfo.TypeName'];
85
+ const i18nKey = getTranslationKey(def['@Common.Label'] || def['@title'] || def['@UI.HeaderInfo.TypeName']);
86
+ return cds.i18n.labels.for(i18nKey) || i18nKey;
87
+ };
88
+
89
+ const parseTime = (time, locale, options) => {
90
+ const timeParts = time.split(':');
91
+ const date = new Date();
92
+ date.setHours(parseInt(timeParts[0], 10), parseInt(timeParts[1], 10), parseInt(timeParts[2], 10));
93
+ return date.toLocaleTimeString(locale, options);
94
+ };
95
+
96
+ const _localizeValue = (change, locale) => {
97
+ if (change.valueDataType !== 'cds.Date' && change.valueDataType !== 'cds.DateTime' && change.valueDataType !== 'cds.Timestamp' && change.valueDataType !== 'cds.Time') {
98
+ return;
99
+ }
100
+ const normalizedLocale = locale.replaceAll('_', '-');
101
+ const options = formatOptions[change.valueDataType]?.[normalizedLocale]
102
+ ?? formatOptions[change.valueDataType]?.['en']
103
+
104
+ if (change.valueDataType === 'cds.Time') {
105
+ if (change.valueChangedFrom) change.valueChangedFrom = parseTime(change.valueChangedFrom, normalizedLocale, options);
106
+ if (change.valueChangedTo) change.valueChangedTo = parseTime(change.valueChangedTo, normalizedLocale, options);
107
+ } else {
108
+ const formatter = change.valueDataType === 'cds.Date' ? 'toLocaleDateString' : 'toLocaleString';
109
+ if (change.valueChangedFrom) change.valueChangedFrom = new Date(change.valueChangedFrom)[formatter](normalizedLocale, options);
110
+ if (change.valueChangedTo) change.valueChangedTo = new Date(change.valueChangedTo)[formatter](normalizedLocale, options);
111
+ }
112
+
102
113
  };
103
114
 
104
115
  const localizeLogFields = function (data, locale) {
105
116
  if (!locale) return
106
117
  for (const change of data) {
107
- _localizeModification(change, locale);
108
- _localizeAttribute(change, locale);
109
- _localizeEntityType(change, locale);
110
- _localizeDefaultObjectID(change, locale);
118
+ _localizeModification(change);
119
+ _localizeAttribute(change);
120
+ _localizeEntityType(change);
121
+ _localizeDefaultObjectID(change);
122
+ _localizeValue(change, locale);
111
123
  }
112
124
  };
113
125
  module.exports = {
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@cap-js/change-tracking",
3
- "version": "1.0.8",
3
+ "version": "1.1.1",
4
4
  "description": "CDS plugin providing out-of-the box support for automatic capturing, storing, and viewing of the change records of modeled entities.",
5
5
  "repository": "cap-js/change-tracking",
6
6
  "author": "SAP SE (https://www.sap.com)",
7
- "license": "SEE LICENSE IN LICENSE",
7
+ "license": "Apache-2.0",
8
8
  "main": "cds-plugin.js",
9
9
  "files": [
10
10
  "lib",
@@ -18,23 +18,23 @@
18
18
  "test": "npx jest --silent"
19
19
  },
20
20
  "peerDependencies": {
21
- "@sap/cds": ">=8"
21
+ "@sap/cds": ">=8.5"
22
+ },
23
+ "engines": {
24
+ "node": ">=20.0.0"
22
25
  },
23
26
  "devDependencies": {
24
27
  "@cap-js/change-tracking": "file:.",
25
- "@cap-js/sqlite": "^1",
26
- "axios": "^1",
27
- "chai": "^4.3.10",
28
- "chai-as-promised": "^7.1.1",
29
- "chai-subset": "^1.6.0",
30
- "eslint": "^8",
31
- "express": "^4",
32
- "jest": "^29"
28
+ "@cap-js/attachments": "^2",
29
+ "@cap-js/sqlite": "^1 || ^2",
30
+ "@cap-js/cds-test": "*",
31
+ "express": "^4"
33
32
  },
34
33
  "cds": {
35
34
  "requires": {
36
35
  "change-tracking": {
37
- "model": "@cap-js/change-tracking"
36
+ "model": "@cap-js/change-tracking",
37
+ "considerLocalizedValues": false
38
38
  }
39
39
  }
40
40
  }