mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Add balance for seeded users
This commit is contained in:
parent
7282330528
commit
4717b0ba4c
@ -19,6 +19,6 @@ export class Balance extends BaseEntity {
|
||||
amount: number
|
||||
|
||||
@OneToOne(() => User, { nullable: false })
|
||||
@JoinColumn({ name: 'user_id' })
|
||||
@JoinColumn({ name: 'state_user_id' })
|
||||
user: User
|
||||
}
|
||||
|
||||
18
database/src/factories/balance.factory.ts
Normal file
18
database/src/factories/balance.factory.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import Faker from 'faker'
|
||||
import { define } from 'typeorm-seeding'
|
||||
import { Balance } from '../../entity/Balance'
|
||||
import { BalanceContext } from '../interface/TransactionContext'
|
||||
|
||||
define(Balance, (faker: typeof Faker, context?: BalanceContext) => {
|
||||
if (!context || !context.user) {
|
||||
throw new Error('Balance: No user present!')
|
||||
}
|
||||
|
||||
const balance = new Balance()
|
||||
balance.modified = context.modified ? context.modified : faker.date.recent()
|
||||
balance.recordDate = context.recordDate ? context.recordDate : faker.date.recent()
|
||||
balance.amount = context.amount ? context.amount : 10000000
|
||||
balance.user = context.user
|
||||
|
||||
return balance
|
||||
})
|
||||
@ -60,11 +60,11 @@ const run = async (command: string) => {
|
||||
root: process.cwd(),
|
||||
configName: 'ormconfig.js',
|
||||
})
|
||||
await runSeeder(DecayStartBlockSeed)
|
||||
await runSeeder(CreatePeterLustigSeed)
|
||||
await runSeeder(CreateBibiBloxbergSeed)
|
||||
await runSeeder(CreateRaeuberHotzenplotzSeed)
|
||||
await runSeeder(CreateBobBaumeisterSeed)
|
||||
await runSeeder(DecayStartBlockSeed)
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unsupported command ${command}`)
|
||||
|
||||
@ -27,4 +27,10 @@ export interface UserInterface {
|
||||
modified?: Date
|
||||
// flag for admin
|
||||
isAdmin?: boolean
|
||||
// flag for balance
|
||||
addBalance?: boolean
|
||||
// balance
|
||||
balanceModified?: Date
|
||||
recordDate?: Date
|
||||
amount?: number
|
||||
}
|
||||
|
||||
@ -5,16 +5,18 @@ import {
|
||||
ServerUserContext,
|
||||
LoginUserRolesContext,
|
||||
} from '../../interface/UserContext'
|
||||
import { BalanceContext } from '../../interface/TransactionContext'
|
||||
import { UserInterface } from '../../interface/UserInterface'
|
||||
import { User } from '../../../entity/User'
|
||||
import { LoginUser } from '../../../entity/LoginUser'
|
||||
import { LoginUserBackup } from '../../../entity/LoginUserBackup'
|
||||
import { ServerUser } from '../../../entity/ServerUser'
|
||||
import { LoginUserRoles } from '../../../entity/LoginUserRoles'
|
||||
import { Balance } from '../../../entity/Balance'
|
||||
import { Factory } from 'typeorm-seeding'
|
||||
|
||||
export const userSeeder = async (factory: Factory, userData: UserInterface): Promise<void> => {
|
||||
await factory(User)(createUserContext(userData)).create()
|
||||
const user = await factory(User)(createUserContext(userData)).create()
|
||||
const loginUser = await factory(LoginUser)(createLoginUserContext(userData)).create()
|
||||
await factory(LoginUserBackup)(createLoginUserBackupContext(userData, loginUser)).create()
|
||||
|
||||
@ -25,9 +27,13 @@ export const userSeeder = async (factory: Factory, userData: UserInterface): Pro
|
||||
// It works with LoginRoles empty!!
|
||||
await factory(LoginUserRoles)(createLoginUserRolesContext(loginUser)).create()
|
||||
}
|
||||
|
||||
if (userData.addBalance) {
|
||||
await factory(Balance)(createBalanceContext(userData, user)).create()
|
||||
}
|
||||
}
|
||||
|
||||
export const createUserContext = (context: UserInterface): UserContext => {
|
||||
const createUserContext = (context: UserInterface): UserContext => {
|
||||
return {
|
||||
pubkey: context.pubKey,
|
||||
email: context.email,
|
||||
@ -38,7 +44,7 @@ export const createUserContext = (context: UserInterface): UserContext => {
|
||||
}
|
||||
}
|
||||
|
||||
export const createLoginUserContext = (context: UserInterface): LoginUserContext => {
|
||||
const createLoginUserContext = (context: UserInterface): LoginUserContext => {
|
||||
return {
|
||||
email: context.email,
|
||||
firstName: context.firstName,
|
||||
@ -59,7 +65,7 @@ export const createLoginUserContext = (context: UserInterface): LoginUserContext
|
||||
}
|
||||
}
|
||||
|
||||
export const createLoginUserBackupContext = (
|
||||
const createLoginUserBackupContext = (
|
||||
context: UserInterface,
|
||||
loginUser: LoginUser,
|
||||
): LoginUserBackupContext => {
|
||||
@ -70,7 +76,7 @@ export const createLoginUserBackupContext = (
|
||||
}
|
||||
}
|
||||
|
||||
export const createServerUserContext = (context: UserInterface): ServerUserContext => {
|
||||
const createServerUserContext = (context: UserInterface): ServerUserContext => {
|
||||
return {
|
||||
role: context.role,
|
||||
username: context.username,
|
||||
@ -83,9 +89,18 @@ export const createServerUserContext = (context: UserInterface): ServerUserConte
|
||||
}
|
||||
}
|
||||
|
||||
export const createLoginUserRolesContext = (loginUser: LoginUser): LoginUserRolesContext => {
|
||||
const createLoginUserRolesContext = (loginUser: LoginUser): LoginUserRolesContext => {
|
||||
return {
|
||||
userId: loginUser.id,
|
||||
roleId: 1,
|
||||
}
|
||||
}
|
||||
|
||||
const createBalanceContext = (context: UserInterface, user: User): BalanceContext => {
|
||||
return {
|
||||
modified: context.balanceModified,
|
||||
recordDate: context.recordDate,
|
||||
amount: context.amount,
|
||||
user,
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,4 +22,8 @@ export const bibiBloxberg = {
|
||||
'knife normal level all hurdle crucial color avoid warrior stadium road bachelor affair topple hawk pottery right afford immune two ceiling budget glance hour ',
|
||||
mnemonicType: 2,
|
||||
isAdmin: false,
|
||||
addBalance: true,
|
||||
balanceModified: new Date('2021-11-30T10:37:11'),
|
||||
recordDate: new Date('2021-11-30T10:37:11'),
|
||||
amount: 10000000,
|
||||
}
|
||||
|
||||
@ -22,4 +22,8 @@ export const bobBaumeister = {
|
||||
'detail master source effort unable waste tilt flush domain orchard art truck hint barrel response gate impose peanut secret merry three uncle wink resource ',
|
||||
mnemonicType: 2,
|
||||
isAdmin: false,
|
||||
addBalance: true,
|
||||
balanceModified: new Date('2021-11-30T10:37:14'),
|
||||
recordDate: new Date('2021-11-30T10:37:14'),
|
||||
amount: 10000000,
|
||||
}
|
||||
|
||||
@ -22,4 +22,8 @@ export const raeuberHotzenplotz = {
|
||||
'gospel trip tenant mouse spider skill auto curious man video chief response same little over expire drum display fancy clinic keen throw urge basket ',
|
||||
mnemonicType: 2,
|
||||
isAdmin: false,
|
||||
addBalance: true,
|
||||
balanceModified: new Date('2021-11-30T10:37:13'),
|
||||
recordDate: new Date('2021-11-30T10:37:13'),
|
||||
amount: 10000000,
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user