identity
Version:
Identity client
85 lines (66 loc) • 2.36 kB
JavaScript
module.exports = function(grunt){
grunt.registerTask('identity:values:resync', 'resync all values', function(){
var done = this.async();
grunt.startActionhero(function(api){
var db = api.db;
var Value = api.identity.store.Model('Value');
if(!db){
grunt.log.error('No ah-openrecord-plugin found or initialized');
return done(false);
}
api.identity.application('identity').del('application/clear/values', function(err, resp){
if(err || resp.error){
grunt.log.error(err || resp.error)
return done(false);
}else{
var entities = {}
//search all models which are configured to be an identity value
for(var model_name in db.models){
if(db.models.hasOwnProperty(model_name)){
var model = db.models[model_name];
if(typeof model.definition._identity_sync === 'string'){
var entity_id = model.definition._identity_sync;
entities[entity_id] = model;
}
}
}
var run = [];
var records = {}
for(var entity_id in entities){
var model = entities[entity_id];
(function(entity_id){
run.push(model.exec(function(result){
records[entity_id] = result;
}));
})(entity_id);
}
Value.all(run).then(function(){
run = [];
for(var entity_id in records){
for(var i = 0; i < records[entity_id].length; i++){
var record = records[entity_id][i];
var name = record.identitySync();
if(name){
run.push(Value.create({
id: record.id,
name: name,
entity_id: entity_id
}));
}
}
}
Value.all(run).then(function(){
done();
}).catch(function(err){
grunt.log.error(err);
done(false);
});
}).catch(function(err){
grunt.log.error(err);
done(false);
});
}
});
});
});
};