<commit_before>americano = require 'americano-cozy'
konnectorHash = require '../lib/konnector_hash'

log = require('printit')
    prefix: null
    date: true

module.exports = Konnector = americano.getModel 'Konnector',
    slug: String
    fieldValues: Object
    password: type: String, default: '{}'
    lastImport: Date
    lastAutoImport: Date
    isImporting: type: Boolean, default: false
    importInterval: type: String, default: 'none'

Konnector.all = (callback) ->
    Konnector.request 'all', (err, konnectors) ->
        konnectors.forEach (konnector) -> konnector.injectEncryptedFields()
        callback err, konnectors

Konnector::injectEncryptedFields = ->
    try
        parsedPasswords = JSON.parse @password
        for name, val of parsedPasswords
            @fieldValues[name] = val
    catch error
        log.info "Injecting encrypted fields : JSON.parse error : #{error}"

Konnector::removeEncryptedFields = (fields) ->

    if not fields?
        log.info "Removing encrypted fields : error : fields variable undefined"

    password = {}
    for name, type of fields
        if type is "password"
            password[name] = @fieldValues[name]
            delete @fieldValues[name]
    @password = JSON.stringify password

Konnector::import = (konnector, fields, callback) ->
    @fieldValues = konnector.fieldValues
    @isImporting = true
    @removeEncryptedFields fields
    @importInterval = konnector.importInterval
    @save (err) =>

        if err?
            data =
                isImporting: false
                lastImport: new Date()
            @updateAttributes data, callback

        else
            konnectorModule = require "../konnectors/#{@slug}"
            @injectEncryptedFields()
            konnectorModule.fetch @fieldValues, (err) =>
                @removeEncryptedFields fields

                if err?
                    data = isImporting: false
                    @updateAttributes data, callback

                else
                    data =
                        isImporting: false
                        lastImport: new Date()<commit_msg>Fixed: errors during import were not raised<commit_after>americano = require 'americano-cozy'
konnectorHash = require '../lib/konnector_hash'

log = require('printit')
    prefix: null
    date: true

module.exports = Konnector = americano.getModel 'Konnector',
    slug: String
    fieldValues: Object
    password: type: String, default: '{}'
    lastImport: Date
    lastAutoImport: Date
    isImporting: type: Boolean, default: false
    importInterval: type: String, default: 'none'

Konnector.all = (callback) ->
    Konnector.request 'all', (err, konnectors) ->
        konnectors.forEach (konnector) -> konnector.injectEncryptedFields()
        callback err, konnectors

Konnector::injectEncryptedFields = ->
    try
        parsedPasswords = JSON.parse @password
        for name, val of parsedPasswords
            @fieldValues[name] = val
    catch error
        log.info "Injecting encrypted fields : JSON.parse error : #{error}"

Konnector::removeEncryptedFields = (fields) ->

    if not fields?
        log.info "Removing encrypted fields : error : fields variable undefined"

    password = {}
    for name, type of fields
        if type is "password"
            password[name] = @fieldValues[name]
            delete @fieldValues[name]
    @password = JSON.stringify password

Konnector::import = (konnector, fields, callback) ->
    @fieldValues = konnector.fieldValues
    @isImporting = true
    @removeEncryptedFields fields
    @importInterval = konnector.importInterval
    @save (err) =>

        if err?
            data =
                isImporting: false
                lastImport: new Date()
            @updateAttributes data, callback

        else
            konnectorModule = require "../konnectors/#{@slug}"
            @injectEncryptedFields()
            konnectorModule.fetch @fieldValues, (err) =>
                @removeEncryptedFields fields

                if err?
                    data = isImporting: false
                    @updateAttributes data, ->
                        # raise the error from the import, not the update
                        callback err

                else
                    data =
                        isImporting: false
                        lastImport: new Date()
{'fraction_non_alphanumeric': 0.08808053077099062, 'fraction_numerical': 0.0, 'mean_word_length': 2.1228571428571428, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '1660727', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1134, 'n_words': 317}