Skip to content

Instantly share code, notes, and snippets.

@dmackerman
Created April 8, 2014 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmackerman/10081641 to your computer and use it in GitHub Desktop.
Save dmackerman/10081641 to your computer and use it in GitHub Desktop.
// TODO: ADD ERROR CONDITIONS
function onRequest(request, response, modules) {
// setup modules
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(err, topicTemplates) {
if (err) {
response.body = err;
response.complete(501);
return;
}
// 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({}, function(err, subTopicTemplates) {
if (err) {
response.body = err;
response.complete(501);
return;
}
// create our new kickstart object with the Kinvey _acl properties.
var newKickstart = modules.utils.kinveyEntity({
name: request.body.name,
description: request.body.description,
startDate: request.body.startDate,
endDate: request.body.endDate,
admins: [request.body.createdBy]
});
newKickstart._acl = {
creator: request.body.createdBy
};
// arrays to store our topics and subtopics.
var topics = [],
topicRefs = [],
subTopics = [],
subTopicRefs = {};
// before we create our topics, we need to insert the subtopics so we can get their IDs and store them as KinveyRefs inside of our topics
subTopicTemplates.forEach(function(subTopicTemplate) {
var subTopic = modules.utils.kinveyEntity({
name: subTopicTemplate.name,
examples: subTopicTemplate.examples,
instructional_text: subTopicTemplate.instructional_text,
placeholder_text: subTopicTemplate.placeholder_text,
topic_tpl_id: subTopicTemplate.topic_tpl_id
});
subTopics.push(subTopic);
});
db.collection('subtopics').insert(subTopics, { safe: true }, function(err, insertedSubTopics) {
if (err) {
response.body = err;
response.complete(501);
return;
}
// once we've inserted the subtopics, we need to create KinveyRefs for them, which then get inserted into the topic which they belong.
insertedSubTopics.forEach(function(subTopic) {
var subTopicRef = {
_id: subTopic._id,
_type: "KinveyRef",
_collection: "subtopics",
};
subTopicRefs[subTopic._id] = subTopicRef;
});
// 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) {
// for each of the topic templates, insert a new topic into the topics collection
// 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,
topic_tpl_id: db.objectID(topicTemplate._id).toString(), // a link back to the template.
kickstart_id: db.objectID(newKickstart._id).toString(), // the associated kickstart_id
subtopics: []
});
topic._acl = {
creator: request.body.createdBy
};
topics.push(topic);
});
// do a bulk insert of our topics into the topics collection
db.collection('topics').insert(topics, { safe: true }, function(err, insertedTopics) {
if (err) {
response.body = err;
response.complete(501);
return;
}
// for each of the inserted topics, create a KinveyRef that we'll stick into the kickstart
insertedTopics.forEach(function(topic) {
var topicRef = {
_id: topic._id,
_type: "KinveyRef",
_collection: "topics",
};
topicRefs.push(topicRef);
});
// once our subtopics and topics are inserted, we need to do some checking and
// insert subtopics refs into their proper topic
var insertedCounter = 0;
var counter = [];
var updatedTopics = [];
var topicIds = [];
insertedTopics.forEach(function(topic) {
var topicSubTopics = [];
insertedSubTopics.forEach(function(subTopic, index) {
if (subTopic.topic_tpl_id.toString() == topic.topic_tpl_id.toString()) {
topicSubTopics.push(subTopicRefs[subTopic._id]);
}
});
topicIds.push(db.objectID(topic._id).toString());
db.collection('topics').update(
{ _id: db.objectID(topic._id) },
{
$set: { name: 'I UPDATED THIS THING' }
},
function(err, updatedTopic) {
if (err) {
response.body = err;
response.complete(501);
return;
}
insertedCounter++;
updatedTopics.push(updatedTopic);
counter.push(insertedCounter);
//After all of them have completed let's continue
if (insertedCounter == insertedTopics.length - 1) {
newKickstart.name = request.body.name;
newKickstart.description = request.body.description;
newKickstart.startDate = request.body.startDate;
newKickstart.endDate = request.body.endDate;
newKickstart.admins = [request.body.createdBy];
newKickstart.topics = topicRefs; // these contain our subtopics
// insert our kickstart with our embedded refs
db.collection('kickstarts').insert(newKickstart, function(err, newKickstart) {
if (err) {
response.body = err;
response.complete(501);
return;
}
response.body = {
success: true,
counter: counter,
topicSubTopics: topicSubTopics,
kickstart: newKickstart,
topicIds: topicIds,
updatedTopics: updatedTopics,
};
response.complete(200);
return;
});
}
});
});
});
});
}); // 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