Skip to content

Commit

Permalink
currency mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Oct 15, 2024
1 parent 638f3a6 commit 327d213
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/destinations/node/meta/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe('Node Destination Meta', () => {
test('Mapping', async () => {
event.data.total = 42;
const custom: CustomEventConfig = {
currency: { default: 'EUR' },
value: 'data.total',
};

Expand All @@ -173,6 +174,7 @@ describe('Node Destination Meta', () => {

expect(custom_data).toEqual(
expect.objectContaining({
currency: 'EUR',
value: 42,
}),
);
Expand Down
9 changes: 7 additions & 2 deletions packages/destinations/node/meta/src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const mapEvent = (
): ServerEvent => {
mapping; // @TODO
const { data, user, source } = event;
const { value } = mapping.custom || {};
const { currency, value } = mapping.custom || {};

let userData = new UserData();
if (user) {
Expand All @@ -71,12 +71,17 @@ export const mapEvent = (
}

const customData = new CustomData();

// Currency
const currencyParams = currency && getMappingValue(event, currency);
if (currencyParams) customData.setCurrency(String(currencyParams));

// Value
const valueParams = value && getMappingValue(event, value);
if (valueParams) customData.setValue(parseFloat(String(valueParams)));

// const content = new Content().setId('product123').setQuantity(1); // @TODO
// .setContents([content])
// .setCurrency('usd') // @TODO

const timestamp = Math.floor(
(event.timestamp || new Date().getTime()) / 1000,
Expand Down
1 change: 1 addition & 0 deletions packages/destinations/node/meta/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export interface CustomConfig {

export interface CustomEventConfig {
// Custom destination event mapping properties
currency?: WalkerOS.MappingValue;
value?: WalkerOS.MappingValue;
}
2 changes: 1 addition & 1 deletion packages/types/src/walkeros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export interface Entity {

export type MappingValue = string | MappingValueObject;
export interface MappingValueObject {
key: string;
key?: string;
default?: PropertyType;
// consent?: string | Array<string>;
}
12 changes: 6 additions & 6 deletions packages/utils/src/core/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { WalkerOS } from '@elbwalker/types';
import { castToProperty, getByStringDot } from '.';

interface MappingObject {
key: string;
key?: string;
defaultValue?: WalkerOS.PropertyType;
}

Expand All @@ -11,14 +11,14 @@ export function getMappingValue(
mapping: WalkerOS.MappingValue,
): WalkerOS.Property | undefined {
const obj = getMappingObject(mapping);
const value = castToProperty(
getByStringDot(event, obj.key, obj.defaultValue),
);
return value;
if (obj.key)
return castToProperty(getByStringDot(event, obj.key, obj.defaultValue));

return obj.defaultValue;
}

function getMappingObject(param: WalkerOS.MappingValue): MappingObject {
let key: string;
let key: string | undefined;
let defaultValue: WalkerOS.PropertyType | undefined;

if (typeof param == 'string') {
Expand Down

0 comments on commit 327d213

Please sign in to comment.