Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
6.5 kB
2
Indexable
Never
// Function to initialize KnownUser object in Redux store
AARP = AARP || {};
AARP.checkCookie = (() => {
    const BP_CODE = 'bpcode';
    const checkForMerkury = () => {
        if (window?._aarpLoginStatus === null && window._svDataLayer && Object.keys(window?._svDataLayer).length !== 0 && window?._svDataLayer.hasOwnProperty('account_stat')) {
            const memberAutoRenewStatus = window?._svDataLayer?.auto_renewal_flag;
            const daysToExpire = window?._svDataLayer?.days_to_expire;
            let memberExpiry;
            const memberShipStatus = window?._svDataLayer?.account_stat;
            if (daysToExpire) {
                let date = new Date();
                date.setDate(date.getDate() + Number(daysToExpire));
                const dateFormatted = new Intl.DateTimeFormat("en-US", {
                    year: "numeric",
                    month: "2-digit",
                    day: "2-digit"
                }).format(date);
                memberExpiry = dateFormatted;
            }
            const ms = setStatus(window._svDataLayer.account_stat, 'merkury');
            return {
                ms,
                memberAutoRenewStatus,
                memberExpiry,
                memberShipStatus
            }
        }
        else {
            return {
                ms: 'ANON',
                memberAutoRenewStatus: 'N',
                memberExpiry: '',
                memberShipStatus: ''
            }
        }
    }
    const matchMsValWithState = (val) => {
        let state = 'ANON'
        if (val === '0') {
            state = 'MEMBER'
        }
        else if (val === '1') {
            state = 'NON-MEMBER'
        }
        else if (val === '4' || val === '5' || val === 'M' || val === 'U' || val === 'D') {
            state = 'EXPIRED'
        }
        return state;
    };
    const setStatus = (cookie, name) => {
        let type = 'ANON';
        switch (name) {
            case 'at':
                type = matchMsValWithState(cookie);
                break;
            case 'ufo':
                // If UFO has ms property
                if (cookie.hasOwnProperty('ms')) {
                    type = matchMsValWithState(cookie.ms)
                }
                else {
                    const expiry = cookie.x ? cookie.x : '';
                    if (new Date(expiry).getTime() >= new Date().getTime()) {
                        type = 'MEMBER'
                    }
                    else if (new Date(expiry).getTime() < new Date().getTime()) {
                        type = 'EXPIRED';
                    }
                    else if (cookie.hasOwnProperty('p') && (!cookie.hasOwnProperty('x') || !cookie.hasOwnProperty('j'))) {
                        type = 'NON-MEMBER'
                    }
                }
                break;
            case 'merkury':
                type = matchMsValWithState(cookie);
                break;
        }
        return type;
    };
    const checkBpCodeInQueryParams = () => {
        const urlParams = new URLSearchParams(window.location.search);
        const bpCode = urlParams?.get(BP_CODE);
        let isBpCodeValid = false;
        const date = new Date();
        if (bpCode) {
            // create a local storage variable with expiry date
            date.setDate(date.getDate() + 30);
            localStorage.setItem(BP_CODE, date);
            isBpCodeValid = true;
        } else {
            // check bpCode expiry date from local storage
            const expiryDate = localStorage.getItem(BP_CODE);
            if (expiryDate && new Date(expiryDate).getTime() > date.getTime()) {
                isBpCodeValid = true;
            }
        }
        return isBpCodeValid;
    }
    const initializeKnownUser = () => {
        console.log('check cookie initialization')
        const atCookie = AARP?.store?.getState()?.cookie?.at || '';
        const ufoCookie = AARP?.store?.getState()?.cookie?.ufo || '';
        let ms;
        const bpCode = checkBpCodeInQueryParams();
        if (bpCode) {
            const knownUser = AARP?.store?.getState()?.knownUser;
            AARP.store.dispatch({
                type: 'SET_KNOWN_USER_MS',
                payload: {
                    ms: knownUser?.memberState,
                    bpcode: bpCode,
                    memberAutoRenewStatus: knownUser?.memberAutoRenewStatus,
                    memberExpiry: knownUser?.memberExpiry,
                    memberShipStatus: knownUser?.memberShipStatus,
                }
            });
        }
        if (atCookie) {
            const msVal = atCookie.ms;
            ms = setStatus(msVal, 'at');
            AARP.store.dispatch({
                type: 'SET_KNOWN_USER_MS',
                payload: {
                    ms: ms,
                    bpcode: AARP?.store?.getState()?.knownUser?.bypassBpCode,
                    memberAutoRenewStatus: atCookie.mar,
                    memberExpiry: atCookie.me,
                    memberShipStatus: msVal
                }
            });
        } else if (ufoCookie) {
            ms = setStatus(ufoCookie, 'ufo');
            AARP.store.dispatch({
                type: 'SET_KNOWN_USER_MS',
                payload: {
                    ms: ms,
                    bpcode: AARP?.store?.getState()?.knownUser?.bypassBpCode,
                    memberAutoRenewStatus: ufoCookie.a,
                    memberExpiry: ufoCookie.x,
                    memberShipStatus: ufoCookie.ms
                }
            });
        }
        // Wait for dtm script load and listen for "_svtrackidentity_response"
        window.addEventListener('_svtrackidentity_response', (event) => {
            if (event.detail && atCookie === '' && ufoCookie === '') {
                let { ms, memberAutoRenewStatus, memberExpiry, memberShipStatus } = checkForMerkury();
                AARP.store.dispatch({
                    type: 'SET_KNOWN_USER_MS',
                    payload: {
                        ms: ms,
                        bpcode: AARP?.store?.getState()?.knownUser?.bypassBpCode,
                        memberShipStatus: memberShipStatus,
                        memberAutoRenewStatus: memberAutoRenewStatus,
                        memberExpiry: memberExpiry
                    }
                });
            }
        });
    };
    AARP.readyStateLoader(initializeKnownUser);
    return {
        initializeKnownUser: initializeKnownUser
    };
})();

Leave a Comment