-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnetrustCmpAdapter.ts
More file actions
181 lines (160 loc) · 5.63 KB
/
OnetrustCmpAdapter.ts
File metadata and controls
181 lines (160 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import type OTPublishersNativeSDK from 'react-native-onetrust-cmp';
import { OTConsentInteraction, OTEventName } from 'react-native-onetrust-cmp';
import type { CmpAdapter } from '@contentpass/react-native-contentpass';
import type { BannerData, PreferenceCenterData } from './types';
import { getTcfPurposes } from './purposes';
export async function createOnetrustCmpAdapter(
sdk: OTPublishersNativeSDK
): Promise<OnetrustCmpAdapter> {
try {
const bannerData = await sdk.getBannerData();
const preferenceCenterData: PreferenceCenterData =
await sdk.getPreferenceCenterData();
return new OnetrustCmpAdapter(sdk, bannerData, preferenceCenterData);
} catch (error: any) {
console.error('Error getting banner or preference center data', error);
throw error;
}
}
export default class OnetrustCmpAdapter implements CmpAdapter {
private readonly groupIds: string[] = [];
private readonly numVendors: number = 0;
private readonly tcfPurposes: string[] = [];
private readonly eventListeners = new Set<
(eventName: OTEventName, data?: any) => void
>();
private readonly eventSubscriptions: Array<{ remove: () => void }> = [];
private readonly consentStatusChangeListeners = new Set<
(fullConsent: boolean) => void
>();
constructor(
private readonly sdk: OTPublishersNativeSDK,
bannerData: BannerData,
preferenceCenterData: PreferenceCenterData
) {
this.groupIds = preferenceCenterData.purposes
.map(({ groupId }) => groupId)
.filter(Boolean);
this.numVendors = OnetrustCmpAdapter.getNumVendors(
bannerData.bannerUIData?.summary?.description?.text ?? ''
);
this.tcfPurposes = getTcfPurposes(preferenceCenterData.purposes);
this.initializeEventBridge();
}
private static getNumVendors(description: string): number {
const match = description.match(/[0-9]+/);
return match ? parseInt(match[0], 10) : 0;
}
async waitForInit(): Promise<void> {
return Promise.resolve();
}
async acceptAll(): Promise<void> {
console.debug('[OnetrustCmpAdapter::acceptAll]');
await this.sdk.saveConsent(OTConsentInteraction.bannerAllowAll);
const hasFullConsent = await this.hasFullConsent();
this.emitConsentStatusChange(hasFullConsent);
}
async denyAll(): Promise<void> {
console.debug('[OnetrustCmpAdapter::denyAll]');
await this.sdk.saveConsent(OTConsentInteraction.bannerRejectAll);
const hasFullConsent = await this.hasFullConsent();
this.emitConsentStatusChange(hasFullConsent);
}
getNumberOfVendors(): Promise<number> {
return Promise.resolve(this.numVendors);
}
getRequiredPurposes(): Promise<string[]> {
return Promise.resolve(this.tcfPurposes);
}
showSecondLayer(view: 'vendor' | 'purpose'): Promise<void> {
console.debug('[OnetrustCmpAdapter::showSecondLayer]', view);
return new Promise<void>((resolve) => {
const remove = this.onEvent((eventName: OTEventName, _?: any) => {
switch (eventName) {
case OTEventName.hidePreferenceCenter:
case OTEventName.preferenceCenterAcceptAll:
case OTEventName.preferenceCenterRejectAll:
case OTEventName.preferenceCenterConfirmChoices:
case OTEventName.hideVendorList:
case OTEventName.vendorConfirmChoices:
case OTEventName.allSDKViewsDismissed:
remove();
resolve();
break;
default:
break;
}
});
if (view === 'vendor') {
this.sdk.showPreferenceCenterUI();
} else {
this.sdk.showConsentPurposesUI();
}
});
}
// FIXME handle reconsent scenarios
hasFullConsent = async (): Promise<boolean> => {
console.debug('[OnetrustCmpAdapter::hasFullConsent]');
const consentStatuses = await Promise.all(
this.groupIds.map((groupId) =>
this.sdk.getConsentStatusForCategory(groupId)
)
);
return consentStatuses.every(
(consentStatus: number) => consentStatus === 1
);
};
onConsentStatusChange(callback: (fullConsent: boolean) => void): () => void {
this.consentStatusChangeListeners.add(callback);
setTimeout(() => {
this.hasFullConsent().then((fullConsent) =>
this.emitConsentStatusChangeEventSingle(fullConsent, callback)
);
}, 0);
return () => this.consentStatusChangeListeners.delete(callback);
}
onEvent(callback: (eventName: OTEventName, data?: any) => void): () => void {
this.eventListeners.add(callback);
return () => {
this.eventListeners.delete(callback);
};
}
private initializeEventBridge(): void {
(Object.values(OTEventName) as OTEventName[]).forEach((eventName) => {
const subscription = this.sdk.addEventListener(
eventName,
(data?: any) => {
this.emitEvent(eventName, data);
}
);
this.eventSubscriptions.push(subscription);
});
}
private emitEvent(eventName: OTEventName, data?: any): void {
this.eventListeners.forEach((listener) => {
try {
listener(eventName, data);
} catch (error) {
console.error('[OnetrustCmpAdapter::onEvent] listener failed', error);
}
});
}
private emitConsentStatusChange(fullConsent: boolean): void {
this.consentStatusChangeListeners.forEach((listener) =>
this.emitConsentStatusChangeEventSingle(fullConsent, listener)
);
}
private emitConsentStatusChangeEventSingle(
fullConsent: boolean,
listener: (fullConsent: boolean) => void
): void {
try {
listener(fullConsent);
} catch (error) {
console.error(
'[OnetrustCmpAdapter::emitConsentStatusChangeEventSingle] listener failed',
error
);
}
}
}