AffiliateAttribution.init = function()

in media/js/products/shared/affiliate-attribution.es6.js [138:246]


AffiliateAttribution.init = function () {
    return new window.Promise((resolve, reject) => {
        const cjEventParamValue = AffiliateAttribution.getCJEventParam();

        FxaProductButton.init()
            .then((flowParams) => {
                if (!flowParams) {
                    reject('FxA flow params are undefined.');
                    return;
                }

                const flowId = AffiliateAttribution.getQueryStringParam(
                    'flow_id',
                    flowParams
                );

                // If `cjevent` param exists in page URL.
                if (cjEventParamValue) {
                    // If marketing cookie already exists.
                    if (AffiliateAttribution.hasMarketingCookie()) {
                        const value = AffiliateAttribution.getMarketingCookie();
                        // Send the flow ID, cjevent param and cookie ID to the micro service.
                        AffiliateAttribution.fetch(
                            flowId,
                            cjEventParamValue,
                            value
                        )
                            .then((data) => {
                                AffiliateAttribution.setMarketingCookie(
                                    data.aic_id,
                                    data.expires
                                );
                                resolve();
                            })
                            .catch((e) => {
                                /**
                                 * If PUT throws a 404 due to unknown aic id then the cookie
                                 * is no longer active in CJMS and has been archived. In this
                                 * instance we should treat it as a new POST request instead.
                                 * See https://github.com/mozilla/bedrock/issues/11506
                                 */
                                if (e === 'Unknown aicID') {
                                    AffiliateAttribution.fetch(
                                        flowId,
                                        cjEventParamValue
                                    )
                                        .then((data) => {
                                            AffiliateAttribution.setMarketingCookie(
                                                data.aic_id,
                                                data.expires
                                            );
                                            resolve();
                                        })
                                        .catch((e) => {
                                            reject(e);
                                        });
                                } else {
                                    reject(e);
                                }
                            });
                    } else {
                        // Else just send the flow ID and cjevent param.
                        AffiliateAttribution.fetch(flowId, cjEventParamValue)
                            .then((data) => {
                                AffiliateAttribution.setMarketingCookie(
                                    data.aic_id,
                                    data.expires
                                );
                                resolve();
                            })
                            .catch((e) => {
                                reject(e);
                            });
                    }
                    // Else if no`cjevent` param exists but there is an marketing cookie set.
                } else if (AffiliateAttribution.hasMarketingCookie()) {
                    const value = AffiliateAttribution.getMarketingCookie();

                    // Send only the flow ID and cookie ID to the micro service.
                    AffiliateAttribution.fetch(flowId, null, value)
                        .then((data) => {
                            AffiliateAttribution.setMarketingCookie(
                                data.aic_id,
                                data.expires
                            );
                            resolve();
                        })
                        .catch((e) => {
                            /**
                             * If PUT throws a 404 due to unknown aic id then the cookie
                             * is no longer active in CJMS and has been archived. We can't
                             * treat this as a new affiliate request since there is no
                             * cjevent param, so instead we delete the existing marketing
                             * cookie rather than throw an unhandled error.
                             * See https://github.com/mozilla/bedrock/issues/11506
                             */
                            if (e === 'Unknown aicID') {
                                AffiliateAttribution.removeMarketingCookie();
                                resolve();
                            }
                            reject(e);
                        });
                }
            })
            .catch((e) => {
                reject(e);
            });
    });
};