<commit_before>﻿var mongoose = require('mongoose')
  , cultures = require('./cultures')
  , utils = require('./utils')
  , Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/verity-translations', { user: 'verity', pass: '<PASSWORD>' });

var sessionSchema = new Schema({
  sessionId: String,
  userId: Schema.ObjectId,
  last: Date
});

var userSchema = new Schema({
  name: String,
  email: String,
  notes: String,
  password: String,
  admin: Boolean,
  permissions: [
    {
      setId: Schema.ObjectId,
      permissions: [String]
    }
  ],
  deleted: Boolean,
  deletedDate: Date
});

userSchema.methods.can = function (permission, setId) {
  var permissions = this.permissions;

  if (setId) {
    for (var i = 0; i < permissions.length; i++) {
      if (permissions[i].setId.equals(setId))
        return permission === 'any' || permissions[i].permissions.indexOf(permission) !== -1;
    }
  }

  return false;
};

var setSchema = new Schema({
  name: String,
  title: String,
  devices: [String],
  version: String,
  changes: String,
  langs: [String],
  deleted: Boolean,
  deletedDate: Date
});

setSchema.methods.export = function (callback) {
  var set = this;

  Term.find({ setId: set._id, deleted: { $ne: true } }).sort('order').exec(function (err1, terms) {
    Entry.find({ setId: set._id, deleted: { $ne: true } }, function (err2, entries) {
      if (err1 || err2)
        return callback(err1 || err2);

      var termsMap = {}, datesMap = {};
      var json = [['Lang'], ['LangId']];
      var langs = { key: 0 };

      set.langs.forEach(function (l, i) {
        var c = cultures.get(l);
        json[0][i + 1] = c.name;
        json[1][i + 1] = c.id;
        langs[l] = i + 1;
      });

      terms.forEach(function (t) {
        var term = new Array(json[0].length);
        json[json.length] = termsMap[t.id] = term;
        datesMap[t.id] = new Array(json[0].length);

        for (var i = 0; i < term.length; i++)
          term[i] = '';
      });

      entries.forEach(function (e) {
        var term = termsMap[e.termId.toString()];
        var date = datesMap[e.termId.toString()];

        if (term) {
          var index = langs[e.lang];

          if (!date[index] || date[index] < e.date) {
            term[index] = e.value.trim();
            date[index] = e.date;
          }
        }
      });

      callback(null, json);
    });
  });
};

var termSchema = new Schema({
  setId: Schema.ObjectId,
  order: Number,
  date: Date,
  userId: Schema.ObjectId,
  deleted: Boolean,
  deletedDate: Date
});

var entrySchema = new Schema({
  setId: Schema.ObjectId,
  termId: Schema.ObjectId,
  userId: Schema.ObjectId,
  lang: String,
  date: Date,
  value: String,
  deleted: Boolean,
  deletedDate: Date
});

var snapshotSchema = new Schema({
  userId: Schema.ObjectId,
  setId: Schema.ObjectId,
  version: String,<commit_msg>removing empty keys from exported translations<commit_after>﻿var mongoose = require('mongoose')
  , cultures = require('./cultures')
  , utils = require('./utils')
  , Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/verity-translations', { user: 'verity', pass: '<PASSWORD>' });

var sessionSchema = new Schema({
  sessionId: String,
  userId: Schema.ObjectId,
  last: Date
});

var userSchema = new Schema({
  name: String,
  email: String,
  notes: String,
  password: String,
  admin: Boolean,
  permissions: [
    {
      setId: Schema.ObjectId,
      permissions: [String]
    }
  ],
  deleted: Boolean,
  deletedDate: Date
});

userSchema.methods.can = function (permission, setId) {
  var permissions = this.permissions;

  if (setId) {
    for (var i = 0; i < permissions.length; i++) {
      if (permissions[i].setId.equals(setId))
        return permission === 'any' || permissions[i].permissions.indexOf(permission) !== -1;
    }
  }

  return false;
};

var setSchema = new Schema({
  name: String,
  title: String,
  devices: [String],
  version: String,
  changes: String,
  langs: [String],
  deleted: Boolean,
  deletedDate: Date
});

setSchema.methods.export = function (callback) {
  var set = this;

  Term.find({ setId: set._id, deleted: { $ne: true } }).sort('order').exec(function (err1, terms) {
    Entry.find({ setId: set._id, deleted: { $ne: true } }, function (err2, entries) {
      if (err1 || err2)
        return callback(err1 || err2);

      var termsMap = {}, datesMap = {};
      var json = [['Lang'], ['LangId']];
      var langs = { key: 0 };

      set.langs.forEach(function (l, i) {
        var c = cultures.get(l);
        json[0][i + 1] = c.name;
        json[1][i + 1] = c.id;
        langs[l] = i + 1;
      });

      terms.forEach(function (t) {
        var term = new Array(json[0].length);
        json[json.length] = termsMap[t.id] = term;
        datesMap[t.id] = new Array(json[0].length);

        for (var i = 0; i < term.length; i++)
          term[i] = '';
      });

      entries.forEach(function (e) {
        var term = termsMap[e.termId.toString()];
        var date = datesMap[e.termId.toString()];

        if (term) {
          var index = langs[e.lang];

          if (!date[index] || date[index] < e.date) {
            term[index] = e.value.trim();
            date[index] = e.date;
          }
        }
      });

      for (var i = json.length - 1; i >= 0; i--) {
        if (!json[i][0])
          json.splice(i, 1);
      }

      callback(null, json);
    });
  });
};

var termSchema = new Schema({
  setId: Schema.ObjectId,
  order: Number,
  date: Date,
  userId: Schema.ObjectId,
  deleted: Boolean,
  deletedDate: Date
});

var entrySchema = new Schema({
  setId: Schema.ObjectId,
  termId: Schema.ObjectId,
  userId: Schema.ObjectId,
  lang: String,
  date: Date,
  value: String,
  deleted: Boolean,
  deletedDate: Date
});

var snapshotSchema = new Schema({
  userId: Schema.ObjectId,
  setId: Schema.ObjectId,
  version: String,
{'fraction_non_alphanumeric': 0.16274343776460626, 'fraction_numerical': 0.006435224386113463, 'mean_word_length': 2.6299938537185, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 6, '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': '1738331', 'n_tokens_mistral': 2013, 'n_tokens_neox': 2007, 'n_words': 490}