Skip to content

Instantly share code, notes, and snippets.

@manduks
Last active August 29, 2015 14:20
Show Gist options
  • Save manduks/87f089ea6bc6dc7092ff to your computer and use it in GitHub Desktop.
Save manduks/87f089ea6bc6dc7092ff to your computer and use it in GitHub Desktop.
/* Used for bulk edit. Updates timelines from csv rows
* @key [String] Array of header/field names from csv
* @rows [[String]] Array of rows. Each row is array of field values
*/
timelineSchema.statics.updateFromArray = function (key, rows, clientEngagementId, callback) {
var counter = rows.length, errors = [];
var keyLength = key.length;
var model = this,
indexKey,
entry = {},
entries = [];
rows.forEach(function (row) {
var json = {otherProps: {}, entries: []};
/* Create a json object from each row mapping header fields to corresponding values */
for (var i = 0; i < keyLength; i++) {
/* otherProps are broken apart since mongoose can't simply set them via $set */
if (key[i] && key[i].match(/^otherProps\./)) {
json.otherProps[key[i].replace(/^otherProps\./, '')] = row[i];
continue;
}
// formatting entries for timeline
if (key[i] && key[i].match(/^entries\./)) {
indexKey = key[i].replace(/^entries\./, '').split('.');
entry = json.entries[indexKey[0]] || {};
entry[indexKey[1]] = row[i];
json.entries[indexKey[0]] = entry;
continue;
}
json[key[i]] = json[key[i]] || row[i];
}
var filter = {};
filter.topBoxId = json.topBoxId || null;
delete json.topBoxId;
model.findOne(filter, function (err, timeline) {
if (err) {
errors.push({
topboxId: filter.topBoxId,
cause : 'Error with ' + JSON.stringify(filter) + ' ' + JSON.stringify(json) + ' ' + err.message,
object : JSON.stringify(json),
message : err.message
});
counter--;
if (counter <= 0) return callback(errors);
}
else {
timeline = timeline || new model({
topBoxId : filter.topBoxId,
isObserved: Ext.isEmpty(json.isObserved) ? true : json.isObserved
});
timeline.otherProps = timeline.otherProps || {};
Ext.merge(timeline.otherProps, json.otherProps);
delete json.otherProps;
//merge entries with old or add new ones
if (!Ext.isEmpty(timeline.entries)) {
entries = timeline.entries;
Ext.each(entries, function (item, index) {
if (!Ext.isEmpty(json.entries[index])) {
Ext.apply(item, json.entries[index]);
json.entries = Ext.Array.remove(json.entries, json.entries[index]);
entries[index] = item;
}
});
json.entries = json.entries.filter(function (item) {
return !!item;
});
Ext.each(json.entries, function (item, index) {
entries.push(item);
});
timeline.entries = entries;
delete json.entries;
}
if (!Ext.isEmpty(json.entries)) {
json.entries = json.entries.filter(function (item) {
return !!item;
});
}
json.clientEngagementId = json.clientEngagementId || clientEngagementId;
timeline.set(json);
timeline.markModified('otherProps');
timeline.isSaved = true;
timeline.save(function (err) {
if (err) errors.push({
topBoxId: filter.topBoxId,
cause : 'Error with ' + JSON.stringify(filter) + ' ' + JSON.stringify(json) + ' ' + err.message,
object : JSON.stringify(json),
message : err.message
});
counter--;
if (counter <= 0) return callback(errors);
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment