<commit_before>// Document model
module.exports = {
  tableName: 'docs',

  attributes: {
    title: {
      type: 'string',
      required: true
    },
    slug: {
      type: 'string',
      required: true,
      alphanumericdashed: true
    },
    shortname: {
      type: 'string'
    },
    init_section: {
      type: 'integer'
    },
    created_at: {
      type: 'datetime',
      defaultsTo: function() {return new Date();}
    },
    updated_at: {
      type: 'datetime',
      defaultsTo: function() {return new Date();}
    },
    deleted_at: {
      type: 'datetime'
    },
    private: {
      type: 'boolean'
    },
    is_template: {
      type: 'boolean'
    }
  },

  // We override the default created/updated fields so we
  // can use our own names.
  autoCreatedAt: false,
  autoUpdatedAt: false,

  //Resonsible for actually updating the 'updated_at' property.
  beforeUpdate:function(values,next) {
      values.updated_at= new Date();
      next();
  },

  findByUser: function(userId, page, limit, filters) {

    var query = 'SELECT docs.* FROM ' + this.tableName +
      ' LEFT JOIN doc_user ON docs.id = doc_user.doc_id' +
      ' LEFT JOIN doc_group ON docs.id = doc_group.doc_id' +
      ' LEFT JOIN group_members ON group_members.group_id = doc_group.group_id' +
      ' WHERE (doc_user.user_id = ' + userId +
      ' OR group_members.user_id = ' + userId + ')';

    var filterQuery = module.exports.parseFilters(filters);
    if(filterQuery.length > 0) {
      query += ' AND ' + filterQuery;
    }

    query += ' ORDER BY docs.updated_at DESC';

    if(limit && page) {
      var offset = limit * (page - 1);
      query += ' LIMIT ' + offset + ', ' + limit;
    }

    var documentQuery = Promise.promisify(this.query);
    return documentQuery(query);
  },

  countByUser: function(userId, filters)
  {
    var query = 'SELECT COUNT(*) AS count FROM ' + this.tableName +
      ' LEFT JOIN doc_user ON docs.id = doc_user.doc_id' +
      ' LEFT JOIN doc_group ON docs.id = doc_group.doc_id' +
      ' LEFT JOIN group_members ON group_members.group_id = doc_group.group_id' +
      ' WHERE (doc_user.user_id = ' + userId +<commit_msg>Select distinct records to avoid duplicates on group join.<commit_after>// Document model
module.exports = {
  tableName: 'docs',

  attributes: {
    title: {
      type: 'string',
      required: true
    },
    slug: {
      type: 'string',
      required: true,
      alphanumericdashed: true
    },
    shortname: {
      type: 'string'
    },
    init_section: {
      type: 'integer'
    },
    created_at: {
      type: 'datetime',
      defaultsTo: function() {return new Date();}
    },
    updated_at: {
      type: 'datetime',
      defaultsTo: function() {return new Date();}
    },
    deleted_at: {
      type: 'datetime'
    },
    private: {
      type: 'boolean'
    },
    is_template: {
      type: 'boolean'
    }
  },

  // We override the default created/updated fields so we
  // can use our own names.
  autoCreatedAt: false,
  autoUpdatedAt: false,

  //Resonsible for actually updating the 'updated_at' property.
  beforeUpdate:function(values,next) {
      values.updated_at= new Date();
      next();
  },

  findByUser: function(userId, page, limit, filters) {

    var query = 'SELECT DISTINCT docs.* FROM ' + this.tableName +
      ' LEFT JOIN doc_user ON docs.id = doc_user.doc_id' +
      ' LEFT JOIN doc_group ON docs.id = doc_group.doc_id' +
      ' LEFT JOIN group_members ON group_members.group_id = doc_group.group_id' +
      ' WHERE (doc_user.user_id = ' + userId +
      ' OR group_members.user_id = ' + userId + ')';

    var filterQuery = module.exports.parseFilters(filters);
    if(filterQuery.length > 0) {
      query += ' AND ' + filterQuery;
    }

    query += ' ORDER BY docs.updated_at DESC';

    if(limit && page) {
      var offset = limit * (page - 1);
      query += ' LIMIT ' + offset + ', ' + limit;
    }

    var documentQuery = Promise.promisify(this.query);
    return documentQuery(query);
  },

  countByUser: function(userId, filters)
  {
    var query = 'SELECT COUNT(DISTINCT docs.id) AS count FROM ' + this.tableName +
      ' LEFT JOIN doc_user ON docs.id = doc_user.doc_id' +
      ' LEFT JOIN doc_group ON docs.id = doc_group.doc_id' +
      ' LEFT JOIN group_members ON group_members.group_id = doc_group.group_id' +
      ' WHERE (doc_user.user_id = ' + userId +
{'fraction_non_alphanumeric': 0.12690124858115778, 'fraction_numerical': 0.0009080590238365494, 'mean_word_length': 2.48300395256917, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7621625', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1421, 'n_words': 360}