<commit_before>'use strict'

const canvasApi = require('../canvasApi')
require('colors')
const log = require('../server/logging')

function isInScope (msg) {
  var affArray = msg.affiliation
  const result = affArray && (affArray.includes('employee') || affArray.includes('student') || affArray.includes('member'))
  if (!result) {
    log.info('\nUser is not an employee and not a student, out of the affilication scope. User ' + msg.username + ' ' + msg.kthid + ' with affiliation ' + msg.affiliation)
  }
  return result
}

function convertToCanvasUser (msg) {
  log.info('\nConverting the user-type message to canvasApi format for: ' + msg.username + ' ' + msg.kthid, ' msg affiliation ', msg.affiliation)

  if (msg.username && (msg.given_name || msg.family_name) && msg.kthid) {
    let user = {
      pseudonym: {
        unique_id: `${<EMAIL>`, // CSVs analogi av 'login_id'
        sis_user_id: msg.kthid // CSVs analogi av 'user_id' needed for enrollments
      },
      user: {
        'name': `${msg.given_name} ${msg.family_name}`,
        'email': msg.primary_email, // must be when 'updating' user
        'sortable_name': `${msg.family_name}, ${msg.given_name}`,
        'short_name': null // a fix to make sure that display name is updated
      },
      communication_channel: { // must be when 'creating' user
        type: 'email',
        address: msg.primary_email
      }
    }
    return user
  } else {
    log.info('\nIncomplete fields to create user in canvas, skipping. Probably,it is missing a name(given_name, family_name) or a username or kth_id.....', msg)
  }
}

async function createOrUpdate (user) {
  try {
    const userFromCanvas = await canvasApi.getUser(user.pseudonym.sis_user_id)
    log.info('found user in canvas', userFromCanvas)
    log.info('update the user with new values: ', user)
    await canvasApi.updateUser(user, userFromCanvas.id)
  } catch (e) {
    if (e.statusCode === 404) {
      log.info('user doesnt exist in canvas. Create it.', user)
      const res = await canvasApi.createUser(user)
      log.info('Success! User created', res)
      return res
    } else {
      throw e
    }
  }
}

module.exports = async function (msg) {
  const user = convertToCanvasUser(msg)

  if (isInScope(msg) && user) {
    await createOrUpdate(user)<commit_msg>Added skip confirmation line to creaation of user<commit_after>'use strict'

const canvasApi = require('../canvasApi')
require('colors')
const log = require('../server/logging')

function isInScope (msg) {
  var affArray = msg.affiliation
  const result = affArray && (affArray.includes('employee') || affArray.includes('student') || affArray.includes('member'))
  if (!result) {
    log.info('\nUser is not an employee and not a student, out of the affilication scope. User ' + msg.username + ' ' + msg.kthid + ' with affiliation ' + msg.affiliation)
  }
  return result
}

function convertToCanvasUser (msg) {
  log.info('\nConverting the user-type message to canvasApi format for: ' + msg.username + ' ' + msg.kthid, ' msg affiliation ', msg.affiliation)

  if (msg.username && (msg.given_name || msg.family_name) && msg.kthid) {
    let user = {
      pseudonym: {
        unique_id: `${<EMAIL>`, // CSVs analogi av 'login_id'
        sis_user_id: msg.kthid // CSVs analogi av 'user_id' needed for enrollments
      },
      user: {
        'name': `${msg.given_name} ${msg.family_name}`,
        'email': msg.primary_email, // must be when 'updating' user
        'sortable_name': `${msg.family_name}, ${msg.given_name}`,
        'short_name': null // a fix to make sure that display name is updated
      },
      communication_channel: { // must be when 'creating' user
        type: 'email',
        address: msg.primary_email,
        skip_confirmation: true
      }
    }
    return user
  } else {
    log.info('\nIncomplete fields to create user in canvas, skipping. Probably,it is missing a name(given_name, family_name) or a username or kth_id.....', msg)
  }
}

async function createOrUpdate (user) {
  try {
    const userFromCanvas = await canvasApi.getUser(user.pseudonym.sis_user_id)
    log.info('found user in canvas', userFromCanvas)
    log.info('update the user with new values: ', user)
    await canvasApi.updateUser(user, userFromCanvas.id)
  } catch (e) {
    if (e.statusCode === 404) {
      log.info('user doesnt exist in canvas. Create it.', user)
      const res = await canvasApi.createUser(user)
      log.info('Success! User created', res)
      return res
    } else {
      throw e
    }
  }
}

module.exports = async function (msg) {
  const user = convertToCanvasUser(msg)

  if (isInScope(msg) && user) {
    await createOrUpdate(user)
{'fraction_non_alphanumeric': 0.13111253469997863, 'fraction_numerical': 0.0012812299807815502, 'mean_word_length': 3.130511463844797, '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': '1658520', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1467, 'n_words': 418}