node.js/imports.js
author carine
Mon, 01 Dec 2014 15:15:25 +0100
changeset 743 0e58fabc0175
parent 732 08a3bcc99c18
permissions -rw-r--r--
test
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     1
// should get it from elsewhere
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     2
var mongoose = require('mongoose'),
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     3
db = mongoose.connect('mongodb://localhost/tpac');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     4
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     5
var People = require('./model.js').People(db);
457
be8629070917 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 456
diff changeset
     6
var Event = require('./model.js').Event(db);
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     7
var Organization = require('./model.js').Organization(db);
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     8
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     9
function loadPeopleData(id) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    10
   var http = require('http');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    11
   var request = http.get({host: 'www.w3.org', path:'/2011/08/w3c-data/people/' + id}, function (response) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    12
     response.setEncoding('utf8');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    13
     var peopleJSON = "", peopleData;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    14
     response.on('data', function (chunk) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    15
       peopleJSON = peopleJSON + chunk;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    16
     });
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    17
     response.on('end', function () {
266
Dominqique Hazael-Massieux <dom@w3.org>
parents: 265
diff changeset
    18
	 
265
3b084b09acb4 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 257
diff changeset
    19
		     try {
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    20
       peopleData = JSON.parse(peopleJSON);
257
91c523e5b1f3 replaced w3cId with slug
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
    21
       People.findOne({slug: id}, function(err, people) {
267
6ee724f72b4d *** empty log message ***
Dominqique Hazael-Massieux <dom@w3.org>
parents: 266
diff changeset
    22
           if (people && peopleData.picture && people.picture != peopleData.picture) {
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    23
	       people.picture = peopleData.picture;
268
b79ae76ac93a *** empty log message ***
Dominqique Hazael-Massieux <dom@w3.org>
parents: 267
diff changeset
    24
	       people.save(function(err) { console.log(err);});
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    25
	   }
267
6ee724f72b4d *** empty log message ***
Dominqique Hazael-Massieux <dom@w3.org>
parents: 266
diff changeset
    26
           if (people && peopleData.thumbnail && people.picture_thumb != peopleData.thumbnail) {
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    27
	       people.picture_thumb = peopleData.thumbnail;
268
b79ae76ac93a *** empty log message ***
Dominqique Hazael-Massieux <dom@w3.org>
parents: 267
diff changeset
    28
	       people.save(function(err) { console.log(err);});
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    29
	   }
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    30
265
3b084b09acb4 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 257
diff changeset
    31
       });			 
3b084b09acb4 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 257
diff changeset
    32
		     } catch (x) {
269
Dominqique Hazael-Massieux <dom@w3.org>
parents: 268
diff changeset
    33
			 console.log("Retrieved http://www.w3.org/2011/08/w3c-data/people/" + id);
266
Dominqique Hazael-Massieux <dom@w3.org>
parents: 265
diff changeset
    34
			 console.log(peopleJSON);
265
3b084b09acb4 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 257
diff changeset
    35
		     }
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    36
     });
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    37
  });
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    38
}
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    39
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    40
exports.importUserList = function(auth, callback)  {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    41
   var https = require('https');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    42
    var success = [];
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    43
    var info = [];
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    44
    var errors = [];
597
945ebe26d76e tpac2012
Dominique Hazaël-Massieux <dom@w3.org>
parents: 477
diff changeset
    45
   var request = https.get({host: 'www.w3.org', path:'/2002/09/wbs/tpRegistrants-json.php?wgid=35125&qaireno=TPAC2012', headers: {Authorization: 'Basic ' + auth}}, function (response) {
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    46
     response.setEncoding('utf8');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    47
     var registrantsJSON = "", registrantsData;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    48
     response.on('data', function (chunk) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    49
       registrantsJSON = registrantsJSON + chunk;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    50
     });
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    51
     response.on('end', function () {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    52
        registrantsData = JSON.parse(registrantsJSON);
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    53
        var additions = {orgs:[],people:[]}, errors;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    54
        var counter = 0, counterAdded = 0;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    55
	
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    56
        function addOrg(org, people) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    57
	    return function (err) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    58
		// assume duplicate key errors
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    59
	        if (err) {
257
91c523e5b1f3 replaced w3cId with slug
Dominqique Hazael-Massieux <dom@w3.org>
parents: 172
diff changeset
    60
		    Organization.findOne({slug: people.affiliationId}, ["_id", "name"], function(err, org) {
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    61
		    if (org) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    62
			people.affiliation = org._id;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    63
		    }
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    64
     	            people.save(addPeople(people));		    
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    65
		    });
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    66
		} else {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    67
		    success.push(org.name + ' added');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    68
		    people.affiliation = org._id;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    69
     	            people.save(addPeople(people));		      
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    70
		}
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    71
	    };
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    72
	}
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    73
        function addPeople(people) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    74
  	  return function (err) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    75
               counter++;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    76
		// We ignore duplicate key errors
466
d0a948a04c64 more dbg
Dominique Hazaël-Massieux <dom@w3.org>
parents: 465
diff changeset
    77
	       if (err){
d0a948a04c64 more dbg
Dominique Hazaël-Massieux <dom@w3.org>
parents: 465
diff changeset
    78
		   console.log(err);
d0a948a04c64 more dbg
Dominique Hazaël-Massieux <dom@w3.org>
parents: 465
diff changeset
    79
	       }
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    80
	       if (!err) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    81
                 counterAdded++;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    82
                 success.push(people.given + ' ' + people.family + ' added');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    83
		 // update list of employees
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    84
  	         Organization.findById(people.affiliation
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    85
                     // closure to add employee to org record
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    86
				    , (function (p) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    87
					return function (err, org) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    88
					if (org) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    89
					    org.employees.push(people._id);
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    90
					    org.save();
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    91
					}};})(people) );
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    92
	       }
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    93
       	       if (counter == registrantsData.registrants.length) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    94
		if (!counterAdded) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    95
		    info.push('No new data to import');
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    96
		}
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    97
		callback(success, info, errors);
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    98
	       }
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    99
	     };
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   100
        }
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   101
        for (p in registrantsData.registrants) {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   102
            var peopleData = registrantsData.registrants[p];
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   103
	    var people = new People();
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   104
	    people.given = peopleData.given;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   105
	    people.family = peopleData.family;
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   106
	    people.email = peopleData.email;
459
7c7387965bae *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 458
diff changeset
   107
	    if (peopleData.w3cId > 0) {
732
08a3bcc99c18 use W3C login for slug rather than id
Dominique Hazaël-Massieux <dom@w3.org>
parents: 597
diff changeset
   108
		people.slug = peopleData.login;
467
d943cb036565 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 466
diff changeset
   109
		people.login = peopleData.login;
458
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   110
		loadPeopleData(people.slug);
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   111
		if (peopleData.organization && peopleData.organization.w3cId) {
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   112
		    people.affiliationId = peopleData.organization.w3cId;
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   113
  		    org = new Organization();
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   114
		    org.slug = peopleData.organization.w3cId;
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   115
		    org.name = peopleData.organization.name;
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   116
		    org.save(addOrg(org, people));		       
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   117
		} else {
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   118
  		    people.save(addPeople(people));		
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   119
		}
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   120
	    } else {
465
Dominique Hazaël-Massieux <dom@w3.org>
parents: 460
diff changeset
   121
		console.log("non W3C account: " + people.given + " " + people.family);
467
d943cb036565 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 466
diff changeset
   122
		people.slug = people.w3cId;
d943cb036565 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 466
diff changeset
   123
		people.login = peopleData.email;
458
04c30858ee1c fix bug in importing users without accounts
Dominique Hazaël-Massieux <dom@w3.org>
parents: 457
diff changeset
   124
		people.save(addPeople(people));
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   125
	    }
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   126
	}
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   127
     });
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   128
   });
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   129
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   130
};
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   131
452
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   132
exports.importRegistrationData = function(auth, callback)  {
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   133
   var https = require('https');
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   134
    var success = [];
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   135
    var info = [];
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   136
    var errors = [];
597
945ebe26d76e tpac2012
Dominique Hazaël-Massieux <dom@w3.org>
parents: 477
diff changeset
   137
   var request = https.get({host: 'www.w3.org', path:'/2002/09/wbs/tpRegistrants-schedule.php?wgid=35125&qaireno=TPAC2012', headers: {Authorization: 'Basic ' + auth}}, function (response) {
452
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   138
     response.setEncoding('utf8');
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   139
     var registrantsJSON = "", registrantsData;
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   140
     response.on('data', function (chunk) {
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   141
       registrantsJSON = registrantsJSON + chunk;
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   142
     });
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   143
     response.on('end', function () {
454
dc0b2129d1ec don't fail on json parse
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   144
        try {
dc0b2129d1ec don't fail on json parse
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   145
            registrantsData = JSON.parse(registrantsJSON);	    
dc0b2129d1ec don't fail on json parse
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   146
	} catch (x) {
dc0b2129d1ec don't fail on json parse
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   147
	    errors.push(x);
455
Dominique Hazaël-Massieux <dom@w3.org>
parents: 454
diff changeset
   148
	    console.log(registrantsJSON);
454
dc0b2129d1ec don't fail on json parse
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   149
	    callback(success, info, errors);
456
d73dca4c0059 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 455
diff changeset
   150
	    return;
454
dc0b2129d1ec don't fail on json parse
Dominique Hazaël-Massieux <dom@w3.org>
parents: 452
diff changeset
   151
	}
471
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   152
        var eventRegistration = {};
452
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   153
        for (p in registrantsData.registrants) {
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   154
            var peopleData = registrantsData.registrants[p];
471
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   155
	    for (var e in peopleData.registered) {
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   156
		if (!eventRegistration[peopleData.registered[e]]) {
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   157
		    eventRegistration[peopleData.registered[e]] = [];
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   158
		}
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   159
		eventRegistration[peopleData.registered[e]].push(peopleData.w3cId);
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   160
	    }
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   161
	}
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   162
        var eventCounter = 0;
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   163
	for (var eventSlug in eventRegistration) {
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   164
	    Event.findOne(
fd1ac20588a5 reorg import schedule data
Dominique Hazaël-Massieux <dom@w3.org>
parents: 470
diff changeset
   165
		{slug: eventSlug}, function(err, event) {
473
76c4e9aef8a5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 472
diff changeset
   166
		    if (event) {
474
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   167
			var peopleCounter = 0;
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   168
			for (var p in eventRegistration[eventSlug]) {
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   169
			    var peopleSlug = eventRegistration[eventSlug][p];
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   170
			    People.findOne(
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   171
				{slug: peopleSlug}, ["_id"],
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   172
				function(err, people) {
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   173
				    peopleCounter ++;
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   174
				    if (people) {
476
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   175
					var alreadyInterested = new RegExp("^" + event.interested.join("|") + "$");
474
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   176
					if (!alreadyInterested.test(people._id)) {
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   177
					    event.interested.push(people._id);
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   178
					}
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   179
				    }
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   180
				    if (peopleCounter == eventRegistration[eventSlug].length) {
476
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   181
					event.save(function(err) {
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   182
					    eventCounter++;
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   183
					    errors.push(err);
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   184
					    if ( eventCounter == eventRegistration.length) {
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   185
						if (!errors.length) {
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   186
						    success.push("Registration data successfully imported");
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   187
						}
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   188
						callback(success, info, errors);
135504fb7291 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 475
diff changeset
   189
					    }
477
7afebb437118 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 476
diff changeset
   190
					});
7afebb437118 *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 476
diff changeset
   191
						  
474
e33716de612a *** empty log message ***
Dominique Hazael-Massieux <dom@w3.org>
parents: 473
diff changeset
   192
				    }
452
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   193
			    });
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   194
		    }
473
76c4e9aef8a5 *** empty log message ***
Dominique Hazaël-Massieux <dom@w3.org>
parents: 472
diff changeset
   195
		    }
452
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   196
		});
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   197
	}
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   198
     });
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   199
   });
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   200
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   201
};
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   202
bde84d9dc214 support for importing registration data from wbs
Dominique Hazaël-Massieux <dom@w3.org>
parents: 295
diff changeset
   203
172
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   204
exports.importRooms = function() {
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   205
84b85139fa2c moved import people into separate module
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
   206
};