cfernandomaciel
9/3/2017 - 9:30 PM

Email Dataset Manipulation - The new way

Email Dataset Manipulation - The new way

// Here is just a demonstration of how the dataset comes from the mongoose request
const emails = [{
        _id: 4632472,
        sender: 'zozimo@gmail.com',
        broadcasted: [{
                tracker_id: '111-222-333',
                email: 'claudio.fernando@gmail.com',
                file: '42348424284728421424/43216472462746242.pdf',
                name: 'claudio fernando maciel',
                sent: false,
                approved: 0,
                edited: false
            },
            {
                tracker_id: '222-333-444',
                email: 'jerry.lewis@gmail.com',
                file: '42348424284728421424/432164fdsafsadfas.pdf',
                name: 'jerry lewis',
                sent: false,
                approved: 1,
                edited: true
            }
        ]
    },
    {
        _id: 42384232,
        sender: 'klencher@gmail.com',
        broadcasted: [{
            tracker_id: '333-444-555',
            email: 'luiz@gmail.com',
            file: '42348424284728421424/45785hfsjdf.pdf',
            name: 'luiz diaz',
            sent: false,
            approved: 1,
            edited: true
        }]
    }
]

// Here is just a demonstration of how the dataset comes from the mongoose request
const users = [{
        name: 'claudio fernando',
        last_name: 'maciel',
        tracker_id: '111-222-333',
        cv: '42348424284728421424/editedstamped43216472462746242.pdf',
        email: 'claudio.fernando@gmail.com'
    },
    {
        name: 'jerry',
        last_name: 'lewis',
        tracker_id: '222-333-444',
        cv: '42348424284728421424/editedformated432164fdsafsadfas.pdf',
        email: 'jerry.lewis@gmail.com'
    },
    {
        name: 'luiz',
        last_name: 'diaz',
        tracker_id: '333-444-555',
        cv: '42348424284728421424/editedformated45785hfsjdf.pdf',
        email: 'luiz@gmail.com'
    }
]

// here's the real deal!
let sendableEmails =
    emails
    .map(x => x.broadcasted)
    .map((x, y) => x.map(z => {
        if (z.edited && !z.sent) return z.tracker_id
    }))
    .map((x, y) => {
        x[y] = x[y] || ''
        return x
    })
    .reduce((flat, toFlatten) => flat.concat(toFlatten), [])
    .filter(x => x !== '')

let changed =
    .map((x, y) => {
        if (sendableEmails.indexOf(x.tracker_id) != -1) {
            return x.tracker_id || ''
        }
    })
    .filter(x => x)

// much better, huh!?