<commit_before>_ = require 'underscore'
mongoose = require 'mongoose'

InviteSchema = require './invite'
Invite = mongoose.model 'Invite'
Person = mongoose.model 'Person'

TeamSchema = module.exports = new mongoose.Schema
  name:
    type: String
    required: true
  emails:
    type: [ mongoose.SchemaTypes.Email ]
    validate: [
      (v) -> 1 <= v.length
      'min'
      (v) -> v.length <= 4
      'max'
    ]
  invites: [ InviteSchema ]
  people_ids:
    type: [ mongoose.Schema.ObjectId ]
    index: true
TeamSchema.plugin require('mongoose-types').useTimestamps

TeamSchema.method 'includes', (person) ->
  _.any @people_ids, (id) -> id.equals(person.id)

TeamSchema.method 'people', (callback) ->
  Person.find _id: { '$in': @people_ids }, callback

TeamSchema.method 'invited', (invite) ->
  _.detect @invites, (i) -> i.code == invite

# create invites
TeamSchema.pre 'save', (next) ->
  for email in @emails
    unless _.detect(@invites, (i) -> i.email == email)
      @invites.push new Invite(email: email)
  _.invoke @invites, 'send'
  next()
TeamSchema.post 'save', () ->
  for invite in @invites
    invite.remove() unless !invite or _.include(@emails, invite.email)
  @save() if @isModified 'invites'

mongoose.model 'Team', TeamSchema<commit_msg>team never includes a null person<commit_after>_ = require 'underscore'
mongoose = require 'mongoose'

InviteSchema = require './invite'
Invite = mongoose.model 'Invite'
Person = mongoose.model 'Person'

TeamSchema = module.exports = new mongoose.Schema
  name:
    type: String
    required: true
  emails:
    type: [ mongoose.SchemaTypes.Email ]
    validate: [
      (v) -> 1 <= v.length
      'min'
      (v) -> v.length <= 4
      'max'
    ]
  invites: [ InviteSchema ]
  people_ids:
    type: [ mongoose.Schema.ObjectId ]
    index: true
TeamSchema.plugin require('mongoose-types').useTimestamps

TeamSchema.method 'includes', (person) ->
  _.any @people_ids, (id) -> id.equals(person.id) if person

TeamSchema.method 'people', (callback) ->
  Person.find _id: { '$in': @people_ids }, callback

TeamSchema.method 'invited', (invite) ->
  _.detect @invites, (i) -> i.code == invite

# create invites
TeamSchema.pre 'save', (next) ->
  for email in @emails
    unless _.detect(@invites, (i) -> i.email == email)
      @invites.push new Invite(email: email)
  _.invoke @invites, 'send'
  next()
TeamSchema.post 'save', () ->
  for invite in @invites
    invite.remove() unless !invite or _.include(@emails, invite.email)
  @save() if @isModified 'invites'

mongoose.model 'Team', TeamSchema
{'fraction_non_alphanumeric': 0.14921875, 'fraction_numerical': 0.0015625, 'mean_word_length': 3.477272727272727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '3348548', 'n_tokens_mistral': 868, 'n_tokens_neox': 881, 'n_words': 224}