-
Notifications
You must be signed in to change notification settings - Fork 95
/
TCModelFactory.ts
144 lines (89 loc) · 3.39 KB
/
TCModelFactory.ts
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
import {TCModel, PurposeRestriction, RestrictionType} from '@iabtechlabtcf/core';
import {makeRandomInt} from './makeRandomInt.js';
import {GVLFactory} from './GVLFactory.js';
export class TCModelFactory {
public static noGVL(tcModel?: TCModel): TCModel {
const latestGVL = GVLFactory.getLatest();
if (!tcModel) {
tcModel = new TCModel();
}
tcModel.cmpId = makeRandomInt(2, 100);
tcModel.cmpVersion = makeRandomInt(1, 10);
tcModel.consentScreen = makeRandomInt(1, 5);
tcModel.isServiceSpecific = !!makeRandomInt(0, 1);
tcModel.vendorListVersion = makeRandomInt(1, latestGVL.vendorListVersion);
let counter = 0;
const rand = makeRandomInt(1, TCModel.consentLanguages.size);
TCModel.consentLanguages.forEach((lang: string): void => {
counter ++;
if (counter === rand) {
tcModel.consentLanguage = lang;
}
});
tcModel.publisherCountryCode = String.fromCharCode(makeRandomInt(65, 90)) +
String.fromCharCode(makeRandomInt(65, 90));
const date = new Date();
const utcDate = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()));
const now = utcDate.getTime();
const GDPRMageddon = 1576883249;
tcModel.created = new Date(makeRandomInt(GDPRMageddon, now));
tcModel.lastUpdated = new Date(makeRandomInt(GDPRMageddon, now));
const mapping = {
'purposes': [
'publisherConsents',
'publisherLegitimateInterests',
'purposeConsents',
'purposeLegitimateInterests',
],
'specialFeatures': [
'specialFeatureOptins',
],
'vendors': [
'vendorConsents',
'vendorLegitimateInterests',
'vendorsAllowed',
'vendorsDisclosed',
],
};
Object.keys(mapping).forEach((gvlIntMap: string): void => {
const ids = Object.keys(latestGVL[gvlIntMap]).map((strId: string): number => +strId);
ids.forEach((id: number): void => {
mapping[gvlIntMap].forEach((vectorName: string): void => {
// 75% chance of being set
if (makeRandomInt(1, 4) !== 3) {
tcModel[vectorName].set(id);
}
});
});
});
return tcModel;
}
public static addPublisherRestrictions(tcModel: TCModel): TCModel {
if (!tcModel.gvl) {
tcModel.gvl = GVLFactory.getLatest();
}
Object.keys(tcModel.gvl.vendors).forEach((vendorId: string): void => {
const vendor = tcModel.gvl.vendors[vendorId];
if (vendor.flexiblePurposes.length) {
const purposeId = vendor.flexiblePurposes[makeRandomInt(0, vendor.flexiblePurposes.length - 1)];
const isInConsent = vendor.purposes.includes(purposeId);
const notAllowed = makeRandomInt(0, 1) === 1;
let purpRestriction: PurposeRestriction;
if (notAllowed) {
purpRestriction = new PurposeRestriction(purposeId, RestrictionType.NOT_ALLOWED);
} else if (isInConsent) {
purpRestriction = new PurposeRestriction(purposeId, RestrictionType.REQUIRE_LI);
} else {
purpRestriction = new PurposeRestriction(purposeId, RestrictionType.REQUIRE_CONSENT);
}
tcModel.publisherRestrictions.add(+vendorId, purpRestriction);
}
});
return tcModel;
}
public static withGVL(): TCModel {
const tcModel = this.noGVL();
tcModel.gvl = GVLFactory.getLatest();
return tcModel;
}
}