-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgutterRouter.ts
More file actions
98 lines (90 loc) · 3.02 KB
/
gutterRouter.ts
File metadata and controls
98 lines (90 loc) · 3.02 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
import express, { Router } from 'express';
import { bodyContainsAllFields } from '../middleware';
import { getQueryParams, Params } from '../lib/params';
import { baseUrl } from '../lib/env';
import {
GutterProps,
GutterTest,
TestTracking,
GutterTargeting,
Tracking,
hideSRMessagingForInfoPageIds,
} from '../../shared/types';
import { ChannelSwitches } from '../channelSwitches';
import { getDeviceType } from '../lib/deviceType';
import { ValueProvider } from '../utils/valueReloader';
import { selectGutterTest } from '../tests/gutters/gutterSelection';
import { buildGutterCampaignCode } from '../lib/tracking';
interface GutterDataResponse {
data?: {
module: {
name: string;
props: GutterProps;
};
meta: TestTracking;
};
}
export const buildGutterRouter = (
channelSwitches: ValueProvider<ChannelSwitches>,
tests: ValueProvider<GutterTest[]>,
): Router => {
const router = Router();
const buildGutterData = (
targeting: GutterTargeting,
baseUrl: string,
params: Params,
req: express.Request,
): GutterDataResponse => {
const { enableGutterLiveblogs } = channelSwitches.get();
if (!enableGutterLiveblogs) {
return {};
}
if (hideSRMessagingForInfoPageIds(targeting)) {
return {};
}
const testSelection = selectGutterTest(
targeting,
tests.get(),
getDeviceType(req),
params.force,
);
if (testSelection) {
const { test, variant, moduleName } = testSelection;
const testTracking: TestTracking = {
abTestName: test.name,
abTestVariant: variant.name,
campaignCode: buildGutterCampaignCode(test, variant),
componentType: 'ACQUISITIONS_OTHER', // TODO: TBC - ACQUISITIONS_GUTTER? Changes will need to be made to the Ophan pipeline.
};
return {
data: {
module: {
name: moduleName,
props: {
content: variant.content,
tracking: testTracking as Tracking, // PageTracking is added client-side
countryCode: targeting.countryCode,
},
},
meta: testTracking,
},
};
}
return { data: undefined };
};
router.post(
'/gutter-liveblog',
bodyContainsAllFields(['targeting']),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const { targeting } = req.body;
const params = getQueryParams(req.query);
const response = buildGutterData(targeting, baseUrl(req), params, req);
res.send(response);
} catch (error) {
next(error);
}
},
);
return router;
};