@cap-js/change-tracking 1.1.0 → 1.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/README.md +33 -1
  3. package/_i18n/i18n_ar.properties +55 -0
  4. package/_i18n/i18n_bg.properties +55 -0
  5. package/_i18n/i18n_cs.properties +55 -0
  6. package/_i18n/i18n_da.properties +55 -0
  7. package/_i18n/i18n_de.properties +55 -55
  8. package/_i18n/i18n_el.properties +55 -0
  9. package/_i18n/i18n_en.properties +1 -1
  10. package/_i18n/i18n_en_US_saptrc.properties +55 -0
  11. package/_i18n/i18n_es.properties +55 -51
  12. package/_i18n/i18n_es_MX.properties +55 -0
  13. package/_i18n/i18n_fi.properties +55 -0
  14. package/_i18n/i18n_fr.properties +55 -51
  15. package/_i18n/i18n_he.properties +55 -0
  16. package/_i18n/i18n_hr.properties +55 -0
  17. package/_i18n/i18n_hu.properties +55 -0
  18. package/_i18n/i18n_it.properties +55 -51
  19. package/_i18n/i18n_ja.properties +11 -3
  20. package/_i18n/i18n_kk.properties +55 -0
  21. package/_i18n/i18n_ko.properties +55 -0
  22. package/_i18n/i18n_ms.properties +55 -0
  23. package/_i18n/i18n_nl.properties +55 -0
  24. package/_i18n/i18n_no.properties +55 -0
  25. package/_i18n/i18n_pl.properties +55 -51
  26. package/_i18n/i18n_pt.properties +55 -51
  27. package/_i18n/i18n_ro.properties +55 -0
  28. package/_i18n/i18n_ru.properties +55 -51
  29. package/_i18n/i18n_sh.properties +55 -0
  30. package/_i18n/i18n_sk.properties +55 -0
  31. package/_i18n/i18n_sl.properties +55 -0
  32. package/_i18n/i18n_sv.properties +55 -0
  33. package/_i18n/i18n_th.properties +55 -0
  34. package/_i18n/i18n_tr.properties +55 -0
  35. package/_i18n/i18n_uk.properties +55 -0
  36. package/_i18n/i18n_vi.properties +55 -0
  37. package/_i18n/i18n_zh_CN.properties +10 -6
  38. package/_i18n/i18n_zh_TW.properties +55 -0
  39. package/index.cds +5 -4
  40. package/lib/change-log.js +25 -17
  41. package/lib/entity-helper.js +5 -5
  42. package/lib/format-options.js +66 -0
  43. package/lib/localization.js +53 -41
  44. package/lib/template-processor.js +15 -2
  45. package/package.json +4 -3
@@ -0,0 +1,66 @@
1
+ const formatOptions = {
2
+ 'cds.Date': {
3
+ 'de': { day: '2-digit', month: '2-digit', year: 'numeric' },
4
+ 'en': { day: 'numeric', month: 'short', year: 'numeric' },
5
+ 'es': { day: '2-digit', month: 'short', year: 'numeric' },
6
+ 'fi': { year: 'numeric', month: '2-digit', day: '2-digit' },
7
+ 'fr': { day: '2-digit', month: 'short', year: 'numeric' },
8
+ 'it': { day: '2-digit', month: 'short', year: 'numeric' },
9
+ 'ja': { year: 'numeric', month: '2-digit', day: '2-digit' },
10
+ 'pl': { day: '2-digit', month: 'short', year: 'numeric' },
11
+ 'pt': { day: '2-digit', month: 'short', year: 'numeric' },
12
+ 'ru': { day: '2-digit', month: 'short', year: 'numeric' },
13
+ 'zh-CN': { year: 'numeric', month: 'long', day: 'numeric' }
14
+ },
15
+ 'cds.DateTime': {
16
+ 'de': { day: '2-digit', month: '2-digit', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
17
+ 'el': { day: 'numeric', month: 'short', year: 'numeric',hour: 'numeric', minute: '2-digit', second: '2-digit' },
18
+ 'en': { day: 'numeric', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
19
+ 'es': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
20
+ 'es_MX': { day: '2-digit', month: 'short', year: 'numeric', hour: 'numeric', minute: '2-digit', second: '2-digit', hour12: true },
21
+ 'fi': { year: 'numeric', month: '2-digit', day: '2-digit',hour: 'numeric', minute: '2-digit', second: '2-digit' },
22
+ 'fr': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
23
+ 'it': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
24
+ 'ja': { year: 'numeric', month: '2-digit', day: '2-digit',hour: '2-digit', minute: '2-digit', second: '2-digit' },
25
+ 'pl': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
26
+ 'pt': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
27
+ 'ru': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
28
+ 'zh-CN': { year: 'numeric', month: 'long', day: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' }
29
+ },
30
+ 'cds.Timestamp': {
31
+ 'cs': { day: 'numeric', month: 'short', year: 'numeric',hour: 'numeric', minute: '2-digit', second: '2-digit' },
32
+ 'de': { day: '2-digit', month: '2-digit', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
33
+ 'el': { day: 'numeric', month: 'short', year: 'numeric',hour: 'numeric', minute: '2-digit', second: '2-digit' },
34
+ 'en': { day: 'numeric', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
35
+ 'es': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
36
+ 'es_MX': { day: '2-digit', month: 'short', year: 'numeric', hour: 'numeric', minute: '2-digit', second: '2-digit', hour12: true },
37
+ 'fi': { year: 'numeric', month: '2-digit', day: '2-digit',hour: 'numeric', minute: '2-digit', second: '2-digit' },
38
+ 'fr': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
39
+ 'it': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
40
+ 'ja': { year: 'numeric', month: '2-digit', day: '2-digit',hour: '2-digit', minute: '2-digit', second: '2-digit' },
41
+ 'pl': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
42
+ 'pt': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
43
+ 'ru': { day: '2-digit', month: 'short', year: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' },
44
+ 'zh-CN': { year: 'numeric', month: 'long', day: 'numeric',hour: '2-digit', minute: '2-digit', second: '2-digit' }
45
+ },
46
+ 'cds.Time': {
47
+ 'cs': { hour: 'numeric', minute: '2-digit', second: '2-digit' },
48
+ 'fi': { hour: 'numeric', minute: '2-digit', second: '2-digit' },
49
+ 'de': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
50
+ 'el': { hour: 'numeric', minute: '2-digit', second: '2-digit' },
51
+ 'en': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
52
+ 'es': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
53
+ 'es_MX': { hour: 'numeric', minute: '2-digit', second: '2-digit', hour12: true },
54
+ 'fr': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
55
+ 'it': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
56
+ 'ja': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
57
+ 'pl': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
58
+ 'pt': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
59
+ 'ru': { hour: '2-digit', minute: '2-digit', second: '2-digit' },
60
+ 'zh-CN': { hour: '2-digit', minute: '2-digit', second: '2-digit' }
61
+ }
62
+ };
63
+
64
+ module.exports = {
65
+ formatOptions
66
+ };
@@ -1,37 +1,21 @@
1
1
  const cds = require("@sap/cds/lib");
2
2
  const LOG = cds.log("change-log");
3
+ const { formatOptions } = require("./format-options");
3
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
  }
@@ -41,9 +25,8 @@ const _localizeDefaultObjectID = function (change, locale) {
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 = {
@@ -23,6 +23,18 @@ const _processElement = (processFn, row, key, elements, isRoot, pathSegments, pi
23
23
  }
24
24
  };
25
25
 
26
+ function retrieveFirstKey(target) {
27
+ const keys = [];
28
+ for (const key of Object.keys(target.keys)) {
29
+ if (target.keys[key].type !== 'cds.Association' && key !== 'IsActiveEntity') {
30
+ keys.push(key);
31
+ }
32
+ }
33
+
34
+ //Revisit: For test 3.5
35
+ return keys.filter(k => !k.startsWith('up_'))[0]
36
+ }
37
+
26
38
  const _processRow = (processFn, row, template, tKey, tValue, isRoot, pathOptions) => {
27
39
  const { template: subTemplate, picked } = tValue;
28
40
  const key = tKey.split(DELIMITER).pop();
@@ -47,7 +59,8 @@ const _processRow = (processFn, row, template, tKey, tValue, isRoot, pathOptions
47
59
  * Construct path from root entity to current entity.
48
60
  */
49
61
  const serviceNodeName = template.target.elements[key].target;
50
- subRow._path = `${row._path}/${serviceNodeName}(${subRow.ID})`;
62
+ const serviceNode = template.target.elements[key]._target;
63
+ subRow._path = `${row._path}/${serviceNodeName}(${subRow[retrieveFirstKey(serviceNode)]})`;
51
64
  }
52
65
  });
53
66
 
@@ -95,4 +108,4 @@ const templateProcessor = ({ processFn, row, template, isRoot = true, pathOption
95
108
  }
96
109
  };
97
110
 
98
- module.exports = templateProcessor;
111
+ module.exports = {templateProcessor, retrieveFirstKey};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/change-tracking",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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)",
@@ -18,7 +18,7 @@
18
18
  "test": "npx jest --silent"
19
19
  },
20
20
  "peerDependencies": {
21
- "@sap/cds": ">=8"
21
+ "@sap/cds": ">=8.5"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">=20.0.0"
@@ -33,7 +33,8 @@
33
33
  "cds": {
34
34
  "requires": {
35
35
  "change-tracking": {
36
- "model": "@cap-js/change-tracking"
36
+ "model": "@cap-js/change-tracking",
37
+ "considerLocalizedValues": false
37
38
  }
38
39
  }
39
40
  }