This commit is contained in:
286
server/AyaNova/util/Seeder.cs
Normal file
286
server/AyaNova/util/Seeder.cs
Normal file
@@ -0,0 +1,286 @@
|
||||
using System;
|
||||
using AyaNova.Models;
|
||||
using AyaNova.Biz;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Bogus;
|
||||
using AyaNova.Api.ControllerHelpers;
|
||||
|
||||
|
||||
namespace AyaNova.Util
|
||||
{
|
||||
|
||||
public static class Seeder
|
||||
{
|
||||
|
||||
public enum SeedLevel { SmallOneManShopTrialDataSet, MediumLocalServiceCompanyTrialDataSet, LargeCorporateMultiRegionalTrialDataSet };
|
||||
|
||||
// //////////////////////////////////////////////////////
|
||||
// //Seed database with default manager account
|
||||
// //
|
||||
// public static User GenerateDefaultManagerAccountUser()
|
||||
// {
|
||||
// User u = new User();
|
||||
// u.Name = "AyaNova Administrator";
|
||||
// u.Salt = Hasher.GenerateSalt();
|
||||
// u.Login = "manager";
|
||||
// u.Password = Hasher.hash(u.Salt, "l3tm3in");
|
||||
// u.Roles = AuthorizationRoles.BizAdminFull | AuthorizationRoles.OpsAdminFull | AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryFull;
|
||||
// u.OwnerId = 1;
|
||||
// return u;
|
||||
// }
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
//Seed database for trial and testing purposes
|
||||
//
|
||||
public static void SeedDatabase(AyContext ct, SeedLevel slevel)
|
||||
{
|
||||
ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("Seeder");
|
||||
ApiServerState apiServerState = (ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(ApiServerState));
|
||||
|
||||
//get the current server state so can set back to it later
|
||||
ApiServerState.ServerState wasServerState = apiServerState.GetState();
|
||||
string wasReason=apiServerState.Reason;
|
||||
|
||||
try
|
||||
{
|
||||
log.LogInformation("SEEDER: SeedDatabase, level is: " + slevel.ToString());
|
||||
|
||||
//Only allow this in a trial database
|
||||
if (!AyaNova.Core.License.ActiveKey.TrialLicense)
|
||||
{
|
||||
throw new System.NotSupportedException("This database has a registered license key and can't be seeded.");
|
||||
}
|
||||
|
||||
log.LogInformation("Setting server state to OpsOnly");
|
||||
apiServerState.SetOpsOnly("Seeding database");
|
||||
|
||||
//Erase all the data except for the license, schema and the manager user
|
||||
DbUtil.PrepareDatabaseForSeeding(log);
|
||||
|
||||
var f = new Faker("en");
|
||||
|
||||
//Seed special test data for integration testing
|
||||
SeedTestData(ct);
|
||||
|
||||
|
||||
switch (slevel)
|
||||
{
|
||||
|
||||
//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);
|
||||
|
||||
//Generate one office person / secretary
|
||||
GenSeedUser(1, ct, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryFull | AuthorizationRoles.AccountingFull);
|
||||
|
||||
//200 widgets
|
||||
GenSeedWidget(200, ct);
|
||||
|
||||
break;
|
||||
//This is for a typical AyaNova medium busy shop
|
||||
//has one location, many techs and full staff for each department
|
||||
case SeedLevel.MediumLocalServiceCompanyTrialDataSet:
|
||||
|
||||
//One IT administrator, can change ops but nothing else
|
||||
GenSeedUser(1, ct, AuthorizationRoles.OpsAdminFull);
|
||||
|
||||
//One business administrator, can view ops issues
|
||||
GenSeedUser(1, ct, AuthorizationRoles.BizAdminFull | AuthorizationRoles.OpsAdminLimited);
|
||||
|
||||
//One owner who doesn't control anything but views stuff
|
||||
GenSeedUser(1, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited | AuthorizationRoles.OpsAdminLimited);
|
||||
|
||||
//20 techs
|
||||
GenSeedUser(20, ct, AuthorizationRoles.TechFull | AuthorizationRoles.DispatchLimited);
|
||||
|
||||
//2 subcontractors
|
||||
GenSeedUser(2, ct, AuthorizationRoles.SubContractorFull);
|
||||
|
||||
//3 sales / generic office people people
|
||||
GenSeedUser(3, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited);
|
||||
|
||||
//1 dispatch manager
|
||||
GenSeedUser(1, ct, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryLimited);
|
||||
|
||||
//1 Inventory manager
|
||||
GenSeedUser(1, ct, AuthorizationRoles.InventoryFull | AuthorizationRoles.DispatchLimited);
|
||||
|
||||
//1 accountant / bookkeeper
|
||||
GenSeedUser(1, ct, AuthorizationRoles.AccountingFull | AuthorizationRoles.BizAdminLimited);
|
||||
|
||||
//10 full on client users
|
||||
GenSeedUser(10, ct, AuthorizationRoles.ClientLimited);
|
||||
|
||||
//10 limited client users
|
||||
GenSeedUser(10, ct, AuthorizationRoles.ClientLimited);
|
||||
|
||||
//2000 widgets
|
||||
GenSeedWidget(2000, ct);
|
||||
|
||||
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);
|
||||
|
||||
//business administrator, can view ops issues
|
||||
GenSeedUser(2, ct, AuthorizationRoles.BizAdminFull | AuthorizationRoles.OpsAdminLimited);
|
||||
|
||||
//owner / upper management who doesn't control anything but views stuff
|
||||
GenSeedUser(5, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited | AuthorizationRoles.OpsAdminLimited);
|
||||
|
||||
//techs
|
||||
GenSeedUser(100, ct, AuthorizationRoles.TechFull | AuthorizationRoles.DispatchLimited);
|
||||
|
||||
//limited techs
|
||||
GenSeedUser(50, ct, AuthorizationRoles.TechLimited | AuthorizationRoles.DispatchLimited);
|
||||
|
||||
//20 subcontractors
|
||||
GenSeedUser(20, ct, AuthorizationRoles.SubContractorFull);
|
||||
|
||||
//10 limited subcontractors
|
||||
GenSeedUser(10, ct, AuthorizationRoles.SubContractorLimited);
|
||||
|
||||
//30 sales / generic office people people
|
||||
GenSeedUser(30, ct, AuthorizationRoles.DispatchLimited | AuthorizationRoles.InventoryLimited);
|
||||
|
||||
//5 dispatch manager
|
||||
GenSeedUser(5, ct, AuthorizationRoles.DispatchFull | AuthorizationRoles.InventoryLimited);
|
||||
|
||||
//5 Inventory manager
|
||||
GenSeedUser(5, ct, AuthorizationRoles.InventoryFull | AuthorizationRoles.DispatchLimited);
|
||||
|
||||
//10 Inventory manager assistants
|
||||
GenSeedUser(5, ct, AuthorizationRoles.InventoryLimited);
|
||||
|
||||
//5 accountant / bookkeeper
|
||||
GenSeedUser(5, ct, AuthorizationRoles.AccountingFull | AuthorizationRoles.BizAdminLimited);
|
||||
|
||||
//100 full on client users
|
||||
GenSeedUser(100, ct, AuthorizationRoles.ClientFull);
|
||||
|
||||
//100 limited client users
|
||||
GenSeedUser(100, ct, AuthorizationRoles.ClientLimited);
|
||||
|
||||
//20000 widgets
|
||||
GenSeedWidget(20000, ct);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
log.LogInformation("Seeding completed successfully");
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
log.LogInformation($"Seeder: setting server state back to {wasServerState.ToString()}");
|
||||
apiServerState.SetState(wasServerState, wasReason);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
//Seed test data for integration tests
|
||||
//
|
||||
public static void SeedTestData(AyContext ct)
|
||||
{
|
||||
//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");
|
||||
|
||||
//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");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
//Seed user - default login / pw is first name
|
||||
//
|
||||
public static void GenSeedUser(int count, AyContext ct, AuthorizationRoles roles, string login = null, string password = null)
|
||||
{
|
||||
|
||||
for (int x = 0; x < count; x++)
|
||||
{
|
||||
User u = new User();
|
||||
var p = new Bogus.Person();
|
||||
u.Name = p.FullName;
|
||||
u.Salt = Hasher.GenerateSalt();
|
||||
if (login != null)
|
||||
{
|
||||
u.Login = login;
|
||||
u.Name += " - " + login;
|
||||
}
|
||||
else
|
||||
u.Login = p.FirstName;
|
||||
if (password != null)
|
||||
u.Password = Hasher.hash(u.Salt, password);
|
||||
else
|
||||
u.Password = Hasher.hash(u.Salt, u.Login);
|
||||
u.Roles = roles;
|
||||
u.LocaleId=ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID;
|
||||
ct.User.Add(u);
|
||||
}
|
||||
ct.SaveChanges();
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
//Seed widget for testing
|
||||
//
|
||||
public static void GenSeedWidget(int count, AyContext ct)
|
||||
{
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}//eoc
|
||||
|
||||
|
||||
|
||||
}//eons
|
||||
Reference in New Issue
Block a user