AffiliateAttribution.fetch = function()

in media/js/products/shared/affiliate-attribution.es6.js [79:132]


AffiliateAttribution.fetch = function (flowId, cjID, value) {
    return new window.Promise((resolve, reject) => {
        const endpoint = AffiliateAttribution.getCJMSEndpoint();

        if (!endpoint) {
            reject('CJMS endpoint was not found.');
            return;
        }

        const endpointPath =
            typeof value === 'string' ? `${endpoint}/${value}` : endpoint;
        const method = typeof value === 'string' ? 'PUT' : 'POST';

        const obj = cjID
            ? { flow_id: flowId, cj_id: cjID }
            : { flow_id: flowId };

        window
            .fetch(endpointPath, {
                method: method,
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(obj)
            })
            .then((resp) => {
                if (resp.status >= 200 && resp.status <= 299) {
                    return resp.json();
                } else if (resp.status === 404 && method === 'PUT') {
                    return 'Unknown aicID';
                } else {
                    return resp
                        .text()
                        .then((message) => {
                            return message;
                        })
                        .catch((e) => {
                            return e;
                        });
                }
            })
            .then((resp) => {
                if (resp.aic_id && resp.expires) {
                    resolve(resp);
                } else {
                    reject(resp);
                }
            })
            .catch((e) => {
                reject(e);
            });
    });
};