mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
refactor seed function
This commit is contained in:
parent
56e95718e1
commit
e2f8de0acb
@ -1,12 +1,7 @@
|
||||
import Faker from 'faker'
|
||||
import { define } from 'typeorm-seeding'
|
||||
import { LoginUserBackup } from '../../entity/LoginUserBackup'
|
||||
|
||||
interface LoginUserBackupContext {
|
||||
userId?: number
|
||||
passphrase?: string
|
||||
mnemonicType?: number
|
||||
}
|
||||
import { LoginUserBackupContext } from '../interface/UserContext'
|
||||
|
||||
define(LoginUserBackup, (faker: typeof Faker, context?: LoginUserBackupContext) => {
|
||||
if (!context) context = {}
|
||||
|
||||
@ -1,11 +1,7 @@
|
||||
import Faker from 'faker'
|
||||
import { define } from 'typeorm-seeding'
|
||||
import { LoginUserRoles } from '../../entity/LoginUserRoles'
|
||||
|
||||
interface LoginUserRolesContext {
|
||||
userId?: number
|
||||
roleId?: number
|
||||
}
|
||||
import { LoginUserRolesContext } from '../interface/UserContext'
|
||||
|
||||
define(LoginUserRoles, (faker: typeof Faker, context?: LoginUserRolesContext) => {
|
||||
if (!context) context = {}
|
||||
|
||||
@ -2,25 +2,7 @@ import Faker from 'faker'
|
||||
import { define } from 'typeorm-seeding'
|
||||
import { LoginUser } from '../../entity/LoginUser'
|
||||
import { randomBytes } from 'crypto'
|
||||
|
||||
interface LoginUserContext {
|
||||
email?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
username?: string
|
||||
description?: string
|
||||
password?: BigInt
|
||||
pubKey?: Buffer
|
||||
privKey?: Buffer
|
||||
emailHash?: Buffer
|
||||
createdAt?: Date
|
||||
emailChecked?: boolean
|
||||
passphraseShown?: boolean
|
||||
language?: string
|
||||
disabled?: boolean
|
||||
groupId?: number
|
||||
publisherId?: number
|
||||
}
|
||||
import { LoginUserContext } from '../interface/UserContext'
|
||||
|
||||
define(LoginUser, (faker: typeof Faker, context?: LoginUserContext) => {
|
||||
if (!context) context = {}
|
||||
|
||||
@ -1,17 +1,7 @@
|
||||
import Faker from 'faker'
|
||||
import { define } from 'typeorm-seeding'
|
||||
import { ServerUser } from '../../entity/ServerUser'
|
||||
|
||||
interface ServerUserContext {
|
||||
username?: string
|
||||
password?: string
|
||||
email?: string
|
||||
role?: string
|
||||
activated?: number
|
||||
lastLogin?: Date
|
||||
created?: Date
|
||||
modified?: Date
|
||||
}
|
||||
import { ServerUserContext } from '../interface/UserContext'
|
||||
|
||||
define(ServerUser, (faker: typeof Faker, context?: ServerUserContext) => {
|
||||
if (!context) context = {}
|
||||
|
||||
@ -2,15 +2,7 @@ import Faker from 'faker'
|
||||
import { define } from 'typeorm-seeding'
|
||||
import { User } from '../../entity/User'
|
||||
import { randomBytes } from 'crypto'
|
||||
|
||||
interface UserContext {
|
||||
pubkey?: Buffer
|
||||
email?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
username?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
import { UserContext } from '../interface/UserContext'
|
||||
|
||||
define(User, (faker: typeof Faker, context?: UserContext) => {
|
||||
if (!context) context = {}
|
||||
|
||||
@ -1,5 +1,13 @@
|
||||
export interface UserContext {
|
||||
// from login user (contains state user)
|
||||
pubkey?: Buffer
|
||||
email?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
username?: string
|
||||
disabled?: boolean
|
||||
}
|
||||
|
||||
export interface LoginUserContext {
|
||||
email?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
@ -15,15 +23,27 @@ export interface UserContext {
|
||||
language?: string
|
||||
disabled?: boolean
|
||||
groupId?: number
|
||||
publisherId?: number
|
||||
// from login user backup
|
||||
publisherId?: number | null
|
||||
}
|
||||
|
||||
export interface LoginUserBackupContext {
|
||||
userId?: number
|
||||
passphrase?: string
|
||||
mnemonicType?: number
|
||||
// from server user
|
||||
}
|
||||
|
||||
export interface ServerUserContext {
|
||||
username?: string
|
||||
password?: string
|
||||
email?: string
|
||||
role?: string
|
||||
activated?: number
|
||||
lastLogin?: Date
|
||||
created?: Date
|
||||
modified?: Date
|
||||
// flag for admin
|
||||
isAdmin?: boolean
|
||||
}
|
||||
|
||||
export interface LoginUserRolesContext {
|
||||
userId?: number
|
||||
roleId?: number
|
||||
}
|
||||
|
||||
30
database/src/interface/UserInterface.ts
Normal file
30
database/src/interface/UserInterface.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export interface UserInterface {
|
||||
// from login user (contains state user)
|
||||
email?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
username?: string
|
||||
description?: string
|
||||
password?: BigInt
|
||||
pubKey?: Buffer
|
||||
privKey?: Buffer
|
||||
emailHash?: Buffer
|
||||
createdAt?: Date
|
||||
emailChecked?: boolean
|
||||
passphraseShown?: boolean
|
||||
language?: string
|
||||
disabled?: boolean
|
||||
groupId?: number
|
||||
publisherId?: number | null
|
||||
// from login user backup
|
||||
passphrase?: string
|
||||
mnemonicType?: number
|
||||
// from server user
|
||||
serverUserPassword?: string
|
||||
role?: string
|
||||
activated?: number
|
||||
lastLogin?: Date
|
||||
modified?: Date
|
||||
// flag for admin
|
||||
isAdmin?: boolean
|
||||
}
|
||||
72
database/src/seeds/helpers/user-helpers.ts
Normal file
72
database/src/seeds/helpers/user-helpers.ts
Normal file
@ -0,0 +1,72 @@
|
||||
import {
|
||||
UserContext,
|
||||
LoginUserContext,
|
||||
LoginUserBackupContext,
|
||||
ServerUserContext,
|
||||
LoginUserRolesContext,
|
||||
} from '../../interface/UserContext'
|
||||
import { UserInterface } from '../../interface/UserInterface'
|
||||
import { LoginUser } from '../../../entity/LoginUser'
|
||||
|
||||
export const createUserContext = (context: UserInterface): UserContext => {
|
||||
return {
|
||||
pubkey: context.pubKey,
|
||||
email: context.email,
|
||||
firstName: context.firstName,
|
||||
lastName: context.lastName,
|
||||
username: context.username,
|
||||
disabled: context.disabled,
|
||||
}
|
||||
}
|
||||
|
||||
export const createLoginUserContext = (context: UserInterface): LoginUserContext => {
|
||||
return {
|
||||
email: context.email,
|
||||
firstName: context.firstName,
|
||||
lastName: context.lastName,
|
||||
username: context.username,
|
||||
description: context.description,
|
||||
password: context.password,
|
||||
pubKey: context.pubKey,
|
||||
privKey: context.privKey,
|
||||
emailHash: context.emailHash,
|
||||
createdAt: context.createdAt,
|
||||
emailChecked: context.emailChecked,
|
||||
passphraseShown: context.passphraseShown,
|
||||
language: context.language,
|
||||
disabled: context.disabled,
|
||||
groupId: context.groupId,
|
||||
publisherId: context.publisherId,
|
||||
}
|
||||
}
|
||||
|
||||
export const createLoginUserBackupContext = (
|
||||
context: UserInterface,
|
||||
loginUser: LoginUser,
|
||||
): LoginUserBackupContext => {
|
||||
return {
|
||||
passphrase: context.passphrase,
|
||||
mnemonicType: context.mnemonicType,
|
||||
userId: loginUser.id,
|
||||
}
|
||||
}
|
||||
|
||||
export const createServerUserContext = (context: UserInterface): ServerUserContext => {
|
||||
return {
|
||||
role: context.role,
|
||||
username: context.username,
|
||||
password: context.serverUserPassword,
|
||||
email: context.email,
|
||||
activated: context.activated,
|
||||
created: context.createdAt,
|
||||
lastLogin: context.lastLogin,
|
||||
modified: context.modified,
|
||||
}
|
||||
}
|
||||
|
||||
export const createLoginUserRolesContext = (loginUser: LoginUser): LoginUserRolesContext => {
|
||||
return {
|
||||
userId: loginUser.id,
|
||||
roleId: 1,
|
||||
}
|
||||
}
|
||||
@ -4,88 +4,29 @@ import { LoginUser } from '../../../entity/LoginUser'
|
||||
import { LoginUserBackup } from '../../../entity/LoginUserBackup'
|
||||
import { ServerUser } from '../../../entity/ServerUser'
|
||||
import { LoginUserRoles } from '../../../entity/LoginUserRoles'
|
||||
import {
|
||||
createUserContext,
|
||||
createLoginUserContext,
|
||||
createLoginUserBackupContext,
|
||||
createServerUserContext,
|
||||
createLoginUserRolesContext,
|
||||
} from '../helpers/user-helpers'
|
||||
import { peterLustig } from './peter-lustig'
|
||||
|
||||
const peterLustig = {
|
||||
email: 'peter@lustig.de',
|
||||
firstName: 'Peter',
|
||||
lastName: 'Lustig',
|
||||
username: 'peter',
|
||||
description: 'Latzhose und Nickelbrille',
|
||||
password: BigInt('3917921995996627700'),
|
||||
pubKey: Buffer.from('7281e0ee3258b08801f3ec73e431b4519677f65c03b0382c63a913b5784ee770', 'hex'),
|
||||
privKey: Buffer.from(
|
||||
'3c7c0253033212ed983f6bb10ce73362a99f0bd01d4d1b21ca702d532ca32710ba36abf72a22a963b9026e764e954f441f4905b87a66861bd6b9d9689b7f8aefea66cc493e21da4244e85be81660b9c4',
|
||||
'hex',
|
||||
),
|
||||
emailHash: Buffer.from('9f700e6f6ec351a140b674c0edd4479509697b023bd8bee8826915ef6c2af036', 'hex'),
|
||||
createdAt: new Date('2020-11-25T10:48:43'),
|
||||
emailChecked: true,
|
||||
passphraseShown: false,
|
||||
language: 'de',
|
||||
disabled: false,
|
||||
groupId: 1,
|
||||
publisherId: null,
|
||||
passphrase:
|
||||
'okay property choice naive calm present weird increase stuff royal vibrant frame attend wood one else tribe pull hedgehog woman kitchen hawk snack smart ',
|
||||
mnemonicType: 2,
|
||||
role: 'admin',
|
||||
serverUserPassword: '$2y$10$TzIWLeZoKs251gwrhSQmHeKhKI/EQ4EV5ClfAT8Ufnb4lcUXPa5X.',
|
||||
activated: 1,
|
||||
lastLogin: new Date('2021-10-27T12:25:57'),
|
||||
modified: new Date('2021-09-27T12:25:57'),
|
||||
isAdmin: true,
|
||||
}
|
||||
const userData = peterLustig
|
||||
|
||||
export class CreatePeterLustigSeed implements Seeder {
|
||||
public async run(factory: Factory): Promise<void> {
|
||||
await factory(User)({
|
||||
pubkey: peterLustig.pubKey,
|
||||
email: peterLustig.email,
|
||||
firstName: peterLustig.firstName,
|
||||
lastName: peterLustig.lastName,
|
||||
username: peterLustig.username,
|
||||
disabled: peterLustig.disabled,
|
||||
}).create()
|
||||
await factory(User)(createUserContext(userData)).create()
|
||||
const loginUser = await factory(LoginUser)(createLoginUserContext(userData)).create()
|
||||
await factory(LoginUserBackup)(createLoginUserBackupContext(userData, loginUser)).create()
|
||||
|
||||
const loginUser = await factory(LoginUser)({
|
||||
email: peterLustig.email,
|
||||
firstName: peterLustig.firstName,
|
||||
lastName: peterLustig.lastName,
|
||||
username: peterLustig.username,
|
||||
description: peterLustig.description,
|
||||
password: peterLustig.password,
|
||||
pubKey: peterLustig.pubKey,
|
||||
privKey: peterLustig.privKey,
|
||||
emailHash: peterLustig.emailHash,
|
||||
createdAt: peterLustig.createdAt,
|
||||
emailChecked: peterLustig.emailChecked,
|
||||
passphraseShown: peterLustig.passphraseShown,
|
||||
language: peterLustig.language,
|
||||
disabled: peterLustig.disabled,
|
||||
groupId: peterLustig.groupId,
|
||||
publisherId: peterLustig.publisherId,
|
||||
}).create()
|
||||
if (userData.isAdmin) {
|
||||
await factory(ServerUser)(createServerUserContext(userData)).create()
|
||||
|
||||
await factory(LoginUserBackup)({
|
||||
passphrase: peterLustig.passphrase,
|
||||
mnemonicType: peterLustig.mnemonicType,
|
||||
userId: loginUser.id,
|
||||
}).create()
|
||||
|
||||
await factory(ServerUser)({
|
||||
role: peterLustig.role,
|
||||
username: peterLustig.username,
|
||||
password: peterLustig.serverUserPassword,
|
||||
email: peterLustig.email,
|
||||
activated: peterLustig.activated,
|
||||
created: peterLustig.createdAt,
|
||||
lastLogin: peterLustig.lastLogin,
|
||||
modified: peterLustig.modified,
|
||||
}).create()
|
||||
|
||||
await factory(LoginUserRoles)({
|
||||
userId: loginUser.id,
|
||||
roleId: 1,
|
||||
}).create()
|
||||
// This is crazy: we just need the relation to roleId but no role at all
|
||||
// It works with LoginRoles empty!!
|
||||
await factory(LoginUserRoles)(createLoginUserRolesContext(loginUser)).create()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
database/src/seeds/users/peter-lustig.ts
Normal file
30
database/src/seeds/users/peter-lustig.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export const peterLustig = {
|
||||
email: 'peter@lustig.de',
|
||||
firstName: 'Peter',
|
||||
lastName: 'Lustig',
|
||||
username: 'peter',
|
||||
description: 'Latzhose und Nickelbrille',
|
||||
password: BigInt('3917921995996627700'),
|
||||
pubKey: Buffer.from('7281e0ee3258b08801f3ec73e431b4519677f65c03b0382c63a913b5784ee770', 'hex'),
|
||||
privKey: Buffer.from(
|
||||
'3c7c0253033212ed983f6bb10ce73362a99f0bd01d4d1b21ca702d532ca32710ba36abf72a22a963b9026e764e954f441f4905b87a66861bd6b9d9689b7f8aefea66cc493e21da4244e85be81660b9c4',
|
||||
'hex',
|
||||
),
|
||||
emailHash: Buffer.from('9f700e6f6ec351a140b674c0edd4479509697b023bd8bee8826915ef6c2af036', 'hex'),
|
||||
createdAt: new Date('2020-11-25T10:48:43'),
|
||||
emailChecked: true,
|
||||
passphraseShown: false,
|
||||
language: 'de',
|
||||
disabled: false,
|
||||
groupId: 1,
|
||||
publisherId: null,
|
||||
passphrase:
|
||||
'okay property choice naive calm present weird increase stuff royal vibrant frame attend wood one else tribe pull hedgehog woman kitchen hawk snack smart ',
|
||||
mnemonicType: 2,
|
||||
role: 'admin',
|
||||
serverUserPassword: '$2y$10$TzIWLeZoKs251gwrhSQmHeKhKI/EQ4EV5ClfAT8Ufnb4lcUXPa5X.',
|
||||
activated: 1,
|
||||
lastLogin: new Date('2021-10-27T12:25:57'),
|
||||
modified: new Date('2021-09-27T12:25:57'),
|
||||
isAdmin: true,
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user