'use strict';

var UserStore = require('fh-wfm-user/lib/user/store')({
  persistent: process.env.WFM_USE_MEMORY_STORE !== 'true'
});

var Topics = require('fh-wfm-mediator/lib/topics');
var Promise = require('bluebird');

// TODO: replace this seed data for users with your own
// i.e. a single initial user to create others through the portal ap
// Seed data for users, replace with your own.
var data = require('./data.json');

function deleteAll(store) {
  var topics = store.topics;
  return topics.request('list').then(function(users) {
    return Promise.map(users, function(user) {
      return topics.request('delete', user.id, {uid: user.id});
    });
  });
}

/**
 * Mediator listener for the user module, manipulates 'wfm:user:*' mediator topics
 * @param  {Mediator} mediator a mediator instance to publish/subscribe to
 */
module.exports = function setupUserStore(mediator) {
  var store = new UserStore('user');

  return store.list().then(function(list) {
    // Dont push all seed data at start if there is data already, if you want to reset back to seed data use
    // DELETE'/admin/data or use Settings > Reset Data function on Portal Demo app
    if (list && list.length > 0) {
      data = store.isPersistent  ? null : [];
    }
    return store.init(data).then(function() {
      store.listen('', mediator);
      var dataTopics = new Topics(mediator);
      dataTopics.prefix('wfm').entity('data')
        .on('reset', function() {
          return deleteAll(store).then(function() {
            return store.init(data);
          });
        });
    });
  });
};

// export function for unit testing
module.exports.deleteAll = deleteAll;
@@ -6,16 +6,43 @@
 strict';
+%0Avar _ = require('lodash');
 %0A%0Avar Us
@@ -418,17 +418,21 @@
 wn.%0Avar 
-d
+seedD
 ata = re
@@ -1187,16 +1187,30 @@
 emo app%0A
+    var data;%0A
     if (
@@ -1268,17 +1268,16 @@
 sistent 
- 
 ? null :
@@ -1281,16 +1281,65 @@
 l : %5B%5D;%0A
+    %7D else %7B%0A      data = _.cloneDeep(seedData);%0A
     %7D%0A  
@@ -1623,21 +1623,38 @@
 re.init(
-d
+_.cloneDeep(seedD
 ata)
+)
 ;%0A      

Enforce cloning of seed data json