change/correct relation user:user_role

This commit is contained in:
Claus-Peter Huebner 2023-06-22 01:27:45 +02:00
parent 44687e6fc3
commit a3b60faa50
6 changed files with 41 additions and 15 deletions

View File

@ -12,7 +12,7 @@ Decimal.set({
})
const constants = {
DB_VERSION: '0066-x-community-sendcoins-transactions_table',
DB_VERSION: '0067-add_user_roles_table',
DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0
LOG4JS_CONFIG: 'log4js-config.json',
// default log level on production should be info

View File

@ -62,6 +62,8 @@ import { peterLustig } from '@/seeds/users/peter-lustig'
import { stephenHawking } from '@/seeds/users/stephen-hawking'
import { printTimeDuration } from '@/util/time'
import { objectValuesToArray } from '@/util/utilities'
import { UserRole } from '@entity/UserRole'
import { ROLE_NAMES } from '@/auth/ROLES'
jest.mock('@/emails/sendEmailVariants', () => {
const originalModule = jest.requireActual('@/emails/sendEmailVariants')
@ -162,7 +164,7 @@ describe('UserResolver', () => {
createdAt: expect.any(Date),
// emailChecked: false,
language: 'de',
isAdmin: null,
userRole: null,
deletedAt: null,
publisherId: 1234,
referrerId: null,
@ -334,8 +336,17 @@ describe('UserResolver', () => {
})
// make Peter Lustig Admin
const peter = await User.findOneOrFail({ id: user[0].id })
peter.isAdmin = new Date()
const peter = await User.findOneOrFail({
where: { id: user[0].id },
relations: ['userRole'],
})
if (peter.userRole == null) {
peter.userRole = UserRole.create()
}
peter.userRole.createdAt = new Date()
peter.userRole.role = ROLE_NAMES.ROLE_NAME_ADMIN
peter.userRole.userId = peter.id
await peter.userRole.save()
await peter.save()
// date statement
@ -679,7 +690,7 @@ describe('UserResolver', () => {
firstName: 'Bibi',
hasElopage: false,
id: expect.any(Number),
isAdmin: null,
userRole: null,
klickTipp: {
newsletterState: false,
},
@ -936,7 +947,7 @@ describe('UserResolver', () => {
beforeAll(async () => {
await mutate({ mutation: login, variables })
user = await User.find()
user = await User.find({ relations: ['userRole'] })
})
afterAll(() => {
@ -956,7 +967,7 @@ describe('UserResolver', () => {
},
hasElopage: false,
publisherId: 1234,
isAdmin: null,
userRole: null,
},
},
}),
@ -1476,7 +1487,7 @@ describe('UserResolver', () => {
firstName: 'Bibi',
hasElopage: false,
id: expect.any(Number),
isAdmin: null,
userRole: null,
klickTipp: {
newsletterState: false,
},
@ -1594,7 +1605,7 @@ describe('UserResolver', () => {
{ email: 'bibi@bloxberg.de' },
{ relations: ['user'] },
)
const adminConatct = await UserContact.findOneOrFail(
const adminContact = await UserContact.findOneOrFail(
{ email: 'peter@lustig.de' },
{ relations: ['user'] },
)
@ -1602,7 +1613,7 @@ describe('UserResolver', () => {
expect.objectContaining({
type: EventType.ADMIN_USER_ROLE_SET,
affectedUserId: userConatct.user.id,
actingUserId: adminConatct.user.id,
actingUserId: adminContact.user.id,
}),
)
})

View File

@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/unbound-method */
import { User } from '@entity/User'
import { UserRole } from '@entity/UserRole'
import { ApolloServerTestClient } from 'apollo-server-testing'
import { ROLE_NAMES } from '@/auth/ROLES'
import { createUser, setPassword } from '@/seeds/graphql/mutations'
import { UserInterface } from '@/seeds/users/UserInterface'
@ -19,7 +21,7 @@ export const userFactory = async (
} = await mutate({ mutation: createUser, variables: user })
// console.log('creatUser:', { id }, { user })
// get user from database
let dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact'] })
let dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact', 'userRole'] })
// console.log('dbUser:', dbUser)
const emailContact = dbUser.emailContact
@ -38,7 +40,13 @@ export const userFactory = async (
if (user.createdAt || user.deletedAt || user.isAdmin) {
if (user.createdAt) dbUser.createdAt = user.createdAt
if (user.deletedAt) dbUser.deletedAt = user.deletedAt
if (user.isAdmin) dbUser.isAdmin = new Date()
if (user.isAdmin) {
dbUser.userRole = UserRole.create()
dbUser.userRole.createdAt = new Date()
dbUser.userRole.role = ROLE_NAMES.ROLE_NAME_ADMIN
dbUser.userRole.userId = dbUser.id
await dbUser.userRole.save()
}
await dbUser.save()
}

View File

@ -87,8 +87,8 @@ export class User extends BaseEntity {
@Column({ type: 'bool', default: false })
hideAmountGDT: boolean
@OneToOne(() => UserRole, (role: UserRole) => role.userId)
@JoinColumn({ name: 'user_id' })
@OneToOne(() => UserRole, (userRole) => userRole.userId)
@JoinColumn({ name: 'id' })
userRole?: UserRole
@Column({ name: 'referrer_id', type: 'int', unsigned: true, nullable: true, default: null })

View File

@ -1,4 +1,5 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn } from 'typeorm'
import { User } from '../User'
@Entity('user_roles', { engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' })
export class UserRole extends BaseEntity {
@ -16,4 +17,8 @@ export class UserRole extends BaseEntity {
@Column({ name: 'updated_at', nullable: true, default: null, type: 'datetime' })
updatedAt: Date | null
@OneToOne(() => User, (user) => user.id)
@JoinColumn({ name: 'user_id' })
userRole?: UserRole
}

View File

@ -11,6 +11,7 @@ import { Event } from './Event'
import { ContributionMessage } from './ContributionMessage'
import { Community } from './Community'
import { FederatedCommunity } from './FederatedCommunity'
import { UserRole } from './UserRole'
export const entities = [
Community,
@ -26,4 +27,5 @@ export const entities = [
TransactionLink,
User,
UserContact,
UserRole,
]