/* Hello, Tracy! Welcome to the Harri script!

Please save this script in a specific folder. That folder should be: ~/Documents/Harri/

Please save the file you get from Chris in that same folder!

Here's how you run it. Now that this file is open in VS Code, you're almost there!
1. Click "Terminal" at the top of your screen and click "New Terminal" (or use Control+Shift+`)
2. A little window will show up at the bottom of your screen. Make sure "Terminal" is highlighted.
3. Go to line 100 and make sure the path is the same where your files are saved (Documents/Harri). Make sure the date is accurate.
4. Click on this new terminal window and make sure you can type. Please copy and paste the following command:
" node ~/Documents/Harri/harri_csv.js", tap Enter.
5. If all goes well, you should see the script "working" by showing emails and uuids in the terminal.

*/
const fs = require("fs");
const { parse } = require("csv-parse");
const axios = require('axios');
var array = [];
const apiKey = 'RfmxChNLLodO6M0Z88BwG9Xyu'

function wait(ms) {
    return new Promise(r => setTimeout(r, ms));
}

function getUserProps(xApiKey, person) {

    axios({
            method: 'get',
            url: 'https://api.northpass.com/v2/properties/people/' + person,
            headers: {
                'accept': 'application/json',
                'x-api-key': xApiKey,
                'content-type': 'application/json'
            }
        })
        .then(res => {
            info = '"' + res.data.data.attributes.properties.account_name + '"' + ';' + '"' + res.data.data.attributes.properties.company + '"' + '\n'
            fs.appendFile('harriUserProps.csv', info, function(err) {
                if (err) throw err;
                console.log('Saved!');
                return 'saved'
            });
        })
        .catch(err => {
            console.log(err);
        });
};

function fetchPeople(xApiKey, users, num) {
    let index = num;
    axios({
            method: 'get',
            url: 'https://api.northpass.com/v2/people?filter[' + users[index][0] + ']=' + encodeURIComponent(users[index][1]),
            headers: {
                'accept': 'application/json',
                'x-api-key': xApiKey,
            }
        })
        .then(res => {
            if (index < users.length - 1) {
                console.log(index)
                console.log(users[index][0], users[index][1], res.data.data[0].id)
                getUserProps(xApiKey, res.data.data[0].id)
                index++;
                wait(300)
                fetchPeople(xApiKey, users, index)
                wait(300)
            } else {
                getUserProps(xApiKey, res.data.data[0].id)
                console.log("complete")

            }
        })
        .catch(err => {
            console.log(err);
        });
};

function csvReader(someArray, filePath) {
    fs.createReadStream(filePath)
        .pipe(parse({
            delimiter: ',',
            columns: true
        }))
        .on("data", function(row) {
            if (row['Email'] == '') {
                someArray.push(['sso_uid', row['SSO UID']])
            } else {
                someArray.push(['email', row['Email']])
            }
        })
        .on("error", function(error) {
            console.log(error.message);
        })
        .on("end", async function() {
            console.log("finished");
            fetchPeople(apiKey, someArray, 0)
        })
}

csvReader(array, "/Users/normrasmussen/Downloads/Harri Activation Report - 2023.06.19.csv")
