From a3b60faa505f2453e1d79d64044cc4db73a0daba Mon Sep 17 00:00:00 2001 From: Claus-Peter Huebner Date: Thu, 22 Jun 2023 01:27:45 +0200 Subject: [PATCH] change/correct relation user:user_role --- backend/src/config/index.ts | 2 +- .../src/graphql/resolver/UserResolver.test.ts | 29 +++++++++++++------ backend/src/seeds/factory/user.ts | 12 ++++++-- .../entity/0067-add_user_roles_table/User.ts | 4 +-- .../0067-add_user_roles_table/UserRole.ts | 7 ++++- database/entity/index.ts | 2 ++ 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 8d1ed8ae6..60da283f3 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -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 diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 25787490f..7d8a57942 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -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, }), ) }) diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index 13a274911..06271eccd 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -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() } diff --git a/database/entity/0067-add_user_roles_table/User.ts b/database/entity/0067-add_user_roles_table/User.ts index f53b397a1..07c723683 100644 --- a/database/entity/0067-add_user_roles_table/User.ts +++ b/database/entity/0067-add_user_roles_table/User.ts @@ -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 }) diff --git a/database/entity/0067-add_user_roles_table/UserRole.ts b/database/entity/0067-add_user_roles_table/UserRole.ts index eb68b6e85..eeca3faa1 100644 --- a/database/entity/0067-add_user_roles_table/UserRole.ts +++ b/database/entity/0067-add_user_roles_table/UserRole.ts @@ -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 } diff --git a/database/entity/index.ts b/database/entity/index.ts index d44029754..b27ac4d61 100644 --- a/database/entity/index.ts +++ b/database/entity/index.ts @@ -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, ]