diff --git a/database/src/enum/RoleNames.ts b/database/src/enum/RoleNames.ts new file mode 100644 index 000000000..dda8eaa8a --- /dev/null +++ b/database/src/enum/RoleNames.ts @@ -0,0 +1,8 @@ +export enum RoleNames { + UNAUTHORIZED = 'UNAUTHORIZED', + USER = 'USER', + MODERATOR = 'MODERATOR', + MODERATOR_AI = 'MODERATOR_AI', + ADMIN = 'ADMIN', + DLT_CONNECTOR = 'DLT_CONNECTOR_ROLE', +} \ No newline at end of file diff --git a/database/src/enum/index.ts b/database/src/enum/index.ts index ed629db22..df243acf3 100644 --- a/database/src/enum/index.ts +++ b/database/src/enum/index.ts @@ -2,4 +2,5 @@ export * from './CommunityHandshakeStateType' export * from './ContributionType' export * from './ContributionStatus' export * from './TransactionTypeId' -export * from './ContributionCycleType' \ No newline at end of file +export * from './ContributionCycleType' +export * from './RoleNames' \ No newline at end of file diff --git a/database/src/seeds/factory/user.ts b/database/src/seeds/factory/user.ts index fb1ab852e..bebd07cf0 100644 --- a/database/src/seeds/factory/user.ts +++ b/database/src/seeds/factory/user.ts @@ -1,11 +1,12 @@ import { UserInterface } from '../users/UserInterface' -import { User, UserContact } from '../../entity' +import { User, UserContact, UserRole } from '../../entity' import { v4 } from 'uuid' import { UserContactType, OptInType, PasswordEncryptionType } from 'shared' import { getHomeCommunity } from '../../queries/communities' import random from 'crypto-random-bigint' import { Community } from '../../entity' import { AppDatabase } from '../..' +import { RoleNames } from '../../enum/RoleNames' export async function userFactory(user: UserInterface, homeCommunity?: Community | null): Promise { // TODO: improve with cascade @@ -15,6 +16,11 @@ export async function userFactory(user: UserInterface, homeCommunity?: Community dbUser.emailId = dbUserContact.id dbUser.emailContact = dbUserContact dbUser = await dbUser.save() + + const userRole = user.role as RoleNames + if (userRole && (userRole === RoleNames.ADMIN || userRole === RoleNames.MODERATOR)) { + await createUserRole(dbUser.id, userRole) + } return dbUser } @@ -23,6 +29,7 @@ export async function userFactory(user: UserInterface, homeCommunity?: Community export async function userFactoryBulk(users: UserInterface[], homeCommunity?: Community | null): Promise { const dbUsers: User[] = [] const dbUserContacts: UserContact[] = [] + const dbUserRoles: UserRole[] = [] const lastUser = await User.findOne({ order: { id: 'DESC' }, select: ['id'], where: {} }) const lastUserContact = await UserContact.findOne({ order: { id: 'DESC' }, select: ['id'], where: {} }) let userId = lastUser ? lastUser.id + 1 : 1 @@ -40,6 +47,11 @@ export async function userFactoryBulk(users: UserInterface[], homeCommunity?: Co dbUsers.push(dbUser) dbUserContacts.push(dbUserContact) + + const userRole = user.role as RoleNames + if (userRole && (userRole === RoleNames.ADMIN || userRole === RoleNames.MODERATOR)) { + dbUserRoles.push(await createUserRole(dbUser.id, userRole, false)) + } userId++ emailId++ @@ -50,9 +62,11 @@ export async function userFactoryBulk(users: UserInterface[], homeCommunity?: Co // because of manuel id assignment const dbUsersCopy = dbUsers.map(user => ({ ...user })) const dbUserContactsCopy = dbUserContacts.map(userContact => ({ ...userContact })) + const dbUserRolesCopy = dbUserRoles.map(userRole => ({ ...userRole })) await Promise.all([ transaction.getRepository(User).insert(dbUsersCopy), - transaction.getRepository(UserContact).insert(dbUserContactsCopy) + transaction.getRepository(UserContact).insert(dbUserContactsCopy), + transaction.getRepository(UserRole).insert(dbUserRolesCopy) ]) }) return dbUsers @@ -107,4 +121,11 @@ export async function createUserContact(user: UserInterface, userId?: number, st } return store ? dbUserContact.save() : dbUserContact +} + +export async function createUserRole(userId: number, role: RoleNames, store: boolean = true): Promise { + let dbUserRole = new UserRole() + dbUserRole.userId = userId + dbUserRole.role = role + return store ? dbUserRole.save() : dbUserRole } \ No newline at end of file diff --git a/database/src/seeds/index.ts b/database/src/seeds/index.ts index 67229133e..4da2c28e3 100644 --- a/database/src/seeds/index.ts +++ b/database/src/seeds/index.ts @@ -83,6 +83,7 @@ async function clearDatabase() { await trx.query(`TRUNCATE TABLE contribution_links`) await trx.query(`TRUNCATE TABLE users`) await trx.query(`TRUNCATE TABLE user_contacts`) + await trx.query(`TRUNCATE TABLE user_roles`) await trx.query(`TRUNCATE TABLE transactions`) await trx.query(`TRUNCATE TABLE transaction_links`) await trx.query(`TRUNCATE TABLE communities`) diff --git a/database/src/seeds/users/peter-lustig.ts b/database/src/seeds/users/peter-lustig.ts index 58b07fe99..e20bac5fe 100644 --- a/database/src/seeds/users/peter-lustig.ts +++ b/database/src/seeds/users/peter-lustig.ts @@ -1,4 +1,5 @@ import { UserInterface } from './UserInterface' +import { RoleNames } from '../../enum' export const peterLustig: UserInterface = { email: 'peter@lustig.de', @@ -7,5 +8,6 @@ export const peterLustig: UserInterface = { // description: 'Latzhose und Nickelbrille', createdAt: new Date('2020-11-25T10:48:43'), emailChecked: true, - language: 'de' + language: 'de', + role: RoleNames.ADMIN, }