<commit_before>incidentReportSchema = require '/imports/schemas/incidentReport.coffee'
@Incidents = new Meteor.Collection "counts"
@grid ?= {}
@grid.Incidents = Incidents

getEventIncidentss = (userEventId) ->
  Incidents.find({userEventId: userEventId})

Incidents.getEventIncidentss = getEventIncidentss

if Meteor.isServer
  Meteor.publish "eventIncidents", (ueId) ->
    getEventIncidentss(ueId)
  Meteor.publish "mapIncidents", () ->
    Incidents.find({locations: {$ne: null}}, {fields: {userEventId: 1, date: 1, locations: 1}})

  Incidents.allow
    insert: (userID, doc) ->
      doc.creationDate = new Date()
      return Roles.userIsInRole(Meteor.userId(), ['admin'])
    remove: (userID, doc) ->
      return Roles.userIsInRole(Meteor.userId(), ['admin'])

Meteor.methods
  addIncidentReport: (incident) ->
    incidentReportSchema.validate(incident)
    user = Meteor.user()
    if not Roles.userIsInRole(user._id, ['admin'])
      throw new Meteor.Error("auth", "User does not have permission to create incident reports")
    incident.addedByUserId = user._id
    incident.addedByUserName = user.profile.name
    incident.addedDate = new Date()
    newId = Incidents.insert(incident)
    Meteor.call("updateUserEventLastModified", incident.userEventId)
    return newId

  addIncidentReports: (incidents) ->
    incidents.map (incident)->
      Meteor.call("addIncidentReport", incident)

  removeIncidentReport: (id) ->
    if Meteor.user()
      removed = Incidents.findOne(id)
      Incidents.remove(id)
      Meteor.call("updateUserEventLastModified", removed.userEventId)

# Split incidents with both case and death counts into separate incidents
if Meteor.isServer
  Meteor.startup ->
    incidents = Incidents.find({$and: [{cases: {$exists: true}}, {deaths: {$exists: true}}]}).fetch()
    for incident in incidents
      if incident.cases?.length and incident.deaths?.length
        newIncident = {
          url: incident.url
          userEventId: incident.userEventId
          species: incident.species
          travelRelated: incident.travel
          addedByUserId: incident.addedByUserId
          addedByUserName: incident.addedByUserName
          addedDate: incident.addedDate
          deaths: incident.deaths
        }

        if incident.locations?.length
          newIncident.locations = incident.locations
        if incident.date
          newIncident.date = incident.date

        Incidents.update(incident._id, {$unset: {deaths: ""}})
        Incidents.insert(newIncident)

    # Convert string case and death counts to integers
    incidents = Incidents.find({$or: [{cases: {$type: "string"}}, {deaths: {$type: "string"}}]}).fetch()
    for incident in incidents
      if incident.cases
        updateField = {cases: parseInt(incident.cases)}
      else
        updateField = {deaths: parseInt(incident.deaths)}
      Incidents.update({_id: incident._id}, {$set: updateField})<commit_msg>make sure parseInt() returns a number when updating incident counts<commit_after>incidentReportSchema = require '/imports/schemas/incidentReport.coffee'
@Incidents = new Meteor.Collection "counts"
@grid ?= {}
@grid.Incidents = Incidents

getEventIncidentss = (userEventId) ->
  Incidents.find({userEventId: userEventId})

Incidents.getEventIncidentss = getEventIncidentss

if Meteor.isServer
  Meteor.publish "eventIncidents", (ueId) ->
    getEventIncidentss(ueId)
  Meteor.publish "mapIncidents", () ->
    Incidents.find({locations: {$ne: null}}, {fields: {userEventId: 1, date: 1, locations: 1}})

  Incidents.allow
    insert: (userID, doc) ->
      doc.creationDate = new Date()
      return Roles.userIsInRole(Meteor.userId(), ['admin'])
    remove: (userID, doc) ->
      return Roles.userIsInRole(Meteor.userId(), ['admin'])

Meteor.methods
  addIncidentReport: (incident) ->
    incidentReportSchema.validate(incident)
    user = Meteor.user()
    if not Roles.userIsInRole(user._id, ['admin'])
      throw new Meteor.Error("auth", "User does not have permission to create incident reports")
    incident.addedByUserId = user._id
    incident.addedByUserName = user.profile.name
    incident.addedDate = new Date()
    newId = Incidents.insert(incident)
    Meteor.call("updateUserEventLastModified", incident.userEventId)
    return newId

  addIncidentReports: (incidents) ->
    incidents.map (incident)->
      Meteor.call("addIncidentReport", incident)

  removeIncidentReport: (id) ->
    if Meteor.user()
      removed = Incidents.findOne(id)
      Incidents.remove(id)
      Meteor.call("updateUserEventLastModified", removed.userEventId)

# Split incidents with both case and death counts into separate incidents
if Meteor.isServer
  Meteor.startup ->
    incidents = Incidents.find({$and: [{cases: {$exists: true}}, {deaths: {$exists: true}}]}).fetch()
    for incident in incidents
      if incident.cases?.length and incident.deaths?.length
        newIncident = {
          url: incident.url
          userEventId: incident.userEventId
          species: incident.species
          travelRelated: incident.travel
          addedByUserId: incident.addedByUserId
          addedByUserName: incident.addedByUserName
          addedDate: incident.addedDate
          deaths: incident.deaths
        }

        if incident.locations?.length
          newIncident.locations = incident.locations
        if incident.date
          newIncident.date = incident.date

        Incidents.update(incident._id, {$unset: {deaths: ""}})
        Incidents.insert(newIncident)

    # Convert string case and death counts to integers
    incidents = Incidents.find({$or: [{cases: {$type: "string"}}, {deaths: {$type: "string"}}]}).fetch()
    for incident in incidents
      if incident.cases
        parsed = parseInt(incident.cases)
        if parsed.toString() is "NaN"
          mongoProjection = {$set: {specify: incident.cases}, $unset: {cases: ""}}
        else
          mongoProjection = {$set: {cases: parsed}}
      else if incident.deaths
        parsed = parseInt(incident.deaths)
        if parsed.toString() is "NaN"
          mongoProjection = {$set: {specify: incident.deaths}, $unset: {deaths: ""}}
        else
          mongoProjection = {$set: {deaths: parsed}}
      if mongoProjection
        Incidents.update({_id: incident._id}, mongoProjection)
{'fraction_non_alphanumeric': 0.11941008563273073, 'fraction_numerical': 0.0009514747859181732, 'mean_word_length': 3.385952712100139, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2460973', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1870, 'n_words': 423}