<commit_before>'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose');
var Club = require('../models/Club');
var Q = require('q');
//var async = require('async');
var ObjectId = mongoose.Types.ObjectId;





/**
 * Update Club Fields
 * @param req
 * @param res
 */
module.exports.updateFields = function(req, res) {
    console.log(req.body);
    //console.log(req.body);

    console.log(req.body.deletedFields);

    if(req.body.modifiedFields.length > 0) {
        updateField(req.params._id, req.body.modifiedFields);
    }

    if(req.body.newFields.length > 0) {
        addField(req.params._id, req.body.newFields);
    }

    if(req.body.deletedFields.length > 0) {
        deleteField(req.params._id, req.body.deletedFields);
    }

};

function updateField(clubID, modifiedFields) {

 /*   Club.update(
        {_id: clubID}, //query, you can also query for email
        {$set: {fields: modifiedFields}},
        {multi: true} //for multiple documents
    )*/

    Club.findById(clubID, function(err, club) {
        // Handle any possible database errors
        if (err) {
            return res.status(500).send(err);
        } else {
            club.fields = modifiedFields;
            // Save the updated document back to the database
            club.save(function(err, club) {
                if (err) {
                    return res.status(500).send(err);
                }
                res.status(200).json(club);
            });
        }
    });


}


async function deleteField(clubID, deletedFields){

        for (var i = 0; i < deletedFields.length; i++) {
            try {
                var string = await Club.update({_id: clubID},{$pull: {fields:{_id : deletedFields[i]}}},{ 'new': true }, function (err) {
                    if (err){
                        console.log(err);
                    }else {
                        console.log(err);
                    }

                });

            }catch(error) {
                console.error(error);
            }
        }


}

function addField(clubID, newFields){
    Club.update( {_id: clubID},
        {$addToSet: {fields: {$each: newFields}}},
        {upsert: true}, function (err){

        if(err) {
            console.log(err);
        }else{
            console.log('se guardaron')
        }

        });
}<commit_msg>Correccion bkend para la edicion de canchas que tumbaba el server<commit_after>'use strict';
var mongoose = require('mongoose');
var Club = require('../models/Club');
var Q = require('q');

/**
 * Update Club Fields
 * @param req
 * @param res
 */
module.exports.updateFields = async function (req, res) {
    const clubId = req.params._id;
    const {modifiedFields, newFields, deletedFields} = req.body;
    let updatedField;
    try {
        if (modifiedFields.length) {
            updatedField = await updateFields(clubId, modifiedFields)
        }
        if (newFields.length) {
            updatedField = await addFields(clubId, newFields);
        }
        if (deletedFields.length) {
            updatedField = await deleteFields(clubId, deletedFields);
        }
    } catch (e) {
        res.status(500).send('Ocurrió un error al intentar actualizar la información de las canchas');
    }
    res.status(200).send(updatedField);

};

const updateFields = async (clubId, modifiedFields) => {
    return await Club.findByIdAndUpdate(clubId,
        {fields: modifiedFields},
        {new: true}
    );
}

const addFields = async (clubId, newFields) => {
    return await Club.findByIdAndUpdate(clubId,
        {$addToSet: {fields: {$each: newFields}}},
        {new: true}
    );
}

const deleteFields = async(clubId, deletedFields) => {
    let updateField;
    for (const fieldToDelete of deletedFields) {
        updateField = await Club.findByIdAndUpdate(
            clubId,
            {$pull: {fields: {_id: fieldToDelete}}},
            {new: true}
        );
    }
    return updateField;
}
{'fraction_non_alphanumeric': 0.13439233484619265, 'fraction_numerical': 0.004790721129601614, 'mean_word_length': 2.0823620823620823, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, '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': '3731385', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1159, 'n_words': 269}