Skip to content

Commit

Permalink
consentState source
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Mar 10, 2025
1 parent fbfc00d commit 0af28d9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/types/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type Loop = [Value, Value];
export type Map = { [key: string]: Value };

export interface Options {
consent?: WalkerOS.Consent;
instance?: WalkerOS.Instance;
props?: unknown;
}
Expand Down
75 changes: 59 additions & 16 deletions packages/utils/src/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,41 +328,84 @@ describe('getMappingValue', () => {
});

test('consent', () => {
const event = createEvent({ consent: { functional: true } });
const instance = {
consent: { functional: true },
consent: { instanceLevel: true },
} as unknown as WalkerOS.Instance;

// Granted
expect(instance.consent.instanceLevel).toBeTruthy();

// Denied
expect(
getMappingValue(
{ foo: 'bar' },
{
key: 'foo',
consent: { notGranted: true },
},
),
).toBeUndefined();

// eventsLevel
expect(
getMappingValue(
event,
{ foo: 'bar', consent: { eventLevel: true } },
{
key: 'data.string',
consent: { functional: true },
key: 'foo',
consent: { eventLevel: true },
},
{ instance },
),
).toBe(event.data.string);
).toBe('bar');

// Denied
// optionsLevel
expect(
getMappingValue(
{ foo: 'bar' },
{ key: 'foo', consent: { optionsLevel: true } },
{ consent: { optionsLevel: true } },
),
).toBe('bar');

// instanceLevel
expect(
getMappingValue(
{ foo: 'bar' },
{
key: 'foo',
consent: { instanceLevel: true },
},
{ instance },
),
).toBe('bar');

// eventsLevel override optionsLevel
expect(
getMappingValue(
{ foo: 'bar', consent: { eventLevel: false } },
{ key: 'foo', consent: { eventLevel: true } },
{ instance },
),
).toBeUndefined();

// eventLevel overrides instanceLevel
expect(
getMappingValue(
event,
{ foo: 'bar', consent: { instanceLevel: false } },
{
key: 'data.string',
consent: { marketing: true },
key: 'foo',
consent: { instanceLevel: true },
},
{ instance },
),
).toBeUndefined();

// Denied automatically if no instance is provided
// optionsLevel overrides instanceLevel
expect(
getMappingValue(event, {
key: 'data.string',
consent: { functional: true },
}),
getMappingValue(
{ foo: 'bar' },
{ key: 'foo', consent: { instanceLevel: true } },
{ instance, consent: { optionsLevel: false } },
),
).toBeUndefined();
});

Expand Down
18 changes: 14 additions & 4 deletions packages/utils/src/core/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Mapping, WalkerOS } from '@elbwalker/types';
import { getGrantedConsent } from './consent';
import { getByPath } from './byPath';
import { isArray, isDefined, isString } from './is';
import { isArray, isDefined, isString, isObject } from './is';
import { castToProperty } from './property';
import { tryCatch } from './tryCatch';

Expand Down Expand Up @@ -58,10 +58,20 @@ export function getMappingValue(
): WalkerOS.Property | undefined {
if (!isDefined(value)) return;

// Get consent state in priority order: value.consent > options.consent > instance?.consent
const consentState =
((isObject(value) && value.consent) as WalkerOS.Consent) ||
options.consent ||
options.instance?.consent;

const mappings = isArray(data) ? data : [data];

for (const mapping of mappings) {
const result = tryCatch(processMappingValue)(value, mapping, options);
const result = tryCatch(processMappingValue)(value, mapping, {
...options,
consent: consentState,
});

if (isDefined(result)) return result;
}
}
Expand All @@ -71,7 +81,7 @@ function processMappingValue(
mapping: Mapping.Value,
options: Mapping.Options = {},
): WalkerOS.Property | undefined {
const { instance } = options;
const { instance, consent: consentState } = options;

// Ensure mapping is an array for uniform processing
const mappings = isArray(mapping) ? mapping : [mapping];
Expand Down Expand Up @@ -100,7 +110,7 @@ function processMappingValue(
if (condition && !tryCatch(condition)(value, mappingItem, instance)) return;

// Check if consent is required and granted
if (consent && !getGrantedConsent(consent, instance?.consent))
if (consent && !getGrantedConsent(consent, consentState))
return staticValue;

let mappingValue: unknown = staticValue || value;
Expand Down
2 changes: 1 addition & 1 deletion website/docs/utils/mapping.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ to standard fields of the destination.
name: 'foo',
consent: {
functional: true,
// marketing: true
// marketing: true // uncomment me
},
},
{
Expand Down

0 comments on commit 0af28d9

Please sign in to comment.