<commit_before># galegis-api -- Structured Importing and RESTful providing of GA General Assembly Activity.
# Copyright (C) 2013 <NAME> - Distributed Under the GNU AGPL 3.0. See LICENSE at project root.
helpers = require '../util/helpers'
ifSuccessful = helpers.ifSuccessful

soap = require 'soap'
ObjectId = require('mongodb').ObjectID
committeesSvcUri = "./wsdl/Committees.svc.xml"

persistCommitteeMembers = (members, committee, session, db, callback) ->
  if ! members.forEach?
    members = [members]

  members.forEach? (member) ->
    db.collection("members").findOne {assemblyId: Number(member.Member.Id)}, (err, ourMember) -> ifSuccessful err, callback, ->
      unless ourMember?
        console.error("Member " + member.Member.Id + " is missing.")
        return

      memberKey = "members." + ourMember._id
      memberSet = {}
      memberSet[memberKey] = member.Role

      db.collection("committees").update
        sessionId: committee.sessionId,
        assemblyId: committee.assemblyId
      ,
        "$set": memberSet
      , (err) ->
        if err
          console.error "Something went wrong updating committees: " + err

persistCommittee = (assemblyCommittee, session, db, callback) ->
  members = assemblyCommittee.Members.CommitteeMember

  committeeDetails =
    sessionId: session._id,
    type: assemblyCommittee.Type.toLowerCase(),
    assemblyCode: assemblyCommittee.Code,
    assemblyId: Number(assemblyCommittee.Id),
    name: assemblyCommittee.Name,
    description: assemblyCommittee.Description,
    members: {}

  db.collection("committees").update
    sessionId: committeeDetails.sessionId,
    assemblyId: committeeDetails.assemblyId
  ,
    committeeDetails
  ,
    upsert: true,
    safe: true
  , (err, doc) ->
    ifSuccessful err, callback, ->
      persistCommitteeMembers(members, committeeDetails, session, db, callback)
      callback()

module.exports = (jobs, db) -> soap.createClient committeesSvcUri, (err, client) ->
  throw err if err

  jobs.process 'import committee for session', 20, (job, callback) ->
    # Ensure object IDs are in the correct format.
    job.data.session._id = new ObjectId(job.data.session._id)

    getCommitteeForSessionArgs =
      CommitteeId: job.data.committeeBrief.Id
      SessionId: job.data.session.assemblyId

    client.CommitteeService.BasicHttpBinding_CommitteeFinder.GetCommitteeForSession getCommitteeForSessionArgs, (err, result, raw) ->
      if err
        callback(err)
        return

      persistCommittee(result.GetCommitteeForSessionResult, job.data.session, db, callback)

  jobs.process 'import committees for session', 4, (job, callback) ->
    getCommitteesArgs =
      SessionId: job.data.session.assemblyId

    client.CommitteeService.BasicHttpBinding_CommitteeFinder.GetCommitteesBySession getCommitteesArgs, (err, result, raw) ->
      if err
        callback(err)
        return

      result.GetCommitteesBySessionResult.CommitteeListing.forEach (committeeBrief) ->
        jobs.create("import committee for session", committeeBrief: committeeBrief, session: job.data.session).save()

      callback()

  jobs.process 'import committees', (job, callback) -><commit_msg>Fix committee importer.<commit_after># galegis-api -- Structured Importing and RESTful providing of GA General Assembly Activity.
# Copyright (C) 2013 <NAME> - Distributed Under the GNU AGPL 3.0. See LICENSE at project root.
helpers = require '../util/helpers'
ifSuccessful = helpers.ifSuccessful

soap = require 'soap'
ObjectId = require('mongodb').ObjectID
committeesSvcUri = "./wsdl/Committees.svc.xml"

persistCommitteeMembers = (members, committee, session, db, callback) ->
  if ! members.forEach?
    members = [members]

  members.forEach? (member) ->
    db.collection("members").findOne {assemblyId: Number(member.Member.Id)}, (err, ourMember) -> ifSuccessful err, callback, ->
      unless ourMember?
        console.error("Member " + member.Member.Id + " is missing.")
        return

      memberKey = "members." + ourMember._id
      memberSet = {}
      memberSet[memberKey] = member.Role

      db.collection("committees").update
        sessionId: committee.sessionId,
        assemblyId: committee.assemblyId
      ,
        "$set": memberSet
      , (err) ->
        if err
          console.error "Something went wrong updating committees: " + err

persistCommittee = (assemblyCommittee, session, db, callback) ->
  members = assemblyCommittee.Members.CommitteeMember

  committeeDetails =
    sessionId: session._id,
    type: assemblyCommittee.Type.toLowerCase(),
    assemblyCode: assemblyCommittee.Code,
    assemblyId: Number(assemblyCommittee.Id),
    name: assemblyCommittee.Name,
    description: assemblyCommittee.Description,
    members: {}

  db.collection("committees").update
    sessionId: committeeDetails.sessionId,
    assemblyId: committeeDetails.assemblyId
  ,
    committeeDetails
  ,
    upsert: true,
    safe: true
  , (err, doc) ->
    ifSuccessful err, callback, ->
      persistCommitteeMembers(members, committeeDetails, session, db, callback)
      callback()

module.exports = (jobs, db) -> soap.createClient committeesSvcUri, (err, client) ->
  throw err if err

  jobs.process 'import committee for session', 20, (job, callback) ->
    # Ensure object IDs are in the correct format.
    job.data.session._id = new ObjectId(job.data.session._id)

    getCommitteeForSessionArgs =
      CommitteeId: job.data.committeeBrief.Id
      SessionId: job.data.session.assemblyId

    client.CommitteeService.BasicHttpBinding_CommitteeFinder.GetCommitteeForSession getCommitteeForSessionArgs, (err, result, raw) ->
      if err
        callback(err)
        return

      persistCommittee(result.GetCommitteeForSessionResult, job.data.session, db, callback)

  jobs.process 'import committees for session', 4, (job, callback) ->
    getCommitteesArgs =
      SessionId: job.data.session.assemblyId

    client.CommitteeService.BasicHttpBinding_CommitteeFinder.GetCommitteesBySession getCommitteesArgs, (err, result, raw) ->
      if err
        callback(err)
        return

      result.GetCommitteesBySessionResult.CommitteeListing?.forEach (committeeBrief) ->
        jobs.create("import committee for session", committeeBrief: committeeBrief, session: job.data.session).save()

      callback()

  jobs.process 'import committees', (job, callback) ->
{'fraction_non_alphanumeric': 0.10499921642375803, 'fraction_numerical': 0.0028208744710860366, 'mean_word_length': 3.5781922525107603, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5603418', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1783, 'n_words': 432}