This commit is contained in:
2018-08-28 22:46:53 +00:00
parent 5cd914767d
commit 78ce79fc5c
4 changed files with 80 additions and 65 deletions

View File

@@ -383,7 +383,7 @@ namespace AyaNova
lb.ValidateLocales();
AyaNova.Core.License.Initialize(apiServerState, dbContext, _log);
AyaNova.Core.License.Fetch(apiServerState, dbContext, _log);
Util.Seeder.SeedDatabase(dbContext, Util.Seeder.SeedLevel.MediumLocalServiceCompanyTrialDataSet);
Util.Seeder.SeedDatabase(Util.Seeder.SeedLevel.LargeCorporateMultiRegionalTrialDataSet);
#endif

View File

@@ -75,7 +75,7 @@ namespace AyaNova.Biz
//Get the import filename from the jsondata
JObject jobData = JObject.Parse(job.JobInfo);
var seedLevel = (Seeder.SeedLevel)jobData["seedLevel"].Value<int>();
Seeder.SeedDatabase(ct, seedLevel);
Seeder.SeedDatabase(seedLevel);
JobsBiz.LogJob(job.GId, "Finished.", ct);
JobsBiz.UpdateJobStatus(job.GId, JobStatus.Completed, ct);
await Task.CompletedTask;

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using AyaNova.Models;
using AyaNova.Biz;
using Microsoft.Extensions.Logging;
@@ -34,7 +35,7 @@ namespace AyaNova.Util
//////////////////////////////////////////////////////
//Seed database for trial and testing purposes
//
public static void SeedDatabase(AyContext ct, SeedLevel slevel)
public static void SeedDatabase(SeedLevel slevel)
{
ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("Seeder");
ApiServerState apiServerState = (ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(ApiServerState));
@@ -62,7 +63,7 @@ namespace AyaNova.Util
var f = new Faker("en");
//Seed special test data for integration testing
SeedTestData(ct);
SeedTestData();
switch (slevel)
@@ -71,13 +72,13 @@ namespace AyaNova.Util
//This is for a busy but one man shop with a single office person handling stuff back at the shop
case SeedLevel.SmallOneManShopTrialDataSet:
//Generate owner and lead tech
GenSeedUser(1, ct, AuthorizationRoles.BizAdminFull | AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryFull | AuthorizationRoles.OpsAdminFull);
GenSeedUser(1, AuthorizationRoles.BizAdminFull | AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryFull | AuthorizationRoles.OpsAdminFull);
//Generate one office person / secretary
GenSeedUser(1, ct, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryFull | AuthorizationRoles.AccountingFull);
GenSeedUser(1, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryFull | AuthorizationRoles.AccountingFull);
//200 widgets
GenSeedWidget(200, ct);
GenSeedWidget(200);
break;
//This is for a typical AyaNova medium busy shop
@@ -85,90 +86,89 @@ namespace AyaNova.Util
case SeedLevel.MediumLocalServiceCompanyTrialDataSet:
//One IT administrator, can change ops but nothing else
GenSeedUser(1, ct, AuthorizationRoles.OpsAdminFull);
GenSeedUser(1, AuthorizationRoles.OpsAdminFull);
//One business administrator, can view ops issues
GenSeedUser(1, ct, AuthorizationRoles.BizAdminFull | AuthorizationRoles.OpsAdminLimited);
GenSeedUser(1, AuthorizationRoles.BizAdminFull | AuthorizationRoles.OpsAdminLimited);
//One owner who doesn't control anything but views stuff
GenSeedUser(1, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited | AuthorizationRoles.OpsAdminLimited);
GenSeedUser(1, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited | AuthorizationRoles.OpsAdminLimited);
//20 techs
GenSeedUser(20, ct, AuthorizationRoles.TechFull | AuthorizationRoles.DispatchLimited);
GenSeedUser(20, AuthorizationRoles.TechFull | AuthorizationRoles.DispatchLimited);
//2 subcontractors
GenSeedUser(2, ct, AuthorizationRoles.SubContractorFull);
GenSeedUser(2, AuthorizationRoles.SubContractorFull);
//3 sales / generic office people people
GenSeedUser(3, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited);
GenSeedUser(3, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited);
//1 dispatch manager
GenSeedUser(1, ct, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryLimited);
GenSeedUser(1, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryLimited);
//1 Inventory manager
GenSeedUser(1, ct, AuthorizationRoles.InventoryFull | AuthorizationRoles.DispatchLimited);
GenSeedUser(1, AuthorizationRoles.InventoryFull | AuthorizationRoles.DispatchLimited);
//1 accountant / bookkeeper
GenSeedUser(1, ct, AuthorizationRoles.AccountingFull | AuthorizationRoles.BizAdminLimited);
GenSeedUser(1, AuthorizationRoles.AccountingFull | AuthorizationRoles.BizAdminLimited);
//10 full on client users
GenSeedUser(10, ct, AuthorizationRoles.ClientLimited);
GenSeedUser(10, AuthorizationRoles.ClientLimited);
//10 limited client users
GenSeedUser(10, ct, AuthorizationRoles.ClientLimited);
GenSeedUser(10, AuthorizationRoles.ClientLimited);
//2000 widgets
GenSeedWidget(2000, ct);
//GenSeedWidget(100, ct);
GenSeedWidget(2000);
break;
//this is a large corporation with multiple branches in multiple locations all in the same country
//Each location has a full staff and corporate head office has an overarching staff member in charge of each location
case SeedLevel.LargeCorporateMultiRegionalTrialDataSet:
//IT administrator, can change ops but nothing else
GenSeedUser(2, ct, AuthorizationRoles.OpsAdminFull);
GenSeedUser(2, AuthorizationRoles.OpsAdminFull);
//business administrator, can view ops issues
GenSeedUser(2, ct, AuthorizationRoles.BizAdminFull | AuthorizationRoles.OpsAdminLimited);
GenSeedUser(2, AuthorizationRoles.BizAdminFull | AuthorizationRoles.OpsAdminLimited);
//owner / upper management who doesn't control anything but views stuff
GenSeedUser(5, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited | AuthorizationRoles.OpsAdminLimited);
GenSeedUser(5, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited | AuthorizationRoles.OpsAdminLimited);
//techs
GenSeedUser(100, ct, AuthorizationRoles.TechFull | AuthorizationRoles.DispatchLimited);
GenSeedUser(100, AuthorizationRoles.TechFull | AuthorizationRoles.DispatchLimited);
//limited techs
GenSeedUser(50, ct, AuthorizationRoles.TechLimited | AuthorizationRoles.DispatchLimited);
GenSeedUser(50, AuthorizationRoles.TechLimited | AuthorizationRoles.DispatchLimited);
//20 subcontractors
GenSeedUser(20, ct, AuthorizationRoles.SubContractorFull);
GenSeedUser(20, AuthorizationRoles.SubContractorFull);
//10 limited subcontractors
GenSeedUser(10, ct, AuthorizationRoles.SubContractorLimited);
GenSeedUser(10, AuthorizationRoles.SubContractorLimited);
//30 sales / generic office people people
GenSeedUser(30, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited);
GenSeedUser(30, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited);
//5 dispatch manager
GenSeedUser(5, ct, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryLimited);
GenSeedUser(5, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryLimited);
//5 Inventory manager
GenSeedUser(5, ct, AuthorizationRoles.InventoryFull | AuthorizationRoles.DispatchLimited);
GenSeedUser(5, AuthorizationRoles.InventoryFull | AuthorizationRoles.DispatchLimited);
//10 Inventory manager assistants
GenSeedUser(5, ct, AuthorizationRoles.InventoryLimited);
GenSeedUser(5, AuthorizationRoles.InventoryLimited);
//5 accountant / bookkeeper
GenSeedUser(5, ct, AuthorizationRoles.AccountingFull | AuthorizationRoles.BizAdminLimited);
GenSeedUser(5, AuthorizationRoles.AccountingFull | AuthorizationRoles.BizAdminLimited);
//100 full on client users
GenSeedUser(100, ct, AuthorizationRoles.ClientFull);
GenSeedUser(100, AuthorizationRoles.ClientFull);
//100 limited client users
GenSeedUser(100, ct, AuthorizationRoles.ClientLimited);
GenSeedUser(100, AuthorizationRoles.ClientLimited);
//20000 widgets
GenSeedWidget(20000, ct);
GenSeedWidget(20000);
break;
}
@@ -192,28 +192,29 @@ namespace AyaNova.Util
//////////////////////////////////////////////////////
//Seed test data for integration tests
//
public static void SeedTestData(AyContext ct)
public static void SeedTestData()
{
//TEST USERS
//one of each role type
GenSeedUser(1, ct, AuthorizationRoles.BizAdminLimited, "BizAdminLimited", "BizAdminLimited");
GenSeedUser(1, ct, AuthorizationRoles.BizAdminFull, "BizAdminFull", "BizAdminFull");
GenSeedUser(1, ct, AuthorizationRoles.DispatchLimited, "DispatchLimited", "DispatchLimited");
GenSeedUser(1, ct, AuthorizationRoles.DispatchFull, "DispatchFull", "DispatchFull");
GenSeedUser(1, ct, AuthorizationRoles.InventoryLimited, "InventoryLimited", "InventoryLimited");
GenSeedUser(1, ct, AuthorizationRoles.InventoryFull, "InventoryFull", "InventoryFull");
GenSeedUser(1, ct, AuthorizationRoles.AccountingFull, "Accounting", "Accounting");
GenSeedUser(1, ct, AuthorizationRoles.TechLimited, "TechLimited", "TechLimited");
GenSeedUser(1, ct, AuthorizationRoles.TechFull, "TechFull", "TechFull");
GenSeedUser(1, ct, AuthorizationRoles.SubContractorLimited, "SubContractorLimited", "SubContractorLimited");
GenSeedUser(1, ct, AuthorizationRoles.SubContractorFull, "SubContractorFull", "SubContractorFull");
GenSeedUser(1, ct, AuthorizationRoles.ClientLimited, "ClientLimited", "ClientLimited");
GenSeedUser(1, ct, AuthorizationRoles.ClientFull, "ClientFull", "ClientFull");
GenSeedUser(1, ct, AuthorizationRoles.OpsAdminLimited, "OpsAdminLimited", "OpsAdminLimited");
GenSeedUser(1, ct, AuthorizationRoles.OpsAdminFull, "OpsAdminFull", "OpsAdminFull");
GenSeedUser(1, AuthorizationRoles.BizAdminLimited, "BizAdminLimited", "BizAdminLimited");
GenSeedUser(1, AuthorizationRoles.BizAdminFull, "BizAdminFull", "BizAdminFull");
GenSeedUser(1, AuthorizationRoles.DispatchLimited, "DispatchLimited", "DispatchLimited");
GenSeedUser(1, AuthorizationRoles.DispatchFull, "DispatchFull", "DispatchFull");
GenSeedUser(1, AuthorizationRoles.InventoryLimited, "InventoryLimited", "InventoryLimited");
GenSeedUser(1, AuthorizationRoles.InventoryFull, "InventoryFull", "InventoryFull");
GenSeedUser(1, AuthorizationRoles.AccountingFull, "Accounting", "Accounting");
GenSeedUser(1, AuthorizationRoles.TechLimited, "TechLimited", "TechLimited");
GenSeedUser(1, AuthorizationRoles.TechFull, "TechFull", "TechFull");
GenSeedUser(1, AuthorizationRoles.SubContractorLimited, "SubContractorLimited", "SubContractorLimited");
GenSeedUser(1, AuthorizationRoles.SubContractorFull, "SubContractorFull", "SubContractorFull");
GenSeedUser(1, AuthorizationRoles.ClientLimited, "ClientLimited", "ClientLimited");
GenSeedUser(1, AuthorizationRoles.ClientFull, "ClientFull", "ClientFull");
GenSeedUser(1, AuthorizationRoles.OpsAdminLimited, "OpsAdminLimited", "OpsAdminLimited");
GenSeedUser(1, AuthorizationRoles.OpsAdminFull, "OpsAdminFull", "OpsAdminFull");
//PRIVACY TEST USER - this is used for a test to see if user info leaks into the logs
GenSeedUser(1, ct, AuthorizationRoles.OpsAdminLimited, "TEST_PRIVACY_USER_ACCOUNT", "TEST_PRIVACY_USER_ACCOUNT");
GenSeedUser(1, AuthorizationRoles.OpsAdminLimited, "TEST_PRIVACY_USER_ACCOUNT", "TEST_PRIVACY_USER_ACCOUNT");
}
@@ -225,8 +226,9 @@ namespace AyaNova.Util
//////////////////////////////////////////////////////
//Seed user - default login / pw is first name
//
public static void GenSeedUser(int count, AyContext ct, AuthorizationRoles roles, string login = null, string password = null)
public static void GenSeedUser(int count, AuthorizationRoles roles, string login = null, string password = null)
{
AyContext ct = ServiceProviderProvider.DBContext;
for (int x = 0; x < count; x++)
{
@@ -256,35 +258,46 @@ namespace AyaNova.Util
//////////////////////////////////////////////////////
//Seed widget for testing
//
public static void GenSeedWidget(int count, AyContext ct)
public static void GenSeedWidget(int count)
{
var s="blah";
//get a context just for this op to save memory on changetracking
AyContext ct = ServiceProviderProvider.DBContext;
var f = new Bogus.Faker();
for (int x = 0; x < count; x++)
{
Widget o = new Widget();
var f = new Bogus.Faker();
o.Name = f.Commerce.ProductName();
o.Active = f.Random.Bool();
o.StartDate = f.Date.Between(DateTime.Now, DateTime.Now.AddMinutes(60));
o.EndDate = f.Date.Between(DateTime.Now.AddMinutes(90), DateTime.Now.AddHours(5));
o.DollarAmount = Convert.ToDecimal(f.Commerce.Price());
o.OwnerId = 1;
//this is nonsense but just to test an enum
o.Roles = AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited | AuthorizationRoles.OpsAdminLimited;
ct.Widget.Add(o);
// ct.SaveChanges();
//Log
EventLogProcessor.AddEntryNoSave(new Event(o.OwnerId, o.Id, AyaType.Widget, AyaEvent.Created), ct);
}
//Save the changes to get the ID values
ct.SaveChanges();
var v=s;
//Now that we have the ID values bulk add the event log entries
//To save a db call iterate the local collection in the context, but...
//can't modify the context in the foreach, even if it's another collection entirely, so need to save the id's in a temporary list
List<long> WidgetsAdded = new List<long>();
foreach (Widget w in ct.Widget.Local)
{
WidgetsAdded.Add(w.Id);
}
//Now we have all the id's can actually add them to the context
foreach (long l in WidgetsAdded)
{
EventLogProcessor.AddEntryNoSave(new Event(1, l, AyaType.Widget, AyaEvent.Created), ct);
}
//Now save the Event Log entries
ct.SaveChanges();
}
}//eoc