Skip to content

Commit cbcfe9a

Browse files
committed
refactor GateType
1 parent 9cc949f commit cbcfe9a

File tree

4 files changed

+48
-46
lines changed

4 files changed

+48
-46
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
userTreatmentsEnvelopToProxyGetTreatmentsAnswerData,
1414
} from './libPure';
1515
import type { GetTreatmentsRequestPayload } from './types';
16-
import { GateType } from './types';
1716

1817
describe('buildGetTreatmentsRequestPayload', () => {
1918
it('should return return the right payload', () => {
@@ -341,7 +340,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
341340
gateDisplayCount: 0,
342341
};
343342
const gateType = getTreatmentsRequestPayloadToGateType(body);
344-
expect(gateType).toStrictEqual(GateType.GuDismissible);
343+
expect(gateType).toStrictEqual('GuDismissible');
345344
});
346345

347346
it('test shouldServeDismissible and showDefaultGate:dismissible interacting together', () => {
@@ -364,7 +363,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
364363
gateDisplayCount: 0,
365364
};
366365
const gateType = getTreatmentsRequestPayloadToGateType(body);
367-
expect(gateType).toStrictEqual(GateType.GuDismissible);
366+
expect(gateType).toStrictEqual('GuDismissible');
368367
});
369368

370369
it('showDefaultGate:mandatory overrides any other behavior', () => {
@@ -387,7 +386,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
387386
gateDisplayCount: 0,
388387
};
389388
const gateType = getTreatmentsRequestPayloadToGateType(body);
390-
expect(gateType).toStrictEqual(GateType.GuMandatory);
389+
expect(gateType).toStrictEqual('GuMandatory');
391390
});
392391

393392
it('showDefaultGate:dismissible overrides any other behavior', () => {
@@ -410,7 +409,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
410409
gateDisplayCount: 0,
411410
};
412411
const gateType = getTreatmentsRequestPayloadToGateType(body);
413-
expect(gateType).toStrictEqual(GateType.GuDismissible);
412+
expect(gateType).toStrictEqual('GuDismissible');
414413
});
415414

416415
it('non consenting users in Ireland, low dailyArticleCount', () => {
@@ -433,7 +432,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
433432
gateDisplayCount: 0,
434433
};
435434
const gateType = getTreatmentsRequestPayloadToGateType(body);
436-
expect(gateType).toStrictEqual(GateType.AuxiaAnalyticThenNone);
435+
expect(gateType).toStrictEqual('AuxiaAnalyticThenNone');
437436
});
438437

439438
it('non consenting users in Ireland, low dailyArticleCount=3 (start of gate showing)', () => {
@@ -456,7 +455,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
456455
gateDisplayCount: 0,
457456
};
458457
const gateType = getTreatmentsRequestPayloadToGateType(body);
459-
expect(gateType).toStrictEqual(GateType.GuDismissible);
458+
expect(gateType).toStrictEqual('GuDismissible');
460459
});
461460

462461
it('non consenting users in Ireland, low gateDisplayCount: 1 (still dismissible)', () => {
@@ -479,7 +478,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
479478
gateDisplayCount: 1,
480479
};
481480
const gateType = getTreatmentsRequestPayloadToGateType(body);
482-
expect(gateType).toStrictEqual(GateType.AuxiaAnalyticThenGuDismissible);
481+
expect(gateType).toStrictEqual('AuxiaAnalyticThenGuDismissible');
483482
});
484483

485484
it('non consenting users in Ireland, low gateDisplayCount: 3 (mandatory from now on)', () => {
@@ -502,7 +501,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
502501
gateDisplayCount: 3,
503502
};
504503
const gateType = getTreatmentsRequestPayloadToGateType(body);
505-
expect(gateType).toStrictEqual(GateType.AuxiaAnalyticThenGuMandatory);
504+
expect(gateType).toStrictEqual('AuxiaAnalyticThenGuMandatory');
506505
});
507506

508507
it('non consenting users in Ireland, low gateDisplayCount: 4 (mandatory from now on)', () => {
@@ -525,7 +524,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
525524
gateDisplayCount: 4,
526525
};
527526
const gateType = getTreatmentsRequestPayloadToGateType(body);
528-
expect(gateType).toStrictEqual(GateType.AuxiaAnalyticThenGuMandatory);
527+
expect(gateType).toStrictEqual('AuxiaAnalyticThenGuMandatory');
529528
});
530529

531530
it('invalid attribute: contentType', () => {
@@ -548,7 +547,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
548547
gateDisplayCount: 0,
549548
};
550549
const gateType = getTreatmentsRequestPayloadToGateType(body);
551-
expect(gateType).toStrictEqual(GateType.None);
550+
expect(gateType).toStrictEqual('None');
552551
});
553552

554553
it('invalid attribute: sectionId', () => {
@@ -571,7 +570,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
571570
gateDisplayCount: 0,
572571
};
573572
const gateType = getTreatmentsRequestPayloadToGateType(body);
574-
expect(gateType).toStrictEqual(GateType.None);
573+
expect(gateType).toStrictEqual('None');
575574
});
576575

577576
it('invalid attribute: tagIds', () => {
@@ -594,7 +593,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
594593
gateDisplayCount: 0,
595594
};
596595
const gateType = getTreatmentsRequestPayloadToGateType(body);
597-
expect(gateType).toStrictEqual(GateType.None);
596+
expect(gateType).toStrictEqual('None');
598597
});
599598

600599
it('invalid attribute: articleIdentifier', () => {
@@ -617,7 +616,7 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
617616
gateDisplayCount: 0,
618617
};
619618
const gateType = getTreatmentsRequestPayloadToGateType(body);
620-
expect(gateType).toStrictEqual(GateType.None);
619+
expect(gateType).toStrictEqual('None');
621620
});
622621

623622
it('should return a gate in the case of the Giulia experiment', () => {
@@ -640,6 +639,6 @@ describe('decideGateTypeFromGetTreatmentsRequestPayload', () => {
640639
gateDisplayCount: 0,
641640
};
642641
const gateType = getTreatmentsRequestPayloadToGateType(body);
643-
expect(gateType).toStrictEqual(GateType.GuDismissible);
642+
expect(gateType).toStrictEqual('GuDismissible');
644643
});
645644
});

src/server/signin-gate/libEffect.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import {
99
guDismissibleUserTreatment,
1010
guMandatoryUserTreatment,
1111
} from './libPure';
12-
import type { GetTreatmentsRequestPayload, UserTreatment, UserTreatmentsEnvelop } from './types';
13-
import { GateType } from './types';
12+
import type {
13+
GateType,
14+
GetTreatmentsRequestPayload,
15+
UserTreatment,
16+
UserTreatmentsEnvelop,
17+
} from './types';
1418

1519
export const callAuxiaGetTreatments = async (
1620
apiKey: string,
@@ -81,19 +85,19 @@ export const gateTypeToUserTreatmentsEnvelop = async (
8185
getTreatmentsRequestPayload: GetTreatmentsRequestPayload,
8286
): Promise<UserTreatmentsEnvelop | undefined> => {
8387
switch (gateType) {
84-
case GateType.None:
88+
case 'None':
8589
return Promise.resolve(undefined);
86-
case GateType.GuDismissible:
90+
case 'GuDismissible':
8791
return {
8892
responseId: '',
8993
userTreatments: [guDismissibleUserTreatment()],
9094
};
91-
case GateType.GuMandatory:
95+
case 'GuMandatory':
9296
return {
9397
responseId: '',
9498
userTreatments: [guDismissibleUserTreatment()],
9599
};
96-
case GateType.Auxia:
100+
case 'AuxiaAPI':
97101
return await callAuxiaGetTreatments(
98102
config.apiKey,
99103
config.projectId,
@@ -106,7 +110,7 @@ export const gateTypeToUserTreatmentsEnvelop = async (
106110
getTreatmentsRequestPayload.hasConsented,
107111
getTreatmentsRequestPayload.shouldServeDismissible,
108112
);
109-
case GateType.AuxiaAnalyticThenNone:
113+
case 'AuxiaAnalyticThenNone':
110114
await callAuxiaGetTreatments(
111115
config.apiKey,
112116
config.projectId,
@@ -120,7 +124,7 @@ export const gateTypeToUserTreatmentsEnvelop = async (
120124
getTreatmentsRequestPayload.shouldServeDismissible,
121125
);
122126
return Promise.resolve(undefined);
123-
case GateType.AuxiaAnalyticThenGuDismissible:
127+
case 'AuxiaAnalyticThenGuDismissible':
124128
await callAuxiaGetTreatments(
125129
config.apiKey,
126130
config.projectId,
@@ -137,7 +141,7 @@ export const gateTypeToUserTreatmentsEnvelop = async (
137141
responseId: '',
138142
userTreatments: [guDismissibleUserTreatment()],
139143
};
140-
case GateType.AuxiaAnalyticThenGuMandatory:
144+
case 'AuxiaAnalyticThenGuMandatory':
141145
await callAuxiaGetTreatments(
142146
config.apiKey,
143147
config.projectId,

src/server/signin-gate/libPure.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import type {
66
AuxiaAPIGetTreatmentsRequestPayload,
77
AuxiaAPILogTreatmentInteractionRequestPayload,
8+
GateType,
89
GetTreatmentsRequestPayload,
910
ProxyGetTreatmentsAnswerData,
1011
UserTreatment,
1112
UserTreatmentsEnvelop,
1213
} from './types';
13-
import { GateType } from './types';
1414

1515
export const buildGetTreatmentsRequestPayload = (
1616
projectId: string,
@@ -315,13 +315,13 @@ export const decideGateTypeNoneOrDismissible = (gateDismissCount: number): GateT
315315
// (We do not want users to have to dismiss the gate 6 times)
316316

317317
if (gateDismissCount > 5) {
318-
return GateType.None;
318+
return 'None';
319319
}
320320

321321
// -----------------------------------------------------------------------
322322
// We are now clear to show the default (dismissible) gu gate.
323323

324-
return GateType.GuDismissible;
324+
return 'GuDismissible';
325325
};
326326

327327
export const decideGuGateTypeNonConsentedIreland = (
@@ -332,7 +332,7 @@ export const decideGuGateTypeNonConsentedIreland = (
332332
// If we reach this point, we are in Ireland
333333

334334
if (dailyArticleCount < 3) {
335-
return GateType.AuxiaAnalyticThenNone;
335+
return 'AuxiaAnalyticThenNone';
336336
}
337337

338338
// gateDisplayCount was introduced to enrich the behavior of the default gate.
@@ -348,10 +348,10 @@ export const decideGuGateTypeNonConsentedIreland = (
348348
// -------------------------------------------------------------------------
349349

350350
if (gateDisplayCount >= 3) {
351-
return GateType.AuxiaAnalyticThenGuMandatory;
351+
return 'AuxiaAnalyticThenGuMandatory';
352352
}
353353

354-
return GateType.AuxiaAnalyticThenGuDismissible;
354+
return 'AuxiaAnalyticThenGuDismissible';
355355
};
356356

357357
export const getTreatmentsRequestPayloadToGateType = (
@@ -382,17 +382,17 @@ export const getTreatmentsRequestPayloadToGateType = (
382382
getTreatmentsRequestPayload.showDefaultGate !== undefined &&
383383
getTreatmentsRequestPayload.shouldServeDismissible
384384
) {
385-
return GateType.GuDismissible;
385+
return 'GuDismissible';
386386
}
387387

388388
// --------------------------------------------------------------
389389
// The attribute showDefaultGate overrides any other behavior
390390

391391
if (getTreatmentsRequestPayload.showDefaultGate) {
392392
if (getTreatmentsRequestPayload.showDefaultGate == 'mandatory') {
393-
return GateType.GuMandatory;
393+
return 'GuMandatory';
394394
} else {
395-
return GateType.GuDismissible;
395+
return 'GuDismissible';
396396
}
397397
}
398398

@@ -408,7 +408,7 @@ export const getTreatmentsRequestPayloadToGateType = (
408408
!isValidTagIdCollection(getTreatmentsRequestPayload.tagIds) ||
409409
!articleIdentifierIsAllowed(getTreatmentsRequestPayload.articleIdentifier)
410410
) {
411-
return GateType.None;
411+
return 'None';
412412
}
413413

414414
// --------------------------------------------------------------
@@ -419,7 +419,7 @@ export const getTreatmentsRequestPayloadToGateType = (
419419

420420
if (getTreatmentsRequestPayload.countryCode === 'IE') {
421421
if (mvtIdIsAuxiaAudienceShare(getTreatmentsRequestPayload.mvtId)) {
422-
return GateType.Auxia;
422+
return 'AuxiaAPI';
423423
} else {
424424
return decideGuGateTypeNonConsentedIreland(
425425
getTreatmentsRequestPayload.dailyArticleCount,
@@ -444,12 +444,12 @@ export const getTreatmentsRequestPayloadToGateType = (
444444
if (getTreatmentsRequestPayload.should_show_legacy_gate_tmp) {
445445
return decideGateTypeNoneOrDismissible(getTreatmentsRequestPayload.gateDismissCount);
446446
} else {
447-
return GateType.None;
447+
return 'None';
448448
}
449449
}
450450

451451
// --------------------------------------------------------------
452452
// Auxia share of the audience (outside Ireland)
453453

454-
return GateType.Auxia;
454+
return 'AuxiaAPI';
455455
};

src/server/signin-gate/types.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ export interface UserTreatmentsEnvelop {
6666
userTreatments: UserTreatment[];
6767
}
6868

69-
export enum GateType {
70-
None, // [1]
71-
GuDismissible, // [2]
72-
GuMandatory, // [3]
73-
Auxia, // [4]
74-
AuxiaAnalyticThenNone, // [5]
75-
AuxiaAnalyticThenGuDismissible, // [6]
76-
AuxiaAnalyticThenGuMandatory, // [7]
77-
}
69+
export type GateType =
70+
| 'None' // [1]
71+
| 'GuDismissible' // [2]
72+
| 'GuMandatory' // [3]
73+
| 'AuxiaAPI' // [4]
74+
| 'AuxiaAnalyticThenNone' // [5]
75+
| 'AuxiaAnalyticThenGuDismissible' // [6]
76+
| 'AuxiaAnalyticThenGuMandatory'; // [7]
7877

7978
// [1] Signals no gate to display
8079
// [2] Signals the Gu Dismissible gate

0 commit comments

Comments
 (0)