Skip to content

Commit bc40fcd

Browse files
Merge pull request #17 from Kount/develo-k360-sfra
Add changes to support array in the response command mapping
2 parents 3ca65f6 + 0e898bd commit bc40fcd

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed

cartridges/int_kount_360_sfra/cartridge/kount360mappings/kount360ResMapping.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"TIMEZONE": "order.riskInquiry.device.deviceAttributes.timezoneOffset",
2020
"COUNTRY": "order.riskInquiry.device.location.countryCode",
2121
"REGION": "order.riskInquiry.device.location.region",
22-
"RULES_TRIGGERED": "order.riskInquiry.segmentExecuted.policiesExecuted",
23-
"RULE_ID_X": "order.riskInquiry.segmentExecuted.policiesExecuted.id",
24-
"RULE_DESCRIPTION_X": "order.riskInquiry.segmentExecuted.policiesExecuted.name",
22+
"RULES_TRIGGERED": "order.riskInquiry.segmentExecuted.policiesExecuted.length",
23+
"RULE_ID": "order.riskInquiry.segmentExecuted.policiesExecuted.[*].id",
24+
"RULE_DESCRIPTION": "order.riskInquiry.segmentExecuted.policiesExecuted.[*].name",
2525
"CARDS": "order.riskInquiry.persona.uniqueCards",
2626
"VELO": "order.riskInquiry.persona.maxVelocity",
2727
"GEOX": "order.riskInquiry.persona.riskiestCountry",

cartridges/int_kount_360_sfra/cartridge/scripts/utils/mappingUtils.js

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,34 @@ function getNestedValue(obj, path) {
7171
function transform(mapping, source, sourceToTarget, stringifyValue) {
7272
var result = {};
7373
var arrayGroups = {};
74+
var keysToFlatten = {}; // Track which keys should be flattened
7475

7576
Object.keys(mapping).forEach(function (key) {
7677
if (!Object.hasOwnProperty.call(mapping, key)) { return; }
7778
var from = sourceToTarget ? key : mapping[key];
7879
var to = sourceToTarget ? mapping[key] : key;
79-
var match = to.match(/^(.*?)\.\[\*\]\.(.+)$/);
80-
if (match) {
81-
var base = match[1];
82-
if (!arrayGroups[base]) arrayGroups[base] = [];
83-
arrayGroups[base].push({
84-
targetSubPath: match[2],
85-
sourcePath: from
80+
81+
// Check for [*] in source path (from) for response mapping
82+
var sourceMatch = from.match(/^(.*?)\.\[\*\]\.(.+)$/);
83+
if (sourceMatch && !sourceToTarget) {
84+
// For response mapping: source has [*], target is the base key
85+
if (!arrayGroups[to]) arrayGroups[to] = [];
86+
arrayGroups[to].push({
87+
targetSubPath: sourceMatch[2],
88+
sourcePath: sourceMatch[1]
8689
});
90+
keysToFlatten[to] = true; // Mark this key for flattening
91+
} else {
92+
// Original logic for request mapping: target has [*]
93+
var match = to.match(/^(.*?)\.\[\*\]\.(.+)$/);
94+
if (match) {
95+
var base = match[1];
96+
if (!arrayGroups[base]) arrayGroups[base] = [];
97+
arrayGroups[base].push({
98+
targetSubPath: match[2],
99+
sourcePath: from
100+
});
101+
}
87102
}
88103
});
89104

@@ -97,10 +112,22 @@ function transform(mapping, source, sourceToTarget, stringifyValue) {
97112
var subPath = group.targetSubPath;
98113
var sourcePath = group.sourcePath;
99114
var sourceVal = getNestedValue(source, sourcePath);
100-
var value = Array.isArray(sourceVal) ? sourceVal[arrIdx] : sourceVal;
101-
if (typeof value !== 'undefined') {
102-
var finalPath = key + '.' + arrIdx + '.' + subPath;
103-
setNestedValue(result, finalPath, value);
115+
116+
// For response mapping, sourceVal is the array, get element and extract subPath
117+
if (Array.isArray(sourceVal)) {
118+
var arrayElement = sourceVal[arrIdx];
119+
var value = subPath ? getNestedValue(arrayElement, subPath) : arrayElement;
120+
if (typeof value !== 'undefined') {
121+
var finalPath = key + '.' + arrIdx;
122+
setNestedValue(result, finalPath, stringifyValue ? String(value) : value);
123+
}
124+
} else {
125+
// Original logic for request mapping
126+
var value = Array.isArray(sourceVal) ? sourceVal[arrIdx] : sourceVal;
127+
if (typeof value !== 'undefined') {
128+
var finalPath = key + '.' + arrIdx + '.' + subPath;
129+
setNestedValue(result, finalPath, value);
130+
}
104131
}
105132
}
106133

@@ -140,6 +167,53 @@ function transform(mapping, source, sourceToTarget, stringifyValue) {
140167
}
141168
});
142169

170+
return flattenArrayNotation(result, keysToFlatten);
171+
}
172+
173+
/**
174+
* Flattens nested array structures created by [*] notation.
175+
* Converts { "RULE_ID": { "0": "value1", "1": "value2" } } or { "RULE_ID": ["value1", "value2"] }
176+
* to { "RULE_ID_0": "value1", "RULE_ID_1": "value2" }
177+
*
178+
* @param {Object} obj - The object to flatten.
179+
* @param {Object} keysToFlatten - Object with keys that should be flattened (optional).
180+
* @returns {Object} The flattened object.
181+
*/
182+
function flattenArrayNotation(obj, keysToFlatten) {
183+
var result = {};
184+
185+
Object.keys(obj).forEach(function (key) {
186+
var value = obj[key];
187+
188+
// Only flatten if this key is marked for flattening or no filter is provided
189+
var shouldFlatten = !keysToFlatten || keysToFlatten[key];
190+
191+
// Check if value is an array
192+
if (shouldFlatten && Array.isArray(value)) {
193+
// Flatten: RULE_ID[0] -> RULE_ID_0
194+
value.forEach(function (item, index) {
195+
result[key + '_' + index] = item;
196+
});
197+
} else if (shouldFlatten && value && typeof value === 'object') {
198+
// Check if value is an object with numeric keys (array-like structure)
199+
var keys = Object.keys(value);
200+
var isArrayLike = keys.length > 0 && keys.every(function (k) {
201+
return /^\d+$/.test(k);
202+
});
203+
204+
if (isArrayLike) {
205+
// Flatten: RULE_ID.0 -> RULE_ID_0
206+
keys.forEach(function (index) {
207+
result[key + '_' + index] = value[index];
208+
});
209+
} else {
210+
result[key] = value;
211+
}
212+
} else {
213+
result[key] = value;
214+
}
215+
});
216+
143217
return result;
144218
}
145219

0 commit comments

Comments
 (0)