Skip to content

Commit

Permalink
account for TCF not using notices
Browse files Browse the repository at this point in the history
  • Loading branch information
eastandwestwind committed Sep 19, 2023
1 parent 7bce7a5 commit 4881704
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
9 changes: 5 additions & 4 deletions clients/fides-js/src/fides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ import {
import {
constructFidesRegionString,
debugLog,
experienceIsValidAndHasNotices,
experienceIsValid,
experienceHasNotices,
transformConsentToFidesUserPreference,
validateOptions,
isEmptyExperience,
} from "./lib/consent-utils";
import { dispatchFidesEvent } from "./lib/events";
import { fetchExperience } from "./services/fides/api";
Expand Down Expand Up @@ -217,7 +218,7 @@ const init = async ({
_Fides.geolocation = geolocation;
_Fides.options = options;
_Fides.initialized = true;
if (experienceHasNotices(experience)) {
if (experience && !isEmptyExperience(experience)) {
// at this point, pre-fetched experience contains no user consent, so we populate with the Fides cookie
updateExperienceFromCookieConsent(
experience as PrivacyExperience,
Expand Down Expand Up @@ -254,7 +255,7 @@ const init = async ({
`User location could not be obtained. Skipping overlay initialization.`
);
shouldInitOverlay = false;
} else if (!experienceHasNotices(experience)) {
} else if (!experience || isEmptyExperience(experience)) {
effectiveExperience = await fetchExperience(
fidesRegionString,
options.fidesApiUrl,
Expand All @@ -263,7 +264,7 @@ const init = async ({
);
}

if (experienceIsValidAndHasNotices(effectiveExperience, options)) {
if (experienceIsValid(effectiveExperience, options)) {
// Overwrite cookie consent with experience-based consent values
cookie.consent = buildCookieConsentForExperiences(
effectiveExperience as PrivacyExperience,
Expand Down
4 changes: 3 additions & 1 deletion clients/fides-js/src/lib/consent-types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export type EmptyExperience = {};

export interface FidesConfig {
// Set the consent defaults from a "legacy" Privacy Center config.json.
consent?: LegacyConsentConfig;
// Set the "experience" to be used for this Fides.js instance -- overrides the "legacy" config.
// If set, Fides.js will fetch neither experience config nor user geolocation.
// If not set or is empty, Fides.js will attempt to fetch its own experience config.
experience?: PrivacyExperience | {};
experience?: PrivacyExperience | EmptyExperience;
// Set the geolocation for this Fides.js instance. If *not* set, Fides.js will fetch its own geolocation.
geolocation?: UserGeolocation;
// Global options for this Fides.js instance. Fides provides defaults for all props except privacyCenterUrl
Expand Down
27 changes: 16 additions & 11 deletions clients/fides-js/src/lib/consent-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConsentContext } from "./consent-context";
import {
ComponentType,
ConsentMechanism,
EmptyExperience,
FidesOptions,
GpcStatus,
PrivacyExperience,
Expand All @@ -26,21 +27,25 @@ export const debugLog = (
}
};

/**
* Returns true if privacy experience is null or empty
*/
export const isEmptyExperience = (obj: unknown): obj is EmptyExperience =>
typeof obj === "object" && obj != null && Object.keys(obj).length === 0;

/**
* Returns true if privacy experience has notices
*/
export const experienceHasNotices = (
experience: PrivacyExperience | undefined | {}
): boolean => {
if (experience && Object.keys(experience).length >= 0) {
const privacyExperience = experience as PrivacyExperience;
return Boolean(
privacyExperience.privacy_notices &&
privacyExperience.privacy_notices.length > 0
);
}
return false;
};
): boolean =>
Boolean(
experience &&
!isEmptyExperience(experience) &&
// fixme: how to tell ts that privacy_notices exists on experience?
experience.privacy_notices &&
experience.privacy_notices.length > 0
);

/**
* Construct user location str to be ingested by Fides API
Expand Down Expand Up @@ -155,7 +160,7 @@ export const validateOptions = (options: FidesOptions): boolean => {
/**
* Determines whether experience is valid and relevant notices exist within the experience
*/
export const experienceIsValidAndHasNotices = (
export const experienceIsValid = (
effectiveExperience: PrivacyExperience | undefined | {},
options: FidesOptions
): boolean => {
Expand Down

0 comments on commit 4881704

Please sign in to comment.