@@ -31,11 +31,14 @@ import { signInGateTestIdToComponentId } from './SignInGate/signInGateMappings';
31
31
import type {
32
32
AuxiaAPIResponseDataUserTreatment ,
33
33
AuxiaGateDisplayData ,
34
+ AuxiaGateReaderPersonalData ,
34
35
AuxiaInteractionActionName ,
35
36
AuxiaInteractionInteractionType ,
37
+ AuxiaProxyGetTreatmentsPayload ,
38
+ AuxiaProxyGetTreatmentsResponse ,
39
+ AuxiaProxyLogTreatmentInteractionPayload ,
36
40
CheckoutCompleteCookieData ,
37
41
CurrentSignInGateABTest ,
38
- SDCAuxiaGetTreatmentsProxyResponse ,
39
42
SignInGateComponent ,
40
43
} from './SignInGate/types' ;
41
44
@@ -440,36 +443,21 @@ interface ShowSignInGateAuxiaProps {
440
443
abTest : CurrentSignInGateABTest ;
441
444
userTreatment : AuxiaAPIResponseDataUserTreatment ;
442
445
contributionsServiceUrl : string ;
443
- browserId : string ;
446
+ browserId : string | undefined ;
444
447
logTreatmentInteractionCall : (
445
448
interactionType : AuxiaInteractionInteractionType ,
446
449
actionName : AuxiaInteractionActionName ,
447
450
) => Promise < void > ;
448
451
}
449
452
450
- const decideBrowserIdWithConsentCheck = async ( ) : Promise <
451
- string | undefined
452
- > => {
453
- const hasConsent = await hasCmpConsentForBrowserId ( ) ;
454
- if ( ! hasConsent ) {
455
- return Promise . resolve ( undefined ) ;
456
- }
457
-
458
- const cookie = getCookie ( { name : 'bwid' , shouldMemoize : true } ) ;
459
- if ( cookie === null ) {
460
- return Promise . resolve ( undefined ) ;
461
- }
462
- return Promise . resolve ( cookie ) ;
463
- } ;
464
-
465
453
const decideIsSupporter = ( ) : boolean => {
466
454
// nb: We will not be calling the Auxia API if the user is signed in, so we can set isSignedIn to false.
467
455
const isSignedIn = false ;
468
- const is_supporter = shouldHideSupportMessaging ( isSignedIn ) ;
469
- if ( is_supporter === 'Pending' ) {
456
+ const isSupporter = shouldHideSupportMessaging ( isSignedIn ) ;
457
+ if ( isSupporter === 'Pending' ) {
470
458
return true ;
471
459
}
472
- return is_supporter ;
460
+ return isSupporter ;
473
461
} ;
474
462
475
463
const decideDailyArticleCount = ( ) : number => {
@@ -486,25 +474,40 @@ const decideDailyArticleCount = (): number => {
486
474
return 0 ;
487
475
} ;
488
476
477
+ const decideAuxiaProxyReaderPersonalData =
478
+ async ( ) : Promise < AuxiaGateReaderPersonalData > => {
479
+ const browserId =
480
+ getCookie ( { name : 'bwid' , shouldMemoize : true } ) ?? '' ;
481
+ const dailyArticleCount = decideDailyArticleCount ( ) ;
482
+ const hasConsent = await hasCmpConsentForBrowserId ( ) ;
483
+ const isSupporter = decideIsSupporter ( ) ;
484
+ const data = {
485
+ browserId : hasConsent ? browserId : undefined ,
486
+ dailyArticleCount,
487
+ isSupporter,
488
+ } ;
489
+ return Promise . resolve ( data ) ;
490
+ } ;
491
+
489
492
const fetchProxyGetTreatments = async (
490
493
contributionsServiceUrl : string ,
491
494
pageId : string ,
492
- browserId : string ,
493
- is_supporter : boolean ,
494
- daily_article_count : number ,
495
- ) : Promise < SDCAuxiaGetTreatmentsProxyResponse > => {
495
+ browserId : string | undefined ,
496
+ isSupporter : boolean ,
497
+ dailyArticleCount : number ,
498
+ ) : Promise < AuxiaProxyGetTreatmentsResponse > => {
496
499
// pageId example: 'money/2017/mar/10/ministers-to-criminalise-use-of-ticket-tout-harvesting-software'
497
- const article_identifier = `www.theguardian.com/${ pageId } ` ;
500
+ const articleIdentifier = `www.theguardian.com/${ pageId } ` ;
498
501
499
502
const url = `${ contributionsServiceUrl } /auxia/get-treatments` ;
500
503
const headers = {
501
504
'Content-Type' : 'application/json' ,
502
505
} ;
503
- const payload = {
506
+ const payload : AuxiaProxyGetTreatmentsPayload = {
504
507
browserId,
505
- is_supporter ,
506
- daily_article_count ,
507
- article_identifier ,
508
+ isSupporter ,
509
+ dailyArticleCount ,
510
+ articleIdentifier ,
508
511
} ;
509
512
const params = {
510
513
method : 'POST' ,
@@ -514,7 +517,7 @@ const fetchProxyGetTreatments = async (
514
517
515
518
const response_raw = await fetch ( url , params ) ;
516
519
const response =
517
- ( await response_raw . json ( ) ) as SDCAuxiaGetTreatmentsProxyResponse ;
520
+ ( await response_raw . json ( ) ) as AuxiaProxyGetTreatmentsResponse ;
518
521
519
522
return Promise . resolve ( response ) ;
520
523
} ;
@@ -523,25 +526,17 @@ const buildAuxiaGateDisplayData = async (
523
526
contributionsServiceUrl : string ,
524
527
pageId : string ,
525
528
) : Promise < AuxiaGateDisplayData | undefined > => {
526
- const browserId = await decideBrowserIdWithConsentCheck ( ) ;
527
- if ( browserId === undefined ) {
528
- return Promise . resolve ( undefined ) ;
529
- }
530
-
531
- const is_supporter = decideIsSupporter ( ) ;
532
- const daily_article_count = decideDailyArticleCount ( ) ;
533
-
529
+ const readerPersonalData = await decideAuxiaProxyReaderPersonalData ( ) ;
534
530
const response = await fetchProxyGetTreatments (
535
531
contributionsServiceUrl ,
536
532
pageId ,
537
- browserId ,
538
- is_supporter ,
539
- daily_article_count ,
533
+ readerPersonalData . browserId ,
534
+ readerPersonalData . isSupporter ,
535
+ readerPersonalData . dailyArticleCount ,
540
536
) ;
541
-
542
537
if ( response . status && response . data ) {
543
538
const answer = {
544
- browserId,
539
+ browserId : readerPersonalData . browserId ,
545
540
auxiaData : response . data ,
546
541
} ;
547
542
return Promise . resolve ( answer ) ;
@@ -555,14 +550,15 @@ const auxiaLogTreatmentInteraction = async (
555
550
userTreatment : AuxiaAPIResponseDataUserTreatment ,
556
551
interactionType : AuxiaInteractionInteractionType ,
557
552
actionName : AuxiaInteractionActionName ,
558
- browserId : string ,
553
+ browserId : string | undefined ,
559
554
) : Promise < void > => {
560
555
const url = `${ contributionsServiceUrl } /auxia/log-treatment-interaction` ;
561
556
const headers = {
562
557
'Content-Type' : 'application/json' ,
563
558
} ;
564
559
const microTime = Date . now ( ) * 1000 ;
565
- const payload = {
560
+
561
+ const payload : AuxiaProxyLogTreatmentInteractionPayload = {
566
562
browserId,
567
563
treatmentTrackingId : userTreatment . treatmentTrackingId ,
568
564
treatmentId : userTreatment . treatmentId ,
0 commit comments