Skip to content

Commit 2630ea5

Browse files
authored
Merge pull request #109 from SupahNickie/reprompt-options-fix
bugfix for getConsentsCount
2 parents e6e44b3 + 9f16dba commit 2630ea5

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

src/lib/cmp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class Cmp {
3939
else {
4040
const vendorConsents = store.getVendorConsentsObject();
4141
const publisherConsents = (config.storePublisherData && store.getPublisherConsentsObject()) || { lastUpdated: Date.now() }; // if publisher consent is not enabled mark - cookie as valid
42-
const shouldBePrompted = checkReprompt(config.repromptOptions, vendorConsents, publisherConsents);
42+
const shouldBePrompted = checkReprompt(config.repromptOptions, store.getVendorList(), vendorConsents, publisherConsents);
4343
const { testingMode } = config;
4444

4545
if (testingMode !== 'normal') {

src/lib/store.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ export default class Store {
236236
};
237237
};
238238

239+
getVendorList = () => {
240+
return this.vendorList;
241+
}
242+
239243
/**
240244
* Persist all consent data to the cookie. This data will NOT be filtered
241245
* by the vendorList and will include global consents set no matter what

src/lib/utils.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,26 @@ const EU_COUNTRY_CODES = new Set(metadata.countryCodes);
1919
const MAX_COOKIE_LIFESPAN_DAYS = metadata.maxCookieLifespanDays;
2020
const CONSENT_PROPS = [ 'purposeConsents', 'vendorConsents', 'customPurposes', 'standardPurposes' ];
2121

22-
function getConsentsCount(consentObject) {
22+
function getConsentsCount(consentObject, vendorList) {
2323
let total = 0;
2424
let consented = 0;
25+
const activeVendors = vendorList.vendors.reduce((acc, vendor) => {
26+
acc[vendor.id] = true;
27+
return acc;
28+
}, {});
2529

2630
for (let i = 0; i < CONSENT_PROPS.length; i++) {
2731
if (consentObject[CONSENT_PROPS[i]]) {
2832
let consents = consentObject[CONSENT_PROPS[i]];
2933
const indexes = Object.keys(consents);
30-
consents = indexes.map(index => consents[index]);
34+
if (CONSENT_PROPS[i] === 'vendorConsents') {
35+
consents = indexes.map(index => consents[index] && activeVendors[index]);
36+
total += Object.keys(activeVendors).length;
37+
} else {
38+
consents = indexes.map(index => consents[index]);
39+
total += indexes.length;
40+
}
3141

32-
total += indexes.length;
3342
consented += consents.filter(Boolean).length;
3443
}
3544
}
@@ -41,11 +50,11 @@ function getTimestamp(dateString) {
4150
return +(new Date(dateString));
4251
}
4352

44-
function checkReprompt(repromptOptions, vendorConsents, publisherConsents) {
53+
function checkReprompt(repromptOptions, vendorList, vendorConsents, publisherConsents) {
4554
const oldestCookieTime = Math.min(...[ vendorConsents.lastUpdated || 0, publisherConsents.lastUpdated || 0 ].map(getTimestamp));
4655

4756
const { total, consented } = [ vendorConsents, publisherConsents ].reduce((previous, current) => {
48-
current = getConsentsCount(current);
57+
current = getConsentsCount(current, vendorList);
4958
previous.total += current.total;
5059
previous.consented += current.consented;
5160
return previous;

src/lib/utils.test.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from './utils';
1616
import config from './config';
1717

18+
let vendorList = {vendors: [{id: 1}, {id: 3}, {id: 4}]};
1819

1920
describe('utils', () => {
2021
describe('checkReprompt', () => {
@@ -37,7 +38,7 @@ describe('utils', () => {
3738
"1": false,
3839
}
3940
};
40-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(true);
41+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(true);
4142
});
4243

4344
it('returns true if atleast one cookie is not set', () => {
@@ -58,7 +59,7 @@ describe('utils', () => {
5859
"1": false,
5960
}
6061
};
61-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(true);
62+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(true);
6263
});
6364

6465
it('returns false if for new cookies', () => {
@@ -80,7 +81,7 @@ describe('utils', () => {
8081
"1": false,
8182
}
8283
};
83-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(false);
84+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(false);
8485
});
8586

8687
it('handles reprompt options', () => {
@@ -92,6 +93,9 @@ describe('utils', () => {
9293
},
9394
"vendorConsents": {
9495
"1": true,
96+
"2": false,
97+
"3": true,
98+
"4": true,
9599
}
96100
};
97101
const publisherConsent = {
@@ -104,29 +108,29 @@ describe('utils', () => {
104108
}
105109
};
106110

107-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(false);
111+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(false);
108112

109113
vendorConsent.lastUpdated = Date.now() - 100 * day;
110-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(false);
114+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(false);
111115

112116
vendorConsent.lastUpdated = Date.now() - 361 * day;
113-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(true);
117+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(true);
114118
vendorConsent.lastUpdated = Date.now() - 500 * day;
115-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(true);
119+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(true);
116120

117121
vendorConsent.purposeConsents['1'] = false;
118122
vendorConsent.lastUpdated = Date.now() - 29 * day;
119-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(false);
123+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(false);
120124
vendorConsent.lastUpdated = Date.now() - 31 * day;
121-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(true);
125+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(true);
122126

123127
vendorConsent.vendorConsents['1'] = false;
124128
publisherConsent.customPurposes['1'] = false;
125129
publisherConsent.standardPurposes['1'] = false;
126130
vendorConsent.lastUpdated = Date.now() - 29 * day;
127-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(false);
131+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(false);
128132
vendorConsent.lastUpdated = Date.now() - 31 * day;
129-
expect(checkReprompt(repromptOptions, vendorConsent, publisherConsent)).eq(true);
133+
expect(checkReprompt(repromptOptions, vendorList, vendorConsent, publisherConsent)).eq(true);
130134
});
131135
});
132136

@@ -227,6 +231,16 @@ describe('utils', () => {
227231
describe('getConsentsCount', () => {
228232
let consentObject;
229233
beforeEach(() => {
234+
vendorList = {
235+
vendors: [
236+
{id: 1},
237+
{id: 2},
238+
{id: 3},
239+
{id: 4},
240+
{id: 5},
241+
{id: 6}
242+
]
243+
};
230244
consentObject = {
231245
"cookieVersion": 1,
232246
"cmpId": 1,
@@ -260,19 +274,19 @@ describe('utils', () => {
260274
});
261275

262276
it('returns total amount of required consents', () => {
263-
expect(getConsentsCount(consentObject).total).eq(16);
277+
expect(getConsentsCount(consentObject, vendorList).total).eq(16);
264278
consentObject.customPurposes['4'] = true;
265-
expect(getConsentsCount(consentObject).total).eq(17);
279+
expect(getConsentsCount(consentObject, vendorList).total).eq(17);
266280
delete consentObject.vendorConsents;
267-
expect(getConsentsCount(consentObject).total).eq(11);
281+
expect(getConsentsCount(consentObject, vendorList).total).eq(11);
268282
});
269283

270284
it('returns amount of required granted consents', () => {
271-
expect(getConsentsCount(consentObject).consented).eq(12);
285+
expect(getConsentsCount(consentObject, vendorList).consented).eq(12);
272286
consentObject.customPurposes['4'] = true;
273-
expect(getConsentsCount(consentObject).consented).eq(13);
287+
expect(getConsentsCount(consentObject, vendorList).consented).eq(13);
274288
delete consentObject.vendorConsents;
275-
expect(getConsentsCount(consentObject).consented).eq(9);
289+
expect(getConsentsCount(consentObject, vendorList).consented).eq(9);
276290
});
277291
});
278292

0 commit comments

Comments
 (0)