Skip to content

Commit

Permalink
consent and set
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Jun 27, 2024
1 parent 96c1b4e commit 2207fec
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import type { WalkerOS } from '@elbwalker/types';
import type { WebClient } from '../types';
import type { WebClient, WebDestination } from '../types';
import { assign } from '@elbwalker/utils';
import { onApply } from './on';
import { pushToDestination } from './push';

export function allowedToPush(
instance: WebClient.Instance,
destination: WebDestination.Destination,
): boolean {
// Default without consent handling
let granted = true;

// Check for consent
const destinationConsent = destination.config.consent;

if (destinationConsent) {
// Let's be strict here
granted = false;

// Set the current consent states
const consentStates = instance.consent;

// Search for a required and granted consent
Object.keys(destinationConsent).forEach((consent) => {
if (consentStates[consent]) granted = true;
});
}

return granted;
}

export function setConsent(
instance: WebClient.Instance,
data: WalkerOS.Consent,
Expand Down Expand Up @@ -43,7 +69,3 @@ export function setConsent(
});
}
}

export function setUserIds(instance: WebClient.Instance, data: WalkerOS.User) {
assign(instance.user, data, { shallow: false });
}
4 changes: 2 additions & 2 deletions packages/clients/walkerjs/src/lib/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { isElementOrDocument, isObject } from './helper';
import { Hooks, WalkerOS } from '@elbwalker/types';
import { initScopeTrigger, ready } from './trigger';
import { getState } from './state';
import { setConsent, setUserIds } from './set';
import { addDestination } from './destination';
import { on } from './on';
import { run } from './run';
import { pushToDestination } from './push';
import { addHook } from './hooks';
import { setConsent } from './consent';

export function handleCommand(
instance: WebClient.Instance,
Expand Down Expand Up @@ -57,7 +57,7 @@ export function handleCommand(
ready(instance, run, instance, data as Partial<WebClient.State>);
break;
case Const.Commands.User:
isObject(data) && setUserIds(instance, data as WalkerOS.User);
if (isObject(data)) assign(instance.user, data, { shallow: false });
break;
default:
break;
Expand Down
28 changes: 1 addition & 27 deletions packages/clients/walkerjs/src/lib/helper.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
import type { WalkerOS } from '@elbwalker/types';
import type { WebClient, WebDestination } from '../types';
import type { WebClient } from '../types';
import { Const, isSameType } from '@elbwalker/utils';
import { getEntities } from './walker';

export function allowedToPush(
instance: WebClient.Instance,
destination: WebDestination.Destination,
): boolean {
// Default without consent handling
let granted = true;

// Check for consent
const destinationConsent = destination.config.consent;

if (destinationConsent) {
// Let's be strict here
granted = false;

// Set the current consent states
const consentStates = instance.consent;

// Search for a required and granted consent
Object.keys(destinationConsent).forEach((consent) => {
if (consentStates[consent]) granted = true;
});
}

return granted;
}

export function createEventOrCommand(
instance: WebClient.Instance,
nameOrEvent: unknown,
Expand Down
3 changes: 2 additions & 1 deletion packages/clients/walkerjs/src/lib/push.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WalkerOS } from '@elbwalker/types';
import type { WebClient, WebDestination } from '../types';
import { allowedToPush, createEventOrCommand, isArgument } from './helper';
import { createEventOrCommand, isArgument } from './helper';
import { handleCommand, handleEvent } from './handle';
import {
Const,
Expand All @@ -10,6 +10,7 @@ import {
tryCatch,
useHooks,
} from '@elbwalker/utils';
import { allowedToPush } from './consent';

export function createPush(instance: WebClient.Instance): WebClient.Elb {
const push = (
Expand Down

0 comments on commit 2207fec

Please sign in to comment.