fixed seed (untested, just building)

This commit is contained in:
Ulf Gebhardt 2022-02-03 04:43:30 +01:00
parent d7cd4d32c0
commit fa7c782672
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
10 changed files with 30 additions and 78 deletions

View File

@ -1,30 +0,0 @@
import Faker from 'faker'
import { define } from 'typeorm-seeding'
import { LoginUser } from '../../entity/LoginUser'
import { randomBytes } from 'crypto'
import { LoginUserContext } from '../interface/UserContext'
define(LoginUser, (faker: typeof Faker, context?: LoginUserContext) => {
if (!context) context = {}
const user = new LoginUser()
user.email = context.email ? context.email : faker.internet.email()
user.firstName = context.firstName ? context.firstName : faker.name.firstName()
user.lastName = context.lastName ? context.lastName : faker.name.lastName()
user.username = context.username ? context.username : faker.internet.userName()
user.description = context.description ? context.description : faker.random.words(4)
// TODO Create real password and keys/hash
user.password = context.password ? context.password : BigInt(0)
user.pubKey = context.pubKey ? context.pubKey : randomBytes(32)
user.privKey = context.privKey ? context.privKey : randomBytes(80)
user.emailHash = context.emailHash ? context.emailHash : randomBytes(32)
user.createdAt = context.createdAt ? context.createdAt : faker.date.recent()
user.emailChecked = context.emailChecked === undefined ? false : context.emailChecked
user.passphraseShown = context.passphraseShown ? context.passphraseShown : false
user.language = context.language ? context.language : 'en'
user.disabled = context.disabled ? context.disabled : false
user.groupId = context.groupId ? context.groupId : 1
user.publisherId = context.publisherId ? context.publisherId : 0
return user
})

View File

@ -1,21 +1,31 @@
import Faker from 'faker' import Faker from 'faker'
import { define } from 'typeorm-seeding' import { define } from 'typeorm-seeding'
import { User } from '../../entity/User' import { User } from '../../entity/User'
import { randomBytes } from 'crypto' import { randomBytes, randomInt } from 'crypto'
import { UserContext } from '../interface/UserContext' import { UserContext } from '../interface/UserContext'
define(User, (faker: typeof Faker, context?: UserContext) => { define(User, (faker: typeof Faker, context?: UserContext) => {
if (!context) context = {} if (!context) context = {}
const user = new User() const user = new User()
user.pubkey = context.pubkey ? context.pubkey : randomBytes(32) user.pubKey = context.pubKey ? context.pubKey : randomBytes(32)
user.email = context.email ? context.email : faker.internet.email() user.email = context.email ? context.email : faker.internet.email()
user.firstName = context.firstName ? context.firstName : faker.name.firstName() user.firstName = context.firstName ? context.firstName : faker.name.firstName()
user.lastName = context.lastName ? context.lastName : faker.name.lastName() user.lastName = context.lastName ? context.lastName : faker.name.lastName()
user.username = context.username ? context.username : faker.internet.userName() user.username = context.username ? context.username : faker.internet.userName()
user.disabled = context.disabled ? context.disabled : false user.disabled = context.disabled ? context.disabled : false
user.groupId = 0 user.loginUserId = context.loginUserId ? context.loginUserId : randomInt(999999)
user.indexId = 0 user.indexId = 0
user.description = context.description ? context.description : faker.random.words(4)
// TODO Create real password and keys/hash
user.password = context.password ? context.password : BigInt(0)
user.privKey = context.privKey ? context.privKey : randomBytes(80)
user.emailHash = context.emailHash ? context.emailHash : randomBytes(32)
user.createdAt = context.createdAt ? context.createdAt : faker.date.recent()
user.emailChecked = context.emailChecked === undefined ? false : context.emailChecked
user.passphraseShown = context.passphraseShown ? context.passphraseShown : false
user.language = context.language ? context.language : 'en'
user.publisherId = context.publisherId ? context.publisherId : 0
return user return user
}) })

View File

@ -1,28 +1,19 @@
export interface UserContext { export interface UserContext {
pubkey?: Buffer loginUserId?: number
pubKey?: Buffer
email?: string email?: string
firstName?: string firstName?: string
lastName?: string lastName?: string
username?: string username?: string
disabled?: boolean disabled?: boolean
}
export interface LoginUserContext {
email?: string
firstName?: string
lastName?: string
username?: string
description?: string description?: string
password?: BigInt password?: BigInt
pubKey?: Buffer
privKey?: Buffer privKey?: Buffer
emailHash?: Buffer emailHash?: Buffer
createdAt?: Date createdAt?: Date
emailChecked?: boolean emailChecked?: boolean
passphraseShown?: boolean passphraseShown?: boolean
language?: string language?: string
disabled?: boolean
groupId?: number
publisherId?: number publisherId?: number
} }

View File

@ -1,5 +1,6 @@
export interface UserInterface { export interface UserInterface {
// from login user (contains state user) // from user
loginUserId?: number
email?: string email?: string
firstName?: string firstName?: string
lastName?: string lastName?: string

View File

@ -1,10 +1,4 @@
import { import { UserContext, LoginUserBackupContext, ServerUserContext } from '../../interface/UserContext'
UserContext,
LoginUserContext,
LoginUserBackupContext,
ServerUserContext,
LoginUserRolesContext,
} from '../../interface/UserContext'
import { import {
BalanceContext, BalanceContext,
TransactionContext, TransactionContext,
@ -13,7 +7,6 @@ import {
} from '../../interface/TransactionContext' } from '../../interface/TransactionContext'
import { UserInterface } from '../../interface/UserInterface' import { UserInterface } from '../../interface/UserInterface'
import { User } from '../../../entity/User' import { User } from '../../../entity/User'
import { LoginUser } from '../../../entity/LoginUser'
import { LoginUserBackup } from '../../../entity/LoginUserBackup' import { LoginUserBackup } from '../../../entity/LoginUserBackup'
import { ServerUser } from '../../../entity/ServerUser' import { ServerUser } from '../../../entity/ServerUser'
import { Balance } from '../../../entity/Balance' import { Balance } from '../../../entity/Balance'
@ -24,9 +17,9 @@ import { Factory } from 'typeorm-seeding'
export const userSeeder = async (factory: Factory, userData: UserInterface): Promise<void> => { export const userSeeder = async (factory: Factory, userData: UserInterface): Promise<void> => {
const user = await factory(User)(createUserContext(userData)).create() const user = await factory(User)(createUserContext(userData)).create()
if (!userData.email) userData.email = user.email await factory(LoginUserBackup)(
const loginUser = await factory(LoginUser)(createLoginUserContext(userData)).create() createLoginUserBackupContext(userData, (<User>user).loginUserId),
await factory(LoginUserBackup)(createLoginUserBackupContext(userData, loginUser)).create() ).create()
if (userData.isAdmin) { if (userData.isAdmin) {
await factory(ServerUser)(createServerUserContext(userData)).create() await factory(ServerUser)(createServerUserContext(userData)).create()
@ -49,44 +42,33 @@ export const userSeeder = async (factory: Factory, userData: UserInterface): Pro
const createUserContext = (context: UserInterface): UserContext => { const createUserContext = (context: UserInterface): UserContext => {
return { return {
pubkey: context.pubKey, pubKey: context.pubKey,
email: context.email, email: context.email,
loginUserId: context.loginUserId,
firstName: context.firstName, firstName: context.firstName,
lastName: context.lastName, lastName: context.lastName,
username: context.username, username: context.username,
disabled: context.disabled, disabled: context.disabled,
}
}
const createLoginUserContext = (context: UserInterface): LoginUserContext => {
return {
email: context.email,
firstName: context.firstName,
lastName: context.lastName,
username: context.username,
description: context.description, description: context.description,
password: context.password, password: context.password,
pubKey: context.pubKey,
privKey: context.privKey, privKey: context.privKey,
emailHash: context.emailHash, emailHash: context.emailHash,
createdAt: context.createdAt, createdAt: context.createdAt,
emailChecked: context.emailChecked, emailChecked: context.emailChecked,
passphraseShown: context.passphraseShown, passphraseShown: context.passphraseShown,
language: context.language, language: context.language,
disabled: context.disabled,
groupId: context.groupId,
publisherId: context.publisherId, publisherId: context.publisherId,
} }
} }
const createLoginUserBackupContext = ( const createLoginUserBackupContext = (
context: UserInterface, context: UserInterface,
loginUser: LoginUser, loginUserId: number,
): LoginUserBackupContext => { ): LoginUserBackupContext => {
return { return {
passphrase: context.passphrase, passphrase: context.passphrase,
mnemonicType: context.mnemonicType, mnemonicType: context.mnemonicType,
userId: loginUser.id, userId: loginUserId,
} }
} }
@ -103,13 +85,6 @@ const createServerUserContext = (context: UserInterface): ServerUserContext => {
} }
} }
const createLoginUserRolesContext = (loginUser: LoginUser): LoginUserRolesContext => {
return {
userId: loginUser.id,
roleId: 1,
}
}
const createBalanceContext = (context: UserInterface, user: User): BalanceContext => { const createBalanceContext = (context: UserInterface, user: User): BalanceContext => {
return { return {
modified: context.balanceModified, modified: context.balanceModified,

View File

@ -1,4 +1,5 @@
export const bibiBloxberg = { export const bibiBloxberg = {
loginUserId: 1,
email: 'bibi@bloxberg.de', email: 'bibi@bloxberg.de',
firstName: 'Bibi', firstName: 'Bibi',
lastName: 'Bloxberg', lastName: 'Bloxberg',

View File

@ -1,4 +1,5 @@
export const bobBaumeister = { export const bobBaumeister = {
loginUserId: 2,
email: 'bob@baumeister.de', email: 'bob@baumeister.de',
firstName: 'Bob', firstName: 'Bob',
lastName: 'der Baumeister', lastName: 'der Baumeister',

View File

@ -1,4 +1,5 @@
export const garrickOllivander = { export const garrickOllivander = {
loginUserId: 3,
email: 'garrick@ollivander.com', email: 'garrick@ollivander.com',
firstName: 'Garrick', firstName: 'Garrick',
lastName: 'Ollivander', lastName: 'Ollivander',

View File

@ -1,4 +1,5 @@
export const peterLustig = { export const peterLustig = {
loginUserId: 4,
email: 'peter@lustig.de', email: 'peter@lustig.de',
firstName: 'Peter', firstName: 'Peter',
lastName: 'Lustig', lastName: 'Lustig',

View File

@ -1,4 +1,5 @@
export const raeuberHotzenplotz = { export const raeuberHotzenplotz = {
loginUserId: 5,
email: 'raeuber@hotzenplotz.de', email: 'raeuber@hotzenplotz.de',
firstName: 'Räuber', firstName: 'Räuber',
lastName: 'Hotzenplotz', lastName: 'Hotzenplotz',