Created
March 26, 2014 17:04
-
-
Save dmackerman/9788168 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: ADD ERROR CONDITIONS | |
function onRequest(request, response, modules) { | |
// setup modules | |
var logger = modules.logger; | |
var db = modules.collectionAccess; | |
// set up the start/end date for the kickstart | |
var startDate = Date.parse(request.body.startDate); | |
var endDate = Date.parse(request.body.endDate); | |
// fetch all of our kickstart-tpls, this could eventually be fetched via ID from the UI | |
db.collection('kickstarts-tpl').find({}, function(err, kickstartTemplates) { | |
// when the kickstart template returns, we look up the default topics from | |
// the topics template (for now, this is just one because we have one type of kickstart; | |
// docs[0] would be replaced with a paramater that they would choose in the UI, | |
// but for now we have one template. | |
var kickstartTemplateId = kickstartTemplates[0]._id.toString(); | |
db.collection('topics-tpl').find({ 'kickstart_tpl_id': kickstartTemplateId }, function(topicTemplateErr, topicTemplates) { | |
// calculate number of days, and the topics per day. | |
var numberOfDays = Math.floor((endDate - startDate) / 86400000) + 1; | |
var topicsPerDay = Math.ceil(topicTemplates.length / numberOfDays); | |
// look up the subtopic-tpls | |
db.collection('subtopics-tpl').find({ 'kickstart_tpl_id': kickstartTemplateId }, function(subTopicTemplateErr, subTopicTemplates) { | |
var subTopicTemplates = subTopicTemplates; | |
// create our new kickstart object with the Kinvey _acl properties. | |
var kickstartObjectWithAcl = modules.utils.kinveyEntity({ | |
name: request.body.name, | |
description: request.body.description, | |
startDate: request.body.startDate, | |
endDate: request.body.endDate | |
}); | |
kickstartObjectWithAcl._acl = { | |
creator: request.body.createdBy | |
}; | |
// insert our new kickstart into the kickstarts collection | |
db.collection('kickstarts').insert(kickstartObjectWithAcl, [{ w: 1 }, { fsync: true }, { safe: true }], function(err, kickstart) { | |
var kickstartId = kickstart[0]._id.toString(); | |
var topics = []; | |
// we need to take our topics from the template, and insert them into the topics collection with the new kickstart _id. | |
topicTemplates.forEach(function(topicTemplate, index) { | |
// calculate the day_offset per topic. | |
var dayOffset = Math.floor(index / topicsPerDay) + ',' + (index % topicsPerDay); | |
var topic = modules.utils.kinveyEntity({ | |
day_offset: dayOffset, | |
name: topicTemplate.name, | |
overview: topicTemplate.overview, | |
kickstart_id: kickstartId // the associated kickstart_id | |
}); | |
topics.push(topic); | |
}); | |
// db.collection('subtopics').insert({ name: 'from topicTemplate.forEach ' + Date.now() }); | |
// insert our topics with the associated kickstart _id | |
logger.info('BEFORE: topics insert()'); | |
// SUCCESSFULLY INSERTS DOCUMENTS | |
db.collection('topics').insert(topics, [{ w: 1 }, { fsync: true }, { safe: true }, { continueOnError: true }], function(insertedTopicsErr, insertedTopics) { | |
// NEVER GETS HERE | |
logger.info('AFTER: topics insert()'); | |
// after we insert the topic, we need to insert the subtopics | |
// subTopicTemplates.forEach(function(subTopicTemplate, index) { | |
// // if (subtopicTemplate.topic_tpl_id == topicTemplate._id.toString()) { | |
// var subtopic = modules.utils.kinveyEntity({ | |
// name: subTopicTemplate.name, | |
// examples: subTopicTemplate.examples, | |
// instructional_text: subTopicTemplate.instructional_text, | |
// placeholder_text: subTopicTemplate.placeholder_text, | |
// kickstart_id: kickstartId, | |
// topic_id: insertedTopic._id | |
// }); | |
// // db.collection('subtopics').insert({ name: 'foobar ' + Date.now() }); | |
// // db.collection('subtopics').insert({ name: 'foobar' }, { safe: true }, function(subTopicError, subTopic) { | |
// // }); | |
// // } | |
// }); // subTopicTemplates forEach | |
}); | |
// }); // topicTemplates forEach | |
response.body.kickstart = kickstart[0]; | |
response.body.subTopicTemplates = subTopicTemplates; | |
response.complete(200); | |
return; | |
}); // insert kickstart | |
}); // subtopics-tpl | |
}); // topics-tpl | |
}); // kickstarts-tpl | |
} // onRequest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment