/* File: patch_profil_prod.js
 *
 * Copyright (c) 2013-2016
 * Centre National d’Enseignement à Distance (Cned), Boulevard Nicephore Niepce, 86360 CHASSENEUIL-DU-POITOU, France
 * (direction-innovation@cned.fr)
 *
 * GNU Affero General Public License (AGPL) version 3.0 or later version
 *
 * This file is part of a program which is free software: you can
 * redistribute it and/or modify it under the terms of the
 * GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 *
 */

'use strict';

var mongoose = require('mongoose'),
    ProfilTag = mongoose.model('ProfilTag'),
    Profil = mongoose.model('Profil'),
    UserProfil = mongoose.model('UserProfil'),
    Tags = mongoose.model('Tag'),
    UserAccount = mongoose.model('User');


exports.updateDb = function () {

    updateProfiles(function () {
        updateUsers();
    });

    /*UserAccount.find().exec(function (err, profiles) {
     if(profiles){
     for (var i = 0; i < profiles.length; i++) {
     console.log('profiles[i].owner', profiles[i]);
     }
     }
     });*/

    /*Profil.find().exec(function (err, profiles) {
     if(profiles){
     for (var i = 0; i < profiles.length; i++) {
     console.log('profiles[i].owner', profiles[i].owner);
     }
     }
     });*/


};

function updateUsers() {
    UserProfil.find()
        .populate('userID')
        .populate('profilID')
        .exec(function (err, _userProfils) {


            if (_userProfils) {
                for (var i = 0; i < _userProfils.length; i++) {

                    if (_userProfils[i].profilID.owner !== 'scripted') {
                        if (_userProfils[i].userID && _userProfils[i].userID.local) {
                            if (_userProfils[i].userID.local.role === 'admin') {
                                _userProfils[i].profilID.owner = 'admin';
                            } else {
                                _userProfils[i].profilID.owner = _userProfils[i].userID.local.email;

                            }
                            try {
                                _userProfils[i].profilID.save(function(err){
                                    if(err){
                                        console.log('Error on user update', err);
                                    }
                                });
                            } catch (e) {
                                console.log('Error on user update', _userProfils);
                            }
                        }

                    }

                }
            }
        });
}

/**
 * Update profiles tags
 * @param cb
 */
function updateProfiles(cb) {

    ProfilTag.find()
        .populate('profil')
        .exec(function (err, _profilsTag) {

            if (_profilsTag) {
                for (var i = 0; i < _profilsTag.length; i++) {

                    if (_profilsTag[i].profil) {
                        if (_profilsTag[i].profil.profileTags.indexOf(_profilsTag[i]._id) < 0) {
                            _profilsTag[i].profil.profileTags.push(_profilsTag[i]._id);
                        }



                        try {
                            // Clean profile
                            _profilsTag[i].profil.photo = undefined;
                            _profilsTag[i].profil.save(function(err){
                                if(err){
                                    console.log('updateProfiles - Error on profil update', err);
                                }
                            });
                        } catch (e) {
                            console.log('updateProfiles - Error on profil update', _profilsTag[i].profil);
                        }
                    }



                    try {
                        // Clean profile Tag
                        _profilsTag[i].texte = undefined;
                        _profilsTag[i].save(function(err){
                            if(err){
                                console.log('updateProfiles - Error on profilTag update', err);
                            }
                        });
                    } catch (e) {
                        console.log('updateProfiles - Error on profilTag update', _profilsTag[i]);
                    }


                }
            }

            cb();


        });
}




@@ -1280,24 +1280,26 @@
 n () %7B%0A%0A    
+/*
 updateProfil
@@ -1320,19 +1320,16 @@
  %7B%0A     
-   
 updateUs
@@ -1339,19 +1339,22 @@
 ();%0A    
+ 
 %7D);
+*/
 %0A%0A    /*
@@ -1554,18 +1554,16 @@
 */%0A%0A    
-/*
 Profil.f
@@ -1597,34 +1597,38 @@
 rofiles) %7B%0A     
+   
 if
+ 
 (profiles)%7B%0A    
@@ -1613,39 +1613,47 @@
    if (profiles)
+ 
 %7B%0A     
+   
+    
 for (var i = 0; 
@@ -1672,32 +1672,43 @@
 .length; i++) %7B%0A
+           
      console.log
@@ -1745,24 +1745,31 @@
 %5Bi%5D.owner);%0A
+       
      %7D%0A     
@@ -1768,28 +1768,28 @@
  %7D%0A     
+   
 %7D%0A
- 
     %7D);
-*/
 %0A%0A%0A%7D;%0A%0Af
@@ -2615,37 +2615,39 @@
 ID.save(function
+ 
 (err)
+ 
 %7B%0A              
@@ -2662,37 +2662,39 @@
               if
+ 
 (err)
+ 
 %7B%0A              
@@ -3606,33 +3606,32 @@
             %7D%0A%0A%0A
-%0A
                 
@@ -3813,37 +3813,39 @@
 il.save(function
+ 
 (err)
+ 
 %7B%0A              
@@ -3856,37 +3856,39 @@
               if
+ 
 (err)
+ 
 %7B%0A              
@@ -4231,17 +4231,16 @@
     %7D%0A%0A%0A
-%0A
         
@@ -4412,21 +4412,23 @@
 function
+ 
 (err)
+ 
 %7B%0A      
@@ -4451,21 +4451,23 @@
       if
+ 
 (err)
+ 
 %7B%0A      

patch update