Skip to content

Commit 5bb5236

Browse files
committed
[sign-in gate] code refactoring
1 parent 9853fb0 commit 5bb5236

File tree

5 files changed

+373
-358
lines changed

5 files changed

+373
-358
lines changed

src/server/api/auxiaProxyRouter.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import type express from 'express';
22
import { Router } from 'express';
33
import { isProd } from '../lib/env';
44
import { bodyContainsAllFields } from '../middleware';
5-
import type { GetTreatmentsRequestPayload } from '../signin-gate/lib';
5+
import { callAuxiaLogTreatmentInteration, gateTypeToUserTreatmentsEnvelop } from '../signin-gate/libEffect';
66
import {
7-
callAuxiaLogTreatmentInteration,
8-
gateTypeToUserTreatmentsEnvelop,
97
getTreatmentsRequestPayloadToGateType,
108
userTreatmentsEnvelopToProxyGetTreatmentsAnswerData,
11-
} from '../signin-gate/lib';
9+
} from '../signin-gate/libPure';
10+
import type { GetTreatmentsRequestPayload } from '../signin-gate/types';
1211
import { getSsmValue } from '../utils/ssm';
1312

1413
export interface AuxiaRouterConfig {

src/server/signin-gate/lib.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import type { GetTreatmentsRequestPayload } from './lib';
21
import {
32
articleIdentifierIsAllowed,
43
buildGetTreatmentsRequestPayload,
54
buildGuUserTreatmentsEnvelop,
65
buildLogTreatmentInteractionRequestPayload,
7-
GateType,
86
getTreatmentsRequestPayloadToGateType,
97
guDismissibleUserTreatment,
108
guMandatoryUserTreatment,
@@ -13,7 +11,9 @@ import {
1311
isValidTagIdCollection,
1412
mvtIdIsAuxiaAudienceShare,
1513
userTreatmentsEnvelopToProxyGetTreatmentsAnswerData,
16-
} from './lib';
14+
} from './libPure';
15+
import type { GetTreatmentsRequestPayload } from './types';
16+
import { GateType } from './types';
1717

1818
describe('buildGetTreatmentsRequestPayload', () => {
1919
it('should return return the right payload', () => {
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// --------------------------------------------------------
2+
// Effect Functions
3+
// --------------------------------------------------------
4+
5+
import type { AuxiaRouterConfig } from '../api/auxiaProxyRouter';
6+
import {
7+
buildGetTreatmentsRequestPayload,
8+
buildLogTreatmentInteractionRequestPayload,
9+
guDismissibleUserTreatment,
10+
guMandatoryUserTreatment,
11+
} from './libPure';
12+
import type { GetTreatmentsRequestPayload, UserTreatment, UserTreatmentsEnvelop } from './types';
13+
import { GateType } from './types';
14+
15+
export const callAuxiaGetTreatments = async (
16+
apiKey: string,
17+
projectId: string,
18+
browserId: string | undefined,
19+
isSupporter: boolean,
20+
dailyArticleCount: number,
21+
articleIdentifier: string,
22+
editionId: string,
23+
countryCode: string,
24+
hasConsented: boolean,
25+
shouldServeDismissible: boolean,
26+
): Promise<UserTreatmentsEnvelop | undefined> => {
27+
// We now have clearance to call the Auxia API.
28+
29+
// If the browser id could not be recovered client side, then we do not call auxia
30+
if (browserId === undefined) {
31+
return;
32+
}
33+
34+
const url = 'https://apis.auxia.io/v1/GetTreatments';
35+
36+
const headers = {
37+
'Content-Type': 'application/json',
38+
'x-api-key': apiKey,
39+
};
40+
41+
const payload = buildGetTreatmentsRequestPayload(
42+
projectId,
43+
browserId,
44+
isSupporter,
45+
dailyArticleCount,
46+
articleIdentifier,
47+
editionId,
48+
countryCode,
49+
hasConsented,
50+
shouldServeDismissible,
51+
);
52+
53+
const params = {
54+
method: 'POST',
55+
headers: headers,
56+
body: JSON.stringify(payload),
57+
};
58+
59+
try {
60+
const response = await fetch(url, params);
61+
const responseBody = (await response.json()) as {
62+
userTreatments: UserTreatment[] | undefined;
63+
};
64+
65+
// nb: In some circumstances, for instance if the payload although having the right
66+
// schema, is going to fail Auxia's validation then the response body may not contain
67+
// the userTreatments field. In this case we return undefined.
68+
if (responseBody['userTreatments'] === undefined) {
69+
return Promise.resolve(undefined);
70+
}
71+
const data = responseBody as UserTreatmentsEnvelop;
72+
return Promise.resolve(data);
73+
} catch (error) {
74+
return Promise.resolve(undefined);
75+
}
76+
};
77+
78+
export const gateTypeToUserTreatmentsEnvelop = async (
79+
config: AuxiaRouterConfig,
80+
gateType: GateType,
81+
getTreatmentsRequestPayload: GetTreatmentsRequestPayload,
82+
): Promise<UserTreatmentsEnvelop | undefined> => {
83+
switch (gateType) {
84+
case GateType.None:
85+
return Promise.resolve(undefined);
86+
case GateType.GuDismissible:
87+
return {
88+
responseId: '',
89+
userTreatments: [guDismissibleUserTreatment()],
90+
};
91+
case GateType.GuMandatory:
92+
return {
93+
responseId: '',
94+
userTreatments: [guDismissibleUserTreatment()],
95+
};
96+
case GateType.Auxia:
97+
return await callAuxiaGetTreatments(
98+
config.apiKey,
99+
config.projectId,
100+
getTreatmentsRequestPayload.browserId,
101+
getTreatmentsRequestPayload.isSupporter,
102+
getTreatmentsRequestPayload.dailyArticleCount,
103+
getTreatmentsRequestPayload.articleIdentifier,
104+
getTreatmentsRequestPayload.editionId,
105+
getTreatmentsRequestPayload.countryCode,
106+
getTreatmentsRequestPayload.hasConsented,
107+
getTreatmentsRequestPayload.shouldServeDismissible,
108+
);
109+
case GateType.AuxiaAnalyticThenNone:
110+
await callAuxiaGetTreatments(
111+
config.apiKey,
112+
config.projectId,
113+
getTreatmentsRequestPayload.browserId,
114+
getTreatmentsRequestPayload.isSupporter,
115+
getTreatmentsRequestPayload.dailyArticleCount,
116+
getTreatmentsRequestPayload.articleIdentifier,
117+
getTreatmentsRequestPayload.editionId,
118+
getTreatmentsRequestPayload.countryCode,
119+
getTreatmentsRequestPayload.hasConsented,
120+
getTreatmentsRequestPayload.shouldServeDismissible,
121+
);
122+
return Promise.resolve(undefined);
123+
case GateType.AuxiaAnalyticThenGuDismissible:
124+
await callAuxiaGetTreatments(
125+
config.apiKey,
126+
config.projectId,
127+
getTreatmentsRequestPayload.browserId,
128+
getTreatmentsRequestPayload.isSupporter,
129+
getTreatmentsRequestPayload.dailyArticleCount,
130+
getTreatmentsRequestPayload.articleIdentifier,
131+
getTreatmentsRequestPayload.editionId,
132+
getTreatmentsRequestPayload.countryCode,
133+
getTreatmentsRequestPayload.hasConsented,
134+
getTreatmentsRequestPayload.shouldServeDismissible,
135+
);
136+
return {
137+
responseId: '',
138+
userTreatments: [guDismissibleUserTreatment()],
139+
};
140+
case GateType.AuxiaAnalyticThenGuMandatory:
141+
await callAuxiaGetTreatments(
142+
config.apiKey,
143+
config.projectId,
144+
getTreatmentsRequestPayload.browserId,
145+
getTreatmentsRequestPayload.isSupporter,
146+
getTreatmentsRequestPayload.dailyArticleCount,
147+
getTreatmentsRequestPayload.articleIdentifier,
148+
getTreatmentsRequestPayload.editionId,
149+
getTreatmentsRequestPayload.countryCode,
150+
getTreatmentsRequestPayload.hasConsented,
151+
getTreatmentsRequestPayload.shouldServeDismissible,
152+
);
153+
return {
154+
responseId: '',
155+
userTreatments: [guMandatoryUserTreatment()],
156+
};
157+
default:
158+
console.error('Unknown direction');
159+
}
160+
};
161+
162+
export const callAuxiaLogTreatmentInteration = async (
163+
apiKey: string,
164+
projectId: string,
165+
browserId: string | undefined,
166+
treatmentTrackingId: string,
167+
treatmentId: string,
168+
surface: string,
169+
interactionType: string,
170+
interactionTimeMicros: number,
171+
actionName: string,
172+
): Promise<void> => {
173+
// If the browser id could not be recovered client side, then we do not call auxia
174+
if (browserId === undefined) {
175+
return;
176+
}
177+
178+
const url = 'https://apis.auxia.io/v1/LogTreatmentInteraction';
179+
180+
const headers = {
181+
'Content-Type': 'application/json',
182+
'x-api-key': apiKey,
183+
};
184+
185+
const payload = buildLogTreatmentInteractionRequestPayload(
186+
projectId,
187+
browserId,
188+
treatmentTrackingId,
189+
treatmentId,
190+
surface,
191+
interactionType,
192+
interactionTimeMicros,
193+
actionName,
194+
);
195+
196+
const params = {
197+
method: 'POST',
198+
headers: headers,
199+
body: JSON.stringify(payload),
200+
};
201+
202+
await fetch(url, params);
203+
204+
// We are not consuming an answer from the server, so we are not returning anything.
205+
};

0 commit comments

Comments
 (0)