<commit_before>const UrlHistory = require('./history');

class MemberAttributionService {
    /**
     *
     * @param {Object} deps
     * @param {Object} deps.attributionBuilder
     * @param {Object} deps.models
     * @param {Object} deps.models.MemberCreatedEvent
     * @param {Object} deps.models.SubscriptionCreatedEvent
     */
    constructor({attributionBuilder, models}) {
        this.models = models;
        this.attributionBuilder = attributionBuilder;
    }

    /**
     *
     * @param {import('./history').UrlHistoryArray} historyArray
     * @returns {Promise<import('./attribution').Attribution>}
     */
    async getAttribution(historyArray) {
        const history = UrlHistory.create(historyArray);
        return await this.attributionBuilder.getAttribution(history);
    }

    /**
     * Add some parameters to a URL so that the frontend script can detect this and add the required records
     * in the URLHistory.
     * @param {URL} url instance that will get updated
     * @param {Object} newsletter The newsletter from which a link was clicked
     * @returns {URL}
     */
    addEmailSourceAttributionTracking(url, newsletter) {
        // Create a deep copy
        url = new URL(url);
        url.searchParams.append('ref', newsletter.get('slug') + '-newsletter');
        return url;
    }

    /**
     * Add some parameters to a URL so that the frontend script can detect this and add the required records
     * in the URLHistory.
     * @param {URL} url instance that will get updated
     * @param {Object} post The post from which a link was clicked
     * @returns {URL}
     */
    addPostAttributionTracking(url, post) {
        // Create a deep copy
        url = new URL(url);

        // Post attribution
        url.searchParams.append('attribution_id', post.id);
        url.searchParams.append('attribution_type', 'post');
        return url;
    }

    /**
     * Returns the attribution resource for a given event model (MemberCreatedEvent / SubscriptionCreatedEvent), where the model has the required relations already loaded
     * You need to already load the 'postAttribution', 'userAttribution', and 'tagAttribution' relations
     * @param {Object} eventModel MemberCreatedEvent or SubscriptionCreatedEvent
     * @returns {import('./attribution').AttributionResource|null}
     */
    getEventAttribution(eventModel) {
        if (eventModel.get('attribution_type') === null) {
            return null;
        }
<commit_msg>Updated newsletter attribution to use the name rather than slug<commit_after>const UrlHistory = require('./history');

class MemberAttributionService {
    /**
     *
     * @param {Object} deps
     * @param {Object} deps.attributionBuilder
     * @param {Object} deps.models
     * @param {Object} deps.models.MemberCreatedEvent
     * @param {Object} deps.models.SubscriptionCreatedEvent
     */
    constructor({attributionBuilder, models}) {
        this.models = models;
        this.attributionBuilder = attributionBuilder;
    }

    /**
     *
     * @param {import('./history').UrlHistoryArray} historyArray
     * @returns {Promise<import('./attribution').Attribution>}
     */
    async getAttribution(historyArray) {
        const history = UrlHistory.create(historyArray);
        return await this.attributionBuilder.getAttribution(history);
    }

    /**
     * Add some parameters to a URL so that the frontend script can detect this and add the required records
     * in the URLHistory.
     * @param {URL} url instance that will get updated
     * @param {Object} newsletter The newsletter from which a link was clicked
     * @returns {URL}
     */
    addEmailSourceAttributionTracking(url, newsletter) {
        // Create a deep copy
        url = new URL(url);
        // We slugify the name here so that we don't use the default slugs in fixtures
        url.searchParams.append('ref', newsletter.get('name') + '-newsletter');
        return url;
    }

    /**
     * Add some parameters to a URL so that the frontend script can detect this and add the required records
     * in the URLHistory.
     * @param {URL} url instance that will get updated
     * @param {Object} post The post from which a link was clicked
     * @returns {URL}
     */
    addPostAttributionTracking(url, post) {
        // Create a deep copy
        url = new URL(url);

        // Post attribution
        url.searchParams.append('attribution_id', post.id);
        url.searchParams.append('attribution_type', 'post');
        return url;
    }

    /**
     * Returns the attribution resource for a given event model (MemberCreatedEvent / SubscriptionCreatedEvent), where the model has the required relations already loaded
     * You need to already load the 'postAttribution', 'userAttribution', and 'tagAttribution' relations
     * @param {Object} eventModel MemberCreatedEvent or SubscriptionCreatedEvent
     * @returns {import('./attribution').AttributionResource|null}
     */
    getEventAttribution(eventModel) {
        if (eventModel.get('attribution_type') === null) {
            return null;
        }

{'fraction_non_alphanumeric': 0.09990224828934506, 'fraction_numerical': 0.0, 'mean_word_length': 2.8236173393124067, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2351696', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1336, 'n_words': 426}