/* global db, ObjectId */

db.users.find({}).forEach(function (user) {
  var profile = Object.assign({}, user.profile, {
    language: lang(user.profile.language)
  });
  var props = { profile: profile };

  if (user.cooperativeId) {
    props.cooperative = user.cooperativeId;
  }

  delete profile.toRehearse;

  db.users.update({ _id: user._id }, {
    $set: props,
    $unset: {
      language: '',
      actions: '',
      achievements: '',
      numFeedback: '',
      recentAchievements: '',
      cooperativeId: ''
    }
  });
});

db.actions.drop();
db.cooperatives.find({}).forEach(function (cooperative) {
  var actions = [];

  cooperative.actions.forEach(function (action) {
    if (action instanceof ObjectId) {
      actions.push(action);
      return;
    }

    var doc = db.actions.insertOne({
      name: action.name,
      date: action.date,
      cost: action.cost,
      description: action.description,
      types: action.types,
      cooperative: cooperative._id,
      user: cooperative.editors[0].editorId,
      comments: action.comments.map(function (comment) {
        return {
          user: comment.user,
          author: db.users.find({ _id: ObjectId(comment.user) }).profile.name,
          comment: comment.comment,
          date: comment.date
        };
      })
    });

    actions.push(doc.insertedId);
  });

  db.cooperatives.update({ _id: cooperative._id }, {
    name: cooperative.name,
    email: cooperative.email,
    lat: cooperative.lat,
    lng: cooperative.lng,
    yearOfConst: cooperative.yearOfConst,
    area: cooperative.area,
    numOfApartments: cooperative.numOfApartments,
    ventilationType: cooperative.ventilationType,
    meters: cooperative.meters,
    performances: [],
    actions: actions,
    editors: cooperative.editors.map(function (props) { return props.editorId; })
  });
});

function lang(language) {
  switch(language) {
    case 'en':
    case 'English':
      return 'en';
    case 'sv':
    case 'Swedish':
    default:
      return 'sv';
  }
}

@@ -84,15 +84,8 @@
 e = 
-Object.
 assi
@@ -2014,8 +2014,345 @@
 ;%0A  %7D%0A%7D%0A
+%0Afunction assign() %7B%0A  var args = Array.prototype.slice.call(arguments);%0A  var target = args%5B0%5D;%0A%0A  for (var i = 1; i %3C args.length; i += 1) %7B%0A    if (args%5Bi%5D && typeof args%5Bi%5D === 'object') %7B%0A      for (var key in args%5Bi%5D) %7B%0A        if (args%5Bi%5D.hasOwnProperty(key)) %7B%0A          target%5Bkey%5D = args%5Bi%5D%5Bkey%5D;%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A%7D%0A

Fix mongo 2.4 not supporting Object.assign