const mongoose = require('mongoose');
const mocks = require('./__mocks__');

function processArray(array) {
  return array.reduce(p => p, Promise.resolve());
}

module.exports = async () => {
  const User = mongoose.model('User');
  const Entry = mongoose.model('Entry');
  const Section = mongoose.model('Section');
  const UserGroup = mongoose.model('UserGroup');
  const Field = mongoose.model('Field');

  const deleteSections = async () => {
    const done = await Section.remove();
    return done;
  };

  const deleteUsers = async () => {
    const done = await User.remove();
    return done;
  };

  const deleteEntries = async () => {
    const done = await Entry.remove();
    return done;
  };

  const deleteUserGroups = async () => {
    const done = await UserGroup.remove();
    return done;
  };

  const deleteFields = async () => {
    const done = await Field.remove();
    return done;
  };


  const addUsers = async () => {
    const done = await User.create(mocks.users);
    return done;
  };

  const addFields = async () => {
    const done = await Field.create(mocks.fields);
    return done;
  };

  const addEntries = async () => {
    const done = await Entry.create(mocks.entries);
    return done;
  };

  const addSections = async () => {
    const done = await Section.create(mocks.sections);
    return done;
  };

  const addUserGroups = async () => {
    const done = await UserGroup.create(mocks.usergroups);
    return done;
  };

  return processArray([
    Promise.all([
      await deleteUsers(),
      await deleteUserGroups(),
      await deleteSections(),
      await deleteEntries(),
      await deleteFields(),
    ]),
    await addFields(),
    await addUserGroups(),
    await addUsers(),
    await addSections(),
    await addEntries(),
  ]);
};

@@ -197,513 +197,306 @@
 nst 
-User = mongoose.model('User');%0A  const Entry = mongoose.model('Entry');%0A  const Section = mongoose.model('Section');%0A  const UserGroup = mongoose.model('UserG
+collections = %5B%0A    %7B model: 'UserGroup', mocks: mocks.userg
 roup
-');%0A  const Field = mongoose.model('Field');%0A%0A  const deleteSections = async () =%3E %7B%0A    const done = await Section.remove();%0A    return done;%0A  %7D;%0A%0A  const deleteUsers = async () =%3E %7B%0A    const done = await User.remove();%0A    return done;%0A  %7D;%0A%0A  const deleteEntries = async () =%3E %7B%0A    const done = await Entry.remove();%0A    return done;%0A  %7D
+s %7D,%0A    %7B model: 'User', mocks: mocks.users %7D,%0A    %7B model: 'Section', mocks: mocks.sections %7D,%0A    %7B model: 'Entry', mocks: mocks.entries %7D,%0A    %7B model: 'Field', mocks: mocks.fields %7D,%0A    %7B model: 'Page', mocks: mocks.pages %7D,%0A  %5D
 ;%0A%0A 
@@ -502,32 +502,24 @@
   const 
-deleteUserGroups
+addModel
  = async
@@ -512,32 +512,41 @@
 dModel = async (
+modelName
 ) =%3E %7B%0A    const
@@ -550,302 +550,124 @@
 nst 
-done = await UserGroup.remove();%0A    return done;%0A  %7D;%0A%0A  const deleteFields = async () =%3E %7B%0A    const done = await Field.remove();%0A    return done;%0A  %7D;%0A%0A%0A  const addUsers = async () =%3E %7B%0A    const done = await User.create(mocks.users);%0A    return done;%0A  %7D;%0A%0A  const addFields = async () =%3E %7B
+%7B mocks: mockData %7D = collections.find(obj =%3E obj.model === modelName);%0A    const Model = mongoose.model(modelName);
 %0A   
@@ -686,21 +686,21 @@
 = await 
-Fi
+Mod
 el
-d
 .create(
@@ -703,24 +703,20 @@
 ate(mock
-s.fields
+Data
 );%0A    r
@@ -739,145 +739,85 @@
 %0A%0A  
-const addEntries = async () =%3E %7B%0A    const done = await Entry.create(mocks.entries);%0A    return done;%0A  %7D;%0A%0A  const addS
+return processArray(%5B%0A    await Promise.all(coll
 ections
- = 
+.map(
 async (
+collection
 ) =%3E
@@ -827,179 +827,101 @@
     
+  
 const 
-done = await Section.create(mocks.sections);%0A    return done;%0A  %7D;%0A%0A  const addUserGroups = async () =%3E %7B%0A    const done = await UserGroup.create(mocks.usergroups
+Model = mongoose.model(collection.model);%0A      const done = await Model.remove(
 );%0A
+  
     
@@ -939,39 +939,24 @@
 ;%0A  
-%7D;%0A%0A  return processArray(%5B%0A   
+  %7D)),%0A    await
  Pro
@@ -982,20 +982,24 @@
 ait 
-deleteUsers(
+addModel('Field'
 ),%0A 
@@ -1009,22 +1009,26 @@
   await 
-delete
+addModel('
 UserGrou
@@ -1024,26 +1024,25 @@
 l('UserGroup
-s(
+'
 ),%0A      awa
@@ -1048,23 +1048,26 @@
 ait 
-delete
+addModel('
 Section
-s(
+'
 ),%0A 
@@ -1081,179 +1081,90 @@
 ait 
-deleteEntries(),%0A      await deleteFields(),%0A    %5D),%0A    await addFields(),%0A    await addUserGroups(),%0A    await addUsers(),%0A    await addSections(),%0A    await addEntries(
+addModel('Entry'),%0A      await addModel('Page'),%0A    %5D),%0A    await addModel('User'
 ),%0A 

Improve populateDB setup