-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathepicRouter.ts
More file actions
230 lines (201 loc) · 7.57 KB
/
epicRouter.ts
File metadata and controls
230 lines (201 loc) · 7.57 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import express, { Router } from 'express';
import {
AmountsTests,
EpicProps,
EpicTargeting,
EpicTest,
EpicType,
EpicVariant,
TestTracking,
Tracking,
WeeklyArticleLog,
} from '../../shared/types';
import { getQueryParams, Params } from '../lib/params';
import { baseUrl } from '../lib/env';
import { ChannelSwitches } from '../channelSwitches';
import { Debug, findForcedTestAndVariant, findTestAndVariant } from '../tests/epics/epicSelection';
import { selectAmountsTestVariant } from '../lib/ab';
import { TickerDataProvider } from '../lib/fetchTickerData';
import { getReminderFields, countryCodeToCountryGroupId } from '../../shared/lib';
import { getArticleViewCounts } from '../lib/history';
import { logWarn } from '../utils/logging';
import { SuperModeArticle } from '../lib/superMode';
import { getDeviceType } from '../lib/deviceType';
import { ValueProvider } from '../utils/valueReloader';
import { BanditData } from '../bandit/banditData';
import { buildEpicCampaignCode } from '../lib/tracking';
interface EpicDataResponse {
data?: {
module: {
name: string;
props: EpicProps;
};
variant: EpicVariant;
meta: TestTracking;
};
debug?: Debug;
}
// Any hardcoded epic tests should go here. They will take priority over any tests from the epic tool.
const hardcodedEpicTests: EpicTest[] = [];
export const buildEpicRouter = (
channelSwitches: ValueProvider<ChannelSwitches>,
superModeArticles: ValueProvider<SuperModeArticle[]>,
articleEpicTests: ValueProvider<EpicTest[]>,
liveblogEpicTests: ValueProvider<EpicTest[]>,
choiceCardAmounts: ValueProvider<AmountsTests>,
tickerData: TickerDataProvider,
banditData: ValueProvider<BanditData[]>,
): Router => {
const router = Router();
const getArticleEpicTests = (
mvtId: number,
isForcingTest: boolean,
enableHardcodedEpicTests: boolean,
): EpicTest[] => {
try {
const hardcodedTests = enableHardcodedEpicTests ? hardcodedEpicTests : [];
if (isForcingTest) {
return [...hardcodedTests, ...articleEpicTests.get()];
}
return [...hardcodedTests, ...articleEpicTests.get()];
} catch (err) {
logWarn(`Error getting article epic tests: ${err}`);
return [];
}
};
const buildEpicData = (
targeting: EpicTargeting,
type: EpicType,
params: Params,
baseUrl: string,
req: express.Request,
): EpicDataResponse => {
const { enableEpics, enableSuperMode, enableHardcodedEpicTests } = channelSwitches.get();
if (!enableEpics) {
return {};
}
if (
targeting.pageId === 'info/privacy' ||
targeting.pageId === 'info/complaints-and-corrections' ||
targeting.pageId === 'about'
) {
return {};
}
const targetingMvtId = targeting.mvtId || 1;
const tests =
type === 'ARTICLE'
? getArticleEpicTests(targetingMvtId, !!params.force, enableHardcodedEpicTests)
: liveblogEpicTests.get();
const result = params.force
? findForcedTestAndVariant(tests, params.force)
: findTestAndVariant(
tests,
targeting,
getDeviceType(req),
enableSuperMode ? superModeArticles.get() : [],
banditData.get(),
params.debug,
);
if (!result.result) {
return { data: undefined, debug: result.debug };
}
const { test, variant } = result.result;
const tickerSettings =
variant.tickerSettings && tickerData.addTickerDataToSettings(variant.tickerSettings);
const showReminderFields =
variant.showReminderFields ?? getReminderFields(targeting.countryCode);
const contributionAmounts = choiceCardAmounts.get();
const requiredCountry = targeting.countryCode ?? 'GB';
const requiredRegion = countryCodeToCountryGroupId(requiredCountry);
const variantAmounts = selectAmountsTestVariant(
contributionAmounts,
requiredCountry,
requiredRegion,
targetingMvtId,
);
const propsVariant = {
...variant,
tickerSettings,
showReminderFields,
choiceCardAmounts: variantAmounts,
};
const testTracking: TestTracking = {
abTestName: test.name,
abTestVariant: variant.name,
campaignCode: buildEpicCampaignCode(test, variant),
campaignId: `epic_${test.campaignId || test.name}`,
componentType: 'ACQUISITIONS_EPIC',
products: ['CONTRIBUTION', 'MEMBERSHIP_SUPPORTER'],
labels: test.isSuperMode ? ['SUPER_MODE'] : undefined,
};
const props: EpicProps = {
variant: propsVariant,
tracking: testTracking as Tracking, // PageTracking is added client-side
articleCounts: getArticleViewCounts(
targeting.weeklyArticleHistory,
test.articlesViewedSettings?.periodInWeeks,
test.articlesViewedSettings?.tagIds,
),
countryCode: targeting.countryCode,
};
return {
data: {
variant: propsVariant,
meta: testTracking,
module: {
name: type === 'ARTICLE' ? 'ContributionsEpic' : 'ContributionsLiveblogEpic',
props,
},
},
debug: result.debug,
};
};
router.post(
'/epic',
(req: express.Request, res: express.Response, next: express.NextFunction): void => {
try {
const epicType: EpicType = 'ARTICLE';
const { targeting } = req.body;
const params = getQueryParams(req.query);
const response = buildEpicData(targeting, epicType, params, baseUrl(req), req);
// for response logging
res.locals.didRenderEpic = !!response.data;
res.locals.epicTargeting = {
weeklyArticleHistory: (targeting.weeklyArticleHistory ?? [])
.slice(0, 3)
.map((c: WeeklyArticleLog) => JSON.stringify(c)),
};
if (!!response.data) {
res.locals.epicSuperMode = (response.data.meta.labels ?? []).includes(
'SUPER_MODE',
);
}
res.send(response);
} catch (error) {
next(error);
}
},
);
router.post(
'/liveblog-epic',
(req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const epicType: EpicType = 'LIVEBLOG';
const { targeting } = req.body;
const params = getQueryParams(req.query);
const response = buildEpicData(targeting, epicType, params, baseUrl(req), req);
// for response logging
res.locals.didRenderEpic = !!response.data;
if (!!response.data) {
res.locals.epicSuperMode = (response.data.meta.labels ?? []).includes(
'SUPER_MODE',
);
}
res.send(response);
} catch (error) {
next(error);
}
},
);
return router;
};