/* This function needs to run either once a day OR after a webhook that contains the course name.
Date range: 2/1/23 - 7/31/23

Order of operations:
- Parse Webhook, if course name ==	Coaching Certification, assignment == Practice Coaching Session 3 (PSA),
Activity name == Step 1: Practice Coaching Session 3 - Recording Upload
- GET List Assignments: https://api.northpass.com/v2/assignments/{assignment_uuid}/submissions
- GET Person https://api.northpass.com/v2/people/{uuid}

Fill in columns: Date Submitted, Person's Name, Email, Assignment Name, Assignment Download Link.


UUIDS:
Activity: a94171bb-0184-433e-a62d-2b8c67ad196e
Course: 03bb0fa0-df1f-4f40-b550-b59b113fd79f
Assignment: a4e082a2-9416-4fdf-9ef6-a817e25d26ee
API Key: HWxj6VTNPwbc3WghFTPzr7SjE
*/

const sheet = SpreadsheetApp.getActiveSheet();
const apiKey = "HWxj6VTNPwbc3WghFTPzr7SjE";
var assign_uid = "c6e05c41-082e-4027-9e2e-49d3c5a7c5a0"
let page = 1;

function getAssignment() {
  clearPrevData()
  for (let page = 1; page <= 50; page++) {
    var api_url =
      'https://api.northpass.com/v2/assignments/'+assign_uid+'/submissions?page='+page;
    Logger.log(api_url)
    const settings = {
      async: true,
      crossDomain: true,
      method: 'GET',
      headers: {
        accept: 'application/json',
        'X-Api-Key': apiKey
      }
    }
    const sendMsg = UrlFetchApp.fetch(api_url, settings);
    var apiResponse = sendMsg.getContentText();
    var parsedata = JSON.parse(apiResponse);
    var data = parsedata["data"];
    if (data.length != 0){
      let rowArray = []
      for (item of data) {
        var submit_date = item["attributes"]["created_at"];
        var person = item["relationships"]["person"]["data"]["id"];
        Logger.log(person)
        var download_link = item["links"]["download"];
        var person_url = 'https://api.northpass.com/v2/people/'+person;
        const settings = {
          async: true,
          crossDomain: true,
          method: 'GET',
          headers: {
            accept: 'application/json',
            'X-Api-Key': apiKey
          }
        };
        const sendMsg = UrlFetchApp.fetch(person_url, settings);
        var uuidResponse = sendMsg.getContentText();
        var parsing = JSON.parse(uuidResponse);
        var info = parsing["data"]
        var full_name = info["attributes"]["full_name"];
        var email = info["attributes"]["email"];
        let rowArray = [submit_date, download_link, person, full_name, email];
        addToSheet(rowArray);
      };
    } else {
      
    }
  }
};

function clearPrevData() {
  var range = SpreadsheetApp
               .getActiveSheet()
               .getRange(2,1,100,15);
  range.clearContent()
}

function addToSheet(rowArray){
  Logger.log("Adding to sheet function")
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = Math.max(sheet.getLastRow(),1);

  sheet.insertRowAfter(lastRow);
  sheet.getRange(lastRow + 1, 1).setValue(rowArray[0]);
  sheet.getRange(lastRow + 1, 2).setValue(rowArray[3]);
  sheet.getRange(lastRow + 1, 3).setValue(rowArray[4]);
  sheet.getRange(lastRow + 1, 4).setValue(rowArray[1]);
}
