This commit is contained in:
2020-01-28 19:02:58 +00:00
parent a653ff4963
commit 9a9641a96b
3 changed files with 53 additions and 47 deletions

View File

@@ -507,60 +507,66 @@ namespace AyaNova.Util
public static async Task GenSeedUserAsync(ILogger log, int count, AuthorizationRoles roles, UserType userType, decimal timeZoneOffset,
bool active = true, string login = null, string password = null, long localeId = 0)
{
if (localeId == 0)
localeId = ServerBootConfig.AYANOVA_DEFAULT_LANGUAGE_ID;
using (var ct = ServiceProviderProvider.DBContext)
{
UserBiz Biz = UserBiz.GetBiz(ct);
//Don't do the following commented out, it's slower
// using (var ct = ServiceProviderProvider.DBContext)
// {
// UserBiz Biz = UserBiz.GetBiz(ct);
Faker Fake = new Faker();
for (int x = 0; x < count; x++)
{
User u = new User();
u.Active = active;
var p = new Bogus.Person();
u.Name = Uniquify(p.FullName);
if (login != null)
{
u.Login = login;
u.Name += " - " + login;
}
else
u.Login = p.FirstName;
if (password != null)
u.Password = password;
else
u.Password = u.Login;
u.Roles = roles;
u.LocaleId = localeId;
u.UserType = userType;
u.EmployeeNumber = "A-" + (454 + SeededUserCount).ToString() + "-Y";
u.Notes = Fake.Lorem.Sentence();//Fake.Lorem.Paragraph(2);
//TODO: After have USER and HEADOFFICE and VENDOR, if usertype is subcontractor or client or headoffice it needs to set a corresponding user's parent org record id to go with it
u.Tags = RandomTags(Fake);
//Children and relations
u.UserOptions = new UserOptions();
u.UserOptions.EmailAddress = p.Email.Replace("gmail.com", "helloayanova.com").Replace("hotmail.com", "helloayanova.com").Replace("yahoo.com", "helloayanova.com");
u.UserOptions.TimeZoneOffset = timeZoneOffset;
//this seems wrong but is actually faster!?
UserBiz Biz = UserBiz.GetBiz(ServiceProviderProvider.DBContext);
//allow creation of not entirely ready users (missing client id or subcontractor vendor id etc)
Biz.SeedOrImportRelaxedRulesMode = true;
Faker Fake = new Faker();
for (int x = 0; x < count; x++)
var NewObject = await Biz.CreateAsync(u);
if (NewObject == null)
{
User u = new User();
u.Active = active;
var p = new Bogus.Person();
u.Name = Uniquify(p.FullName);
if (login != null)
{
u.Login = login;
u.Name += " - " + login;
}
else
u.Login = p.FirstName;
if (password != null)
u.Password = password;
else
u.Password = u.Login;
u.Roles = roles;
u.LocaleId = localeId;
u.UserType = userType;
u.EmployeeNumber = "A-" + (454 + SeededUserCount).ToString() + "-Y";
u.Notes = Fake.Lorem.Sentence();//Fake.Lorem.Paragraph(2);
//TODO: After have USER and HEADOFFICE and VENDOR, if usertype is subcontractor or client or headoffice it needs to set a corresponding user's parent org record id to go with it
u.Tags = RandomTags(Fake);
//Children and relations
u.UserOptions = new UserOptions();
u.UserOptions.EmailAddress = p.Email.Replace("gmail.com", "helloayanova.com").Replace("hotmail.com", "helloayanova.com").Replace("yahoo.com", "helloayanova.com");
u.UserOptions.TimeZoneOffset = timeZoneOffset;
var NewObject = await Biz.CreateAsync(u);
if (NewObject == null)
{
log.LogError($"Seeder::GenSeedUser error creating user {u.Name}\r\n" + Biz.GetErrorsAsString());
throw new System.Exception("Seeder::GenSeedUser error creating user\r\n" + Biz.GetErrorsAsString());
}
log.LogError($"Seeder::GenSeedUser error creating user {u.Name}\r\n" + Biz.GetErrorsAsString());
throw new System.Exception("Seeder::GenSeedUser error creating user\r\n" + Biz.GetErrorsAsString());
}
//}
}
SeededUserCount += count;
@@ -571,6 +577,9 @@ namespace AyaNova.Util
//
public static async Task GenSeedWidgetAsync(ILogger log, int count)
{
//this is 4 times slower than doing it inside the loop below
//seems counterintuitive but maybe it's to do with the db context not being refreshed?
// WidgetBiz biz = WidgetBiz.GetBiz(ServiceProviderProvider.DBContext);
var f = new Bogus.Faker();
@@ -605,7 +614,7 @@ namespace AyaNova.Util
o.CustomFields = $@"{{c1:""{c1}"",c2:""{c2}"",c3:{c3},c4:{c4},c5:{c5}}}";
//This seems wrong to do in a loop but is 4 times faster this way ?!?
WidgetBiz biz = WidgetBiz.GetBiz(ServiceProviderProvider.DBContext);
var NewObject = await biz.CreateAsync(o);
if (NewObject == null)