Skip to content

Commit 53aeb3c

Browse files
nested
1 parent 00ef4f5 commit 53aeb3c

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

packages/connectors/datalayer/src/__tests__/mapping.test.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ describe('mapping', () => {
2323
quantity: 2,
2424
};
2525
const product2 = {
26-
data: {
27-
name: 'Cool Cap',
28-
size: 'one size',
29-
prize: 42,
30-
},
26+
item_id: 'xyz',
27+
item_name: 'Cool Cap',
28+
price: 42,
3129
};
3230

3331
beforeEach(() => {});
@@ -293,9 +291,14 @@ describe('mapping', () => {
293291
total: 'value',
294292
coupon: 'coupon',
295293
},
296-
// nested: {
297-
// type: { value: 'product' },
298-
// },
294+
nested: {
295+
type: { value: 'product' },
296+
data: {
297+
id: 'items.*.item_id',
298+
name: 'items.*.item_name',
299+
price: 'items.*.price',
300+
},
301+
},
299302
},
300303
},
301304
})!;
@@ -322,6 +325,29 @@ describe('mapping', () => {
322325
total: 555,
323326
coupon: 'SUMM3RS4L3',
324327
},
328+
context: {},
329+
nested: [
330+
{
331+
type: 'product',
332+
data: {
333+
id: 'abc',
334+
name: 'Everyday Ruck Snack',
335+
price: 420,
336+
},
337+
nested: [],
338+
context: {},
339+
},
340+
{
341+
type: 'product',
342+
data: {
343+
id: 'xyz',
344+
name: 'Cool Cap',
345+
price: 42,
346+
},
347+
nested: [],
348+
context: {},
349+
},
350+
],
325351
}),
326352
);
327353
});

packages/connectors/datalayer/src/mapping.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,50 @@ export function objToEvent(
5959
'source',
6060
];
6161

62-
const foo = eventMappingObjectValueKeys.reduce((acc, key) => {
62+
const objectValues = eventMappingObjectValueKeys.reduce((acc, key) => {
6363
const config = mapping[key];
6464
if (config) acc[key] = mapEntries(obj, config);
6565
return acc;
6666
}, {} as WalkerOS.AnyObject);
67-
event = { ...event, ...foo };
67+
event = { ...event, ...objectValues };
68+
69+
// @TODO
70+
const context = {};
71+
event.context = context;
72+
73+
if (mapping.nested) {
74+
const nested: WalkerOS.Entities = [];
75+
const config = mapping.nested;
76+
77+
const nestedData = mapEntries(obj, config.data ?? {});
78+
const maxLength = Math.max(
79+
...Object.values(nestedData)
80+
.filter((value) => Array.isArray(value))
81+
.map((array) => array.length),
82+
);
83+
84+
for (let i = 0; i < maxLength; i++) {
85+
const data = Object.entries(nestedData).reduce((acc, [key, value]) => {
86+
acc[key] = Array.isArray(value) ? value[i] : value;
87+
return acc;
88+
}, {} as WalkerOS.Properties);
89+
nested.push({
90+
type: String(
91+
getMappingValue(
92+
obj,
93+
config.type ?? { value: 'item' },
94+
undefined,
95+
i,
96+
),
97+
),
98+
data: data,
99+
nested: [],
100+
context,
101+
});
102+
}
103+
104+
event.nested = nested;
105+
}
68106
}
69107

70108
// Update the event name

packages/connectors/datalayer/src/types/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export type EventMappingObjectValues = {
3131
globals?: ObjectValue;
3232
custom?: ObjectValue;
3333
user?: ObjectValue;
34-
// nested?: WalkerOS.Entities; // @TODO
34+
nested?: Nested;
3535
consent?: ObjectValue;
3636
version?: ObjectValue;
3737
source?: ObjectValue;
@@ -48,6 +48,11 @@ export type EventMappingValues = {
4848
count?: Value;
4949
};
5050

51+
export type Nested = {
52+
type?: Value;
53+
data?: ObjectValue;
54+
};
55+
5156
export type ObjectValue = {
5257
[key: string]: Value;
5358
};

0 commit comments

Comments
 (0)