-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibEffect.ts
More file actions
209 lines (192 loc) · 6.91 KB
/
libEffect.ts
File metadata and controls
209 lines (192 loc) · 6.91 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// --------------------------------------------------------
// Effect Functions
// --------------------------------------------------------
import type { AuxiaRouterConfig } from '../api/auxiaProxyRouter';
import {
buildGetTreatmentsRequestPayload,
buildLogTreatmentInteractionRequestPayload,
guDismissibleUserTreatment,
guMandatoryUserTreatment,
} from './libPure';
import type {
GateType,
GetTreatmentsRequestPayload,
UserTreatment,
UserTreatmentsEnvelop,
} from './types';
export const callAuxiaGetTreatments = async (
apiKey: string,
projectId: string,
browserId: string | undefined,
isSupporter: boolean,
dailyArticleCount: number,
articleIdentifier: string,
editionId: string,
countryCode: string,
hasConsented: boolean,
shouldServeDismissible: boolean,
): Promise<UserTreatmentsEnvelop | undefined> => {
// We now have clearance to call the Auxia API.
// If the browser id could not be recovered client side, then we do not call auxia
if (browserId === undefined) {
return;
}
const url = 'https://apis.auxia.io/v1/GetTreatments';
const headers = {
'Content-Type': 'application/json',
'x-api-key': apiKey,
};
const payload = buildGetTreatmentsRequestPayload(
projectId,
browserId,
isSupporter,
dailyArticleCount,
articleIdentifier,
editionId,
countryCode,
hasConsented,
shouldServeDismissible,
);
const params = {
method: 'POST',
headers: headers,
body: JSON.stringify(payload),
};
try {
const response = await fetch(url, params);
const responseBody = (await response.json()) as {
userTreatments: UserTreatment[] | undefined;
};
// nb: In some circumstances, for instance if the payload although having the right
// schema, is going to fail Auxia's validation then the response body may not contain
// the userTreatments field. In this case we return undefined.
if (responseBody['userTreatments'] === undefined) {
return Promise.resolve(undefined);
}
const data = responseBody as UserTreatmentsEnvelop;
return Promise.resolve(data);
} catch (error) {
return Promise.resolve(undefined);
}
};
export const gateTypeToUserTreatmentsEnvelop = async (
config: AuxiaRouterConfig,
gateType: GateType,
getTreatmentsRequestPayload: GetTreatmentsRequestPayload,
): Promise<UserTreatmentsEnvelop | undefined> => {
switch (gateType) {
case 'None':
return Promise.resolve(undefined);
case 'GuDismissible':
return {
responseId: '',
userTreatments: [guDismissibleUserTreatment()],
};
case 'GuMandatory':
return {
responseId: '',
userTreatments: [guDismissibleUserTreatment()],
};
case 'AuxiaAPI':
return await callAuxiaGetTreatments(
config.apiKey,
config.projectId,
getTreatmentsRequestPayload.browserId,
getTreatmentsRequestPayload.isSupporter,
getTreatmentsRequestPayload.dailyArticleCount,
getTreatmentsRequestPayload.articleIdentifier,
getTreatmentsRequestPayload.editionId,
getTreatmentsRequestPayload.countryCode,
getTreatmentsRequestPayload.hasConsented,
getTreatmentsRequestPayload.shouldServeDismissible,
);
case 'AuxiaAnalyticThenNone':
await callAuxiaGetTreatments(
config.apiKey,
config.projectId,
getTreatmentsRequestPayload.browserId,
getTreatmentsRequestPayload.isSupporter,
getTreatmentsRequestPayload.dailyArticleCount,
getTreatmentsRequestPayload.articleIdentifier,
getTreatmentsRequestPayload.editionId,
getTreatmentsRequestPayload.countryCode,
getTreatmentsRequestPayload.hasConsented,
getTreatmentsRequestPayload.shouldServeDismissible,
);
return Promise.resolve(undefined);
case 'AuxiaAnalyticThenGuDismissible':
await callAuxiaGetTreatments(
config.apiKey,
config.projectId,
getTreatmentsRequestPayload.browserId,
getTreatmentsRequestPayload.isSupporter,
getTreatmentsRequestPayload.dailyArticleCount,
getTreatmentsRequestPayload.articleIdentifier,
getTreatmentsRequestPayload.editionId,
getTreatmentsRequestPayload.countryCode,
getTreatmentsRequestPayload.hasConsented,
getTreatmentsRequestPayload.shouldServeDismissible,
);
return {
responseId: '',
userTreatments: [guDismissibleUserTreatment()],
};
case 'AuxiaAnalyticThenGuMandatory':
await callAuxiaGetTreatments(
config.apiKey,
config.projectId,
getTreatmentsRequestPayload.browserId,
getTreatmentsRequestPayload.isSupporter,
getTreatmentsRequestPayload.dailyArticleCount,
getTreatmentsRequestPayload.articleIdentifier,
getTreatmentsRequestPayload.editionId,
getTreatmentsRequestPayload.countryCode,
getTreatmentsRequestPayload.hasConsented,
getTreatmentsRequestPayload.shouldServeDismissible,
);
return {
responseId: '',
userTreatments: [guMandatoryUserTreatment()],
};
default:
console.error('Unknown direction');
}
};
export const callAuxiaLogTreatmentInteration = async (
apiKey: string,
projectId: string,
browserId: string | undefined,
treatmentTrackingId: string,
treatmentId: string,
surface: string,
interactionType: string,
interactionTimeMicros: number,
actionName: string,
): Promise<void> => {
// If the browser id could not be recovered client side, then we do not call auxia
if (browserId === undefined) {
return;
}
const url = 'https://apis.auxia.io/v1/LogTreatmentInteraction';
const headers = {
'Content-Type': 'application/json',
'x-api-key': apiKey,
};
const payload = buildLogTreatmentInteractionRequestPayload(
projectId,
browserId,
treatmentTrackingId,
treatmentId,
surface,
interactionType,
interactionTimeMicros,
actionName,
);
const params = {
method: 'POST',
headers: headers,
body: JSON.stringify(payload),
};
await fetch(url, params);
// We are not consuming an answer from the server, so we are not returning anything.
};