diff --git a/backend/src/graphql/resolver/ContributionResolver.test.ts b/backend/src/graphql/resolver/ContributionResolver.test.ts index 31d405942..399dc7e82 100644 --- a/backend/src/graphql/resolver/ContributionResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionResolver.test.ts @@ -2139,14 +2139,6 @@ describe('ContributionResolver', () => { }) }) - it('stores the EMAIL_CONFIRMATION event in the database', async () => { - await expect(DbEvent.find()).resolves.toContainEqual( - expect.objectContaining({ - type: EventType.EMAIL_CONFIRMATION, - }), - ) - }) - describe('confirm same contribution again', () => { it('throws an error', async () => { jest.clearAllMocks() diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 0fd06c0d1..b64b66370 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -67,7 +67,7 @@ import { printTimeDuration } from '@/util/time' import { objectValuesToArray } from 'core' import { LOG4JS_BASE_CATEGORY_NAME } from '@/config/const' -import { getLogger } from 'config-schema/test/testSetup' +import { clearLogs, getLogger, printLogs } from 'config-schema/test/testSetup' import { Location2Point } from './util/Location2Point' jest.mock('@/apis/humhub/HumHubClient') @@ -738,7 +738,7 @@ describe('UserResolver', () => { hasElopage: false, hideAmountGDD: false, hideAmountGDT: false, - humhubAllowed: true, + humhubAllowed: false, humhubPublishName: 'PUBLISH_NAME_ALIAS_OR_INITALS', klickTipp: { newsletterState: false, @@ -1610,7 +1610,7 @@ describe('UserResolver', () => { hasElopage: false, hideAmountGDD: false, hideAmountGDT: false, - humhubAllowed: true, + humhubAllowed: false, humhubPublishName: 'PUBLISH_NAME_ALIAS_OR_INITALS', klickTipp: { newsletterState: false, @@ -2256,7 +2256,7 @@ describe('UserResolver', () => { relations: ['user'], }) const activationLink = `${ - CONFIG.EMAIL_LINK_VERIFICATION + CONFIG.EMAIL_LINK_SETPASSWORD }${userContact.emailVerificationCode.toString()}` expect(sendAccountActivationEmail).toBeCalledWith({ firstName: 'Bibi', diff --git a/backend/src/seeds/community/index.ts b/backend/src/seeds/community/index.ts index d51eafc31..0d3b44d88 100644 --- a/backend/src/seeds/community/index.ts +++ b/backend/src/seeds/community/index.ts @@ -1,27 +1,19 @@ import { Community as DbCommunity } from 'database' -import { v4 as uuidv4 } from 'uuid' import { CONFIG } from '@/config' + export async function writeHomeCommunityEntry(): Promise { try { // check for existing homeCommunity entry let homeCom = await DbCommunity.findOne({ where: { foreign: false } }) - if (homeCom) { - // simply update the existing entry, but it MUST keep the ID and UUID because of possible relations - homeCom.publicKey = Buffer.from('public-key-data-seeding') // keyPair.publicKey - // homeCom.privateKey = keyPair.secretKey - homeCom.url = 'http://localhost/api/' - homeCom.name = CONFIG.COMMUNITY_NAME - homeCom.description = CONFIG.COMMUNITY_DESCRIPTION - await DbCommunity.save(homeCom) - } else { + if (!homeCom) { // insert a new homecommunity entry including a new ID and a new but ensured unique UUID homeCom = new DbCommunity() homeCom.foreign = false homeCom.publicKey = Buffer.from('public-key-data-seeding') // keyPair.publicKey // homeCom.privateKey = keyPair.secretKey - homeCom.communityUuid = uuidv4() // await newCommunityUuid() + homeCom.communityUuid = 'beac216d-73ae-427f-9678-0209af4936ce' // await newCommunityUuid() homeCom.url = 'http://localhost/api/' homeCom.name = CONFIG.COMMUNITY_NAME homeCom.description = CONFIG.COMMUNITY_DESCRIPTION diff --git a/backend/src/seeds/contributionLink/ContributionLinkInterface.ts b/backend/src/seeds/contributionLink/ContributionLinkInterface.ts index 15ba4b72d..ce0a60695 100644 --- a/backend/src/seeds/contributionLink/ContributionLinkInterface.ts +++ b/backend/src/seeds/contributionLink/ContributionLinkInterface.ts @@ -1,3 +1,6 @@ +export { ContributionLinkInterface } from 'database' + +/* export interface ContributionLinkInterface { amount: number name: string @@ -5,3 +8,4 @@ export interface ContributionLinkInterface { validFrom?: Date validTo?: Date } +*/ diff --git a/backend/src/seeds/contributionLink/index.ts b/backend/src/seeds/contributionLink/index.ts index 41d28eb60..5be34e171 100644 --- a/backend/src/seeds/contributionLink/index.ts +++ b/backend/src/seeds/contributionLink/index.ts @@ -1,5 +1,6 @@ -import { ContributionLinkInterface } from './ContributionLinkInterface' +export { contributionLinks } from 'database' +/* export const contributionLinks: ContributionLinkInterface[] = [ { name: 'Dokumenta 2017', @@ -16,3 +17,4 @@ export const contributionLinks: ContributionLinkInterface[] = [ validTo: new Date(2022, 8, 25), }, ] +*/ \ No newline at end of file diff --git a/backend/src/seeds/creation/CreationInterface.ts b/backend/src/seeds/creation/CreationInterface.ts index ee450bd93..a05276a3d 100644 --- a/backend/src/seeds/creation/CreationInterface.ts +++ b/backend/src/seeds/creation/CreationInterface.ts @@ -1,3 +1,5 @@ +export { CreationInterface } from 'database' +/* export interface CreationInterface { email: string amount: number @@ -7,3 +9,4 @@ export interface CreationInterface { // number of months to move the confirmed creation to the past moveCreationDate?: number } +*/ \ No newline at end of file diff --git a/backend/src/seeds/creation/index.ts b/backend/src/seeds/creation/index.ts index 1499f663b..f95bc6fa4 100644 --- a/backend/src/seeds/creation/index.ts +++ b/backend/src/seeds/creation/index.ts @@ -1,3 +1,7 @@ + +export { creations } from 'database' + +/* import { nMonthsBefore } from '@/seeds/factory/creation' import { CreationInterface } from './CreationInterface' @@ -153,3 +157,4 @@ export const creations: CreationInterface[] = [ confirmed: true, }, ] +*/ \ No newline at end of file diff --git a/backend/src/seeds/factory/contributionLink.ts b/backend/src/seeds/factory/contributionLink.ts index 4e8c945f3..e29d7a635 100644 --- a/backend/src/seeds/factory/contributionLink.ts +++ b/backend/src/seeds/factory/contributionLink.ts @@ -1,32 +1,15 @@ -import { ApolloServerTestClient } from 'apollo-server-testing' +import { + contributionLinkFactory as contributionLinkFactoryDb, + ContributionLinkInterface +} from 'database' import { ContributionLink } from '@model/ContributionLink' -import { ContributionLinkInterface } from '@/seeds/contributionLink/ContributionLinkInterface' -import { createContributionLink, login } from '@/seeds/graphql/mutations' +export { ContributionLinkInterface } -export const contributionLinkFactory = async ( - client: ApolloServerTestClient, +export async function contributionLinkFactory ( + _client: any, contributionLink: ContributionLinkInterface, -): Promise => { - const { mutate } = client - - // login as admin - await mutate({ - mutation: login, - variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, - }) - const variables = { - amount: contributionLink.amount, - memo: contributionLink.memo, - name: contributionLink.name, - cycle: 'ONCE', - maxPerCycle: 1, - maxAmountPerMonth: 200, - validFrom: contributionLink.validFrom ? contributionLink.validFrom.toISOString() : undefined, - validTo: contributionLink.validTo ? contributionLink.validTo.toISOString() : undefined, - } - - const result = await mutate({ mutation: createContributionLink, variables }) - return result.data.createContributionLink +): Promise { + return new ContributionLink(await contributionLinkFactoryDb(contributionLink)) } diff --git a/backend/src/seeds/factory/creation.ts b/backend/src/seeds/factory/creation.ts index 9ce36bfa9..2d57dc782 100644 --- a/backend/src/seeds/factory/creation.ts +++ b/backend/src/seeds/factory/creation.ts @@ -1,58 +1,15 @@ -import { ApolloServerTestClient } from 'apollo-server-testing' -import { Contribution, Transaction } from 'database' +import { + Contribution, + creationFactory as creationFactoryDb, + CreationInterface, + nMonthsBefore +} from 'database' -import { findUserByEmail } from '@/graphql/resolver/UserResolver' -import { CreationInterface } from '@/seeds/creation/CreationInterface' -import { confirmContribution, createContribution, login } from '@/seeds/graphql/mutations' - -export const nMonthsBefore = (date: Date, months = 1): string => { - return new Date(date.getFullYear(), date.getMonth() - months, 1).toISOString() -} +export { CreationInterface, nMonthsBefore } export const creationFactory = async ( - client: ApolloServerTestClient, + _client: any, creation: CreationInterface, ): Promise => { - const { mutate } = client - await mutate({ - mutation: login, - variables: { email: creation.email, password: 'Aa12345_' }, - }) - const { - data: { createContribution: contribution }, - } = await mutate({ mutation: createContribution, variables: { ...creation } }) - - if (creation.confirmed) { - const user = await findUserByEmail(creation.email) // userContact.user - - await mutate({ mutation: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } }) - await mutate({ mutation: confirmContribution, variables: { id: contribution.id } }) - const confirmedContribution = await Contribution.findOneOrFail({ - where: { id: contribution.id }, - }) - - if (creation.moveCreationDate) { - const transaction = await Transaction.findOneOrFail({ - where: { userId: user.id, creationDate: new Date(creation.contributionDate) }, - order: { balanceDate: 'DESC' }, - }) - - if (transaction.decay.equals(0) && transaction.creationDate) { - confirmedContribution.contributionDate = new Date( - nMonthsBefore(transaction.creationDate, creation.moveCreationDate), - ) - transaction.creationDate = new Date( - nMonthsBefore(transaction.creationDate, creation.moveCreationDate), - ) - transaction.balanceDate = new Date( - nMonthsBefore(transaction.balanceDate, creation.moveCreationDate), - ) - await transaction.save() - await confirmedContribution.save() - } - } - return confirmedContribution - } else { - return contribution - } + return creationFactoryDb(creation) } diff --git a/backend/src/seeds/factory/transactionLink.ts b/backend/src/seeds/factory/transactionLink.ts index 80384025f..02911bd32 100644 --- a/backend/src/seeds/factory/transactionLink.ts +++ b/backend/src/seeds/factory/transactionLink.ts @@ -1,46 +1,13 @@ -import { ApolloServerTestClient } from 'apollo-server-testing' -import { TransactionLink } from 'database' +import { + transactionLinkFactory as transactionLinkFactoryDb, + TransactionLinkInterface +} from 'database' -import { transactionLinkExpireDate } from '@/graphql/resolver/TransactionLinkResolver' -import { createTransactionLink, login } from '@/seeds/graphql/mutations' -import { TransactionLinkInterface } from '@/seeds/transactionLink/TransactionLinkInterface' +export { TransactionLinkInterface } -export const transactionLinkFactory = async ( - client: ApolloServerTestClient, +export async function transactionLinkFactory ( + _client: any, transactionLink: TransactionLinkInterface, -): Promise => { - const { mutate } = client - - // login - await mutate({ - mutation: login, - variables: { email: transactionLink.email, password: 'Aa12345_' }, - }) - - const variables = { - amount: transactionLink.amount, - memo: transactionLink.memo, - } - - // get the transaction links's id - const { - data: { - createTransactionLink: { id }, - }, - } = await mutate({ mutation: createTransactionLink, variables }) - - if (transactionLink.createdAt || transactionLink.deletedAt) { - const dbTransactionLink = await TransactionLink.findOneOrFail({ where: { id } }) - - if (transactionLink.createdAt) { - dbTransactionLink.createdAt = transactionLink.createdAt - dbTransactionLink.validUntil = transactionLinkExpireDate(transactionLink.createdAt) - await dbTransactionLink.save() - } - - if (transactionLink.deletedAt) { - dbTransactionLink.deletedAt = new Date(dbTransactionLink.createdAt.getTime() + 1000) - await dbTransactionLink.save() - } - } +): Promise { + await transactionLinkFactoryDb(transactionLink) } diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index 36d9adfeb..f174df39a 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -1,77 +1,20 @@ -import { ApolloServerTestClient } from 'apollo-server-testing' -import { User } from 'database' +import { User, userFactory as userFactoryDb } from 'database' -import { RoleNames } from '@enum/RoleNames' - -import { setUserRole } from '@/graphql/resolver/util/modifyUserRole' import { writeHomeCommunityEntry } from '@/seeds/community' -import { createUser, setPassword } from '@/seeds/graphql/mutations' import { UserInterface } from '@/seeds/users/UserInterface' +import { encryptPassword } from '@/password/PasswordEncryptor' export const userFactory = async ( - client: ApolloServerTestClient, + _client: any, user: UserInterface, ): Promise => { - const { mutate } = client - const homeCom = await writeHomeCommunityEntry() - // console.log('call createUser with', JSON.stringify(user, null, 2)) - const response = await mutate({ mutation: createUser, variables: user }) - if (!response?.data?.createUser) { - // console.log(JSON.stringify(response, null, 2)) - throw new Error('createUser mutation returned unexpected response') - } - const { - data: { - createUser: { id }, - }, - } = response - // get user from database - let dbUser = await User.findOneOrFail({ where: { id }, relations: ['emailContact', 'userRoles'] }) - - const emailContact = dbUser.emailContact + const dbUser = await userFactoryDb(user, homeCom) if (user.emailChecked) { - await mutate({ - mutation: setPassword, - variables: { password: 'Aa12345_', code: emailContact.emailVerificationCode }, - }) - } - - // get last changes of user from database - dbUser = await User.findOneOrFail({ where: { id }, relations: { userRoles: true, emailContact: true } }) - - if (user.createdAt || user.deletedAt || user.role) { - if (user.createdAt) { - dbUser.createdAt = user.createdAt - // make sure emailContact is also updated for e2e test, prevent failing when time between seeding and test run is < 1 minute - dbUser.emailContact.createdAt = user.createdAt - dbUser.emailContact.updatedAt = user.createdAt - await dbUser.emailContact.save() - } - if (user.deletedAt) { - dbUser.deletedAt = user.deletedAt - } - const userRole = user.role as RoleNames - if (userRole && (userRole === RoleNames.ADMIN || userRole === RoleNames.MODERATOR)) { - await setUserRole(dbUser, user.role) - } + const passwortHash = await encryptPassword(dbUser, 'Aa12345_') + dbUser.password = passwortHash await dbUser.save() } - try { - if (homeCom.communityUuid) { - dbUser.communityUuid = homeCom.communityUuid - await User.save(dbUser) - } - } catch (_err) { - // no homeCommunity exists - } - - // get last changes of user from database - dbUser = await User.findOneOrFail({ - where: { id }, - withDeleted: true, - relations: ['emailContact', 'userRoles'], - }) return dbUser } diff --git a/backend/src/seeds/transactionLink/TransactionLinkInterface.ts b/backend/src/seeds/transactionLink/TransactionLinkInterface.ts index eaacfdf92..4b2e57fa3 100644 --- a/backend/src/seeds/transactionLink/TransactionLinkInterface.ts +++ b/backend/src/seeds/transactionLink/TransactionLinkInterface.ts @@ -1,3 +1,5 @@ +export { TransactionLinkInterface } from 'database' +/* export interface TransactionLinkInterface { email: string amount: number @@ -8,3 +10,4 @@ export interface TransactionLinkInterface { // redeemedBy?: number deletedAt?: boolean } +*/ \ No newline at end of file diff --git a/backend/src/seeds/transactionLink/index.ts b/backend/src/seeds/transactionLink/index.ts index 17683b580..c5d46b146 100644 --- a/backend/src/seeds/transactionLink/index.ts +++ b/backend/src/seeds/transactionLink/index.ts @@ -1,3 +1,5 @@ +export { transactionLinks } from 'database' +/* import { TransactionLinkInterface } from './TransactionLinkInterface' export const transactionLinks: TransactionLinkInterface[] = [ @@ -53,3 +55,4 @@ bei Gradidio sei dabei!`, deletedAt: true, }, ] +*/ \ No newline at end of file diff --git a/backend/src/seeds/users/UserInterface.ts b/backend/src/seeds/users/UserInterface.ts index 1fb1b128a..abbecb254 100644 --- a/backend/src/seeds/users/UserInterface.ts +++ b/backend/src/seeds/users/UserInterface.ts @@ -1,3 +1,5 @@ +export { UserInterface } from 'database' +/* export interface UserInterface { alias?: string email?: string @@ -11,3 +13,4 @@ export interface UserInterface { publisherId?: number role?: string } +*/ \ No newline at end of file diff --git a/backend/src/seeds/users/bibi-bloxberg.ts b/backend/src/seeds/users/bibi-bloxberg.ts index f78a3af1c..dda6b1525 100644 --- a/backend/src/seeds/users/bibi-bloxberg.ts +++ b/backend/src/seeds/users/bibi-bloxberg.ts @@ -1,3 +1,5 @@ +export { bibiBloxberg } from 'database' +/* import { UserInterface } from './UserInterface' export const bibiBloxberg: UserInterface = { @@ -12,3 +14,4 @@ export const bibiBloxberg: UserInterface = { // move user createdAt before transaction link createdAt: new Date(2021, 9, 17), } +*/ \ No newline at end of file diff --git a/backend/src/seeds/users/bob-baumeister.ts b/backend/src/seeds/users/bob-baumeister.ts index b7902f7bc..61df64759 100644 --- a/backend/src/seeds/users/bob-baumeister.ts +++ b/backend/src/seeds/users/bob-baumeister.ts @@ -1,3 +1,5 @@ +export { bobBaumeister } from 'database' +/* import { UserInterface } from './UserInterface' export const bobBaumeister: UserInterface = { @@ -9,3 +11,4 @@ export const bobBaumeister: UserInterface = { emailChecked: true, language: 'de', } +*/ \ No newline at end of file diff --git a/backend/src/seeds/users/garrick-ollivander.ts b/backend/src/seeds/users/garrick-ollivander.ts index 264a866bd..332f06157 100644 --- a/backend/src/seeds/users/garrick-ollivander.ts +++ b/backend/src/seeds/users/garrick-ollivander.ts @@ -1,3 +1,6 @@ + +export { garrickOllivander } from 'database' +/* import { UserInterface } from './UserInterface' export const garrickOllivander: UserInterface = { @@ -10,3 +13,4 @@ export const garrickOllivander: UserInterface = { emailChecked: false, language: 'en', } +*/ \ No newline at end of file diff --git a/backend/src/seeds/users/peter-lustig.ts b/backend/src/seeds/users/peter-lustig.ts index ff1fe065e..6c86ef800 100644 --- a/backend/src/seeds/users/peter-lustig.ts +++ b/backend/src/seeds/users/peter-lustig.ts @@ -1,3 +1,5 @@ +export { peterLustig } from 'database' +/* import { RoleNames } from '@enum/RoleNames' import { UserInterface } from './UserInterface' @@ -12,3 +14,4 @@ export const peterLustig: UserInterface = { language: 'de', role: RoleNames.ADMIN, } +*/ \ No newline at end of file diff --git a/backend/src/seeds/users/raeuber-hotzenplotz.ts b/backend/src/seeds/users/raeuber-hotzenplotz.ts index 62601efff..dc52f10b4 100644 --- a/backend/src/seeds/users/raeuber-hotzenplotz.ts +++ b/backend/src/seeds/users/raeuber-hotzenplotz.ts @@ -1,3 +1,6 @@ + +export { raeuberHotzenplotz } from 'database' +/* import { UserInterface } from './UserInterface' export const raeuberHotzenplotz: UserInterface = { @@ -8,3 +11,4 @@ export const raeuberHotzenplotz: UserInterface = { emailChecked: true, language: 'de', } +*/ \ No newline at end of file diff --git a/backend/src/seeds/users/stephen-hawking.ts b/backend/src/seeds/users/stephen-hawking.ts index a683b7579..a9a2085e6 100644 --- a/backend/src/seeds/users/stephen-hawking.ts +++ b/backend/src/seeds/users/stephen-hawking.ts @@ -1,3 +1,5 @@ +export { stephenHawking } from 'database' +/* import { UserInterface } from './UserInterface' export const stephenHawking: UserInterface = { @@ -10,3 +12,4 @@ export const stephenHawking: UserInterface = { deletedAt: new Date('2018-03-14T09:17:52'), language: 'en', } +*/ \ No newline at end of file diff --git a/bun.lock b/bun.lock index 6dd175b61..5485b2e8d 100644 --- a/bun.lock +++ b/bun.lock @@ -245,7 +245,7 @@ "@types/mysql": "^2.15.27", "@types/node": "^18.7.14", "await-semaphore": "^0.1.3", - "crypto-random-bigint": "^2.1.1", + "random-bigint": "^0.0.1", "ts-node": "^10.9.2", "typescript": "^4.9.5", }, @@ -1768,8 +1768,6 @@ "cross-spawn": ["cross-spawn@7.0.6", "", { "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" } }, "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA=="], - "crypto-random-bigint": ["crypto-random-bigint@2.1.1", "", { "dependencies": { "uint-rng": "^1.2.1" } }, "sha512-96+FDrenXybkpnLML/60S8NcG32KgJ5Y8yvNNCYPW02r+ssoXFR5XKenuIQcHLWumnGj8UPqUUHBzXNrDGkDmQ=="], - "css-functions-list": ["css-functions-list@3.2.3", "", {}, "sha512-IQOkD3hbR5KrN93MtcYuad6YPuTSUhntLHDuLEbFWE+ff2/XSZNdZG+LcbbIW5AXKg/WFIfYItIzVoHngHXZzA=="], "css-select": ["css-select@4.3.0", "", { "dependencies": { "boolbase": "^1.0.0", "css-what": "^6.0.1", "domhandler": "^4.3.1", "domutils": "^2.8.0", "nth-check": "^2.0.1" } }, "sha512-wPpOYtnsVontu2mODhA19JrqWxNsfdatRKd64kmpRbQgh1KtItko5sTnEpPdpSaJszTOhEMlF/RPz28qj4HqhQ=="], @@ -3278,8 +3276,6 @@ "tiny-case": ["tiny-case@1.0.3", "", {}, "sha512-Eet/eeMhkO6TX8mnUteS9zgPbUMQa4I6Kkp5ORiBD5476/m+PIRiumP5tmh5ioJpH7k51Kehawy2UDfsnxxY8Q=="], - "tiny-webcrypto": ["tiny-webcrypto@1.0.3", "", {}, "sha512-LQQdNMAgz9BXNT2SKbYh3eCb+fLV0p7JB7MwUjzY6IOlQLGIadfnFqRpshERsS5Dl2OM/hs0+4I/XmSrF+RBbw=="], - "tinybench": ["tinybench@2.9.0", "", {}, "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg=="], "tinyexec": ["tinyexec@1.0.1", "", {}, "sha512-5uC6DDlmeqiOwCPmK9jMSdOuZTh8bU39Ys6yidB+UTt5hfZUPGAypSgFRiEp+jbi9qH40BLDvy85jIU88wKSqw=="], @@ -3392,8 +3388,6 @@ "uglify-js": ["uglify-js@3.19.3", "", { "bin": { "uglifyjs": "bin/uglifyjs" } }, "sha512-v3Xu+yuwBXisp6QYTcH4UbH+xYJXqnq2m/LtQVWKWzYc1iehYnLixoQDN9FH6/j9/oybfd6W9Ghwkl8+UMKTKQ=="], - "uint-rng": ["uint-rng@1.2.1", "", { "dependencies": { "tiny-webcrypto": "^1.0.2" } }, "sha512-swhDg5H+3DX2sIvnYA7VMBMXV/t8mPxvh49CjCDkwFmj/3OZIDOQwJANBgM1MPSUBrUHNIlXmU7/GcL7m4907g=="], - "uint8array-extras": ["uint8array-extras@1.5.0", "", {}, "sha512-rvKSBiC5zqCCiDZ9kAOszZcDvdAHwwIKJG33Ykj43OKcWsnmcBRL09YTU4nOeHZ8Y2a7l1MgTd08SBe9A8Qj6A=="], "unbox-primitive": ["unbox-primitive@1.1.0", "", { "dependencies": { "call-bound": "^1.0.3", "has-bigints": "^1.0.2", "has-symbols": "^1.1.0", "which-boxed-primitive": "^1.1.1" } }, "sha512-nWJ91DjeOkej/TA8pXQ3myruKpKEYgqvpw9lz4OPHj/NWFNluYrjbz9j01CJ8yKQd2g4jFoOkINCTW2I5LEEyw=="], diff --git a/database/@types/random-bigint/index.d.ts b/database/@types/random-bigint/index.d.ts new file mode 100644 index 000000000..9692fcbf7 --- /dev/null +++ b/database/@types/random-bigint/index.d.ts @@ -0,0 +1,5 @@ + +declare module 'random-bigint' { + function random(bits: number, cb?: (err: Error, num: BigInt) => void): BigInt + export = random +} diff --git a/database/package.json b/database/package.json index 906e8682e..ada327ba9 100644 --- a/database/package.json +++ b/database/package.json @@ -25,7 +25,7 @@ "up": "cross-env TZ=UTC tsx migration/index.ts up", "down": "cross-env TZ=UTC tsx migration/index.ts down", "reset": "cross-env TZ=UTC tsx migration/index.ts reset", - "seed": "cross-env TZ=UTC NODE_ENV=development bun src/seeds/index.ts", + "seed": "cross-env TZ=UTC NODE_ENV=development bun src/seeds/seed.ts", "up:test": "cross-env TZ=UTC DB_DATABASE=gradido_test tsx migration/index.ts up", "up:backend_test": "cross-env TZ=UTC DB_DATABASE=gradido_test_backend tsx migration/index.ts up", "up:federation_test": "cross-env TZ=UTC DB_DATABASE=gradido_test_federation tsx migration/index.ts up", @@ -39,7 +39,7 @@ "@types/mysql": "^2.15.27", "@types/node": "^18.7.14", "await-semaphore": "^0.1.3", - "crypto-random-bigint": "^2.1.1", + "random-bigint": "^0.0.1", "ts-node": "^10.9.2", "typescript": "^4.9.5" }, diff --git a/backend/src/seeds/graphql/enums.ts b/database/src/enum/GdtEntryType.ts similarity index 100% rename from backend/src/seeds/graphql/enums.ts rename to database/src/enum/GdtEntryType.ts diff --git a/database/src/index.ts b/database/src/index.ts index 45b60530c..2afe19cad 100644 --- a/database/src/index.ts +++ b/database/src/index.ts @@ -4,6 +4,7 @@ export { latestDbVersion } export * from './entity' export * from './logging' export * from './queries' +export * from './seeds' export * from './util' export * from './enum' export { AppDatabase } from './AppDatabase' diff --git a/database/src/seeds/contributionLink/index.ts b/database/src/seeds/contributionLink/index.ts index 41d28eb60..15c34d32b 100644 --- a/database/src/seeds/contributionLink/index.ts +++ b/database/src/seeds/contributionLink/index.ts @@ -1,4 +1,5 @@ import { ContributionLinkInterface } from './ContributionLinkInterface' +export type { ContributionLinkInterface } export const contributionLinks: ContributionLinkInterface[] = [ { diff --git a/database/src/seeds/creation/index.ts b/database/src/seeds/creation/index.ts index 109591d46..f20ec2bc5 100644 --- a/database/src/seeds/creation/index.ts +++ b/database/src/seeds/creation/index.ts @@ -2,6 +2,8 @@ import { nMonthsBefore } from '../factory/creation' import { CreationInterface } from './CreationInterface' +export type { CreationInterface } + const bobsSendings = [ { amount: 10, diff --git a/database/src/seeds/factory/contributionLink.ts b/database/src/seeds/factory/contributionLink.ts index 031fa9c00..c05888568 100644 --- a/database/src/seeds/factory/contributionLink.ts +++ b/database/src/seeds/factory/contributionLink.ts @@ -5,13 +5,11 @@ import { ContributionLinkInterface } from '../contributionLink/ContributionLinkI import { transactionLinkCode } from './transactionLink' import { ContributionCycleType } from '../../enum' -export const contributionLinkFactory = async ( - contributionLink: ContributionLinkInterface, -): Promise => { +export function contributionLinkFactory(contributionLink: ContributionLinkInterface): Promise { return createContributionLink(contributionLink) } -export async function createContributionLink(contributionLinkData: ContributionLinkInterface): Promise { +export function createContributionLink(contributionLinkData: ContributionLinkInterface): Promise { const contributionLink = new ContributionLink() contributionLink.amount = new Decimal(contributionLinkData.amount) contributionLink.name = contributionLinkData.name diff --git a/database/src/seeds/factory/index.ts b/database/src/seeds/factory/index.ts new file mode 100644 index 000000000..1165a6f27 --- /dev/null +++ b/database/src/seeds/factory/index.ts @@ -0,0 +1,6 @@ +export * from './contributionLink' +export * from './creation' +export * from './pendingTransaction' +export * from './transaction' +export * from './transactionLink' +export * from './user' diff --git a/database/src/seeds/factory/user.ts b/database/src/seeds/factory/user.ts index bebd07cf0..009c007df 100644 --- a/database/src/seeds/factory/user.ts +++ b/database/src/seeds/factory/user.ts @@ -3,7 +3,7 @@ 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 random from 'random-bigint' import { Community } from '../../entity' import { AppDatabase } from '../..' import { RoleNames } from '../../enum/RoleNames' @@ -12,14 +12,13 @@ export async function userFactory(user: UserInterface, homeCommunity?: Community // TODO: improve with cascade let dbUser = await createUser(user, homeCommunity) let dbUserContact = await createUserContact(user, dbUser.id) - dbUserContact = await dbUserContact.save() 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) + dbUser.userRoles = [await createUserRole(dbUser.id, userRole)] } return dbUser @@ -86,7 +85,7 @@ export async function createUser(user: UserInterface, homeCommunity?: Community dbUser.gradidoID = v4() if (user.emailChecked) { - dbUser.password = random(64) + // dbUser.password = dbUser.passwordEncryptionType = PasswordEncryptionType.GRADIDO_ID } if (!homeCommunity) { diff --git a/database/src/seeds/index.ts b/database/src/seeds/index.ts index 4da2c28e3..61c60ba28 100644 --- a/database/src/seeds/index.ts +++ b/database/src/seeds/index.ts @@ -1,98 +1,5 @@ -import { AppDatabase } from '../AppDatabase' -import { createCommunity } from './community' -import { userFactoryBulk } from './factory/user' -import { users } from './users' -import { internet, name } from 'faker' -import { creationFactoryBulk } from './factory/creation' -import { creations } from './creation' -import { transactionLinkCode, transactionLinkFactoryBulk } from './factory/transactionLink' -import { transactionLinks } from './transactionLink' -import { contributionLinkFactory } from './factory/contributionLink' -import { contributionLinks } from './contributionLink' -import { User } from '../entity' -import { UserInterface } from './users/UserInterface' - -const RANDOM_USER_COUNT = 100 - -async function run() { - console.info('##seed## seeding started...') - - const db = AppDatabase.getInstance() - await db.init() - await clearDatabase() - - // seed home community - const homeCommunity = await createCommunity(false) - console.info(`##seed## seeding home community successful ...`) - - // seed standard users - // put into map for later direct access - const userCreationIndexedByEmail = new Map() - const defaultUsers = await userFactoryBulk(users, homeCommunity) - for (const dbUser of defaultUsers) { - userCreationIndexedByEmail.set(dbUser.emailContact.email, dbUser) - } - console.info(`##seed## seeding all standard users successful ...`) - - // seed 100 random users - const randomUsers = new Array(RANDOM_USER_COUNT) - for (let i = 0; i < RANDOM_USER_COUNT; i++) { - randomUsers[i] = { - firstName: name.firstName(), - lastName: name.lastName(), - email: internet.email(), - language: Math.random() < 0.5 ? 'en' : 'de', - } - } - await userFactoryBulk(randomUsers, homeCommunity) - console.info(`##seed## seeding ${RANDOM_USER_COUNT} random users successful ...`) - - // create GDD serial, must be called one after another because seeding don't use semaphore - const moderatorUser = userCreationIndexedByEmail.get('peter@lustig.de')! - await creationFactoryBulk(creations, userCreationIndexedByEmail, moderatorUser) - console.info(`##seed## seeding all creations successful ...`) - - // create Contribution Links - for (const contributionLink of contributionLinks) { - await contributionLinkFactory(contributionLink) - } - console.info(`##seed## seeding all contributionLinks successful ...`) - - // create Transaction Links - const movedTransactionLinks = transactionLinks.map(transactionLink => { - let createdAt = new Date(new Date().getTime() + 1000) - if (transactionLink.createdAt) { - createdAt = transactionLink.createdAt - } - return { - ...transactionLink, - createdAt: createdAt, - } - }) - await transactionLinkFactoryBulk(movedTransactionLinks, userCreationIndexedByEmail) - console.info(`##seed## seeding all transactionLinks successful ...`) - - await db.destroy() - console.info(`##seed## seeding successful...`) -} - -async function clearDatabase() { - await AppDatabase.getInstance().getDataSource().transaction(async trx => { - await trx.query(`SET FOREIGN_KEY_CHECKS = 0`) - await trx.query(`TRUNCATE TABLE contributions`) - 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`) - await trx.query(`SET FOREIGN_KEY_CHECKS = 1`) - }) -} - -run().catch((err) => { - // biome-ignore lint/suspicious/noConsole: no logger present - console.error('error on seeding', err) -}) - +export * from './contributionLink' +export * from './creation' +export * from './factory' +export * from './transactionLink' +export * from './users' diff --git a/database/src/seeds/seed.ts b/database/src/seeds/seed.ts new file mode 100644 index 000000000..e551d7a61 --- /dev/null +++ b/database/src/seeds/seed.ts @@ -0,0 +1,98 @@ +import { AppDatabase } from '../AppDatabase' +import { createCommunity } from './community' +import { userFactoryBulk } from './factory/user' +import { users } from './users' +import { internet, name } from 'faker' +import { creationFactoryBulk } from './factory/creation' +import { creations } from './creation' +import { transactionLinkFactoryBulk } from './factory/transactionLink' +import { transactionLinks } from './transactionLink' +import { contributionLinkFactory } from './factory/contributionLink' +import { contributionLinks } from './contributionLink' +import { User } from '../entity' +import { UserInterface } from './users/UserInterface' + +const RANDOM_USER_COUNT = 100 + +async function run() { + console.info('##seed## seeding started...') + + const db = AppDatabase.getInstance() + await db.init() + await clearDatabase() + + // seed home community + const homeCommunity = await createCommunity(false) + console.info(`##seed## seeding home community successful ...`) + + // seed standard users + // put into map for later direct access + const userCreationIndexedByEmail = new Map() + const defaultUsers = await userFactoryBulk(users, homeCommunity) + for (const dbUser of defaultUsers) { + userCreationIndexedByEmail.set(dbUser.emailContact.email, dbUser) + } + console.info(`##seed## seeding all standard users successful ...`) + + // seed 100 random users + const randomUsers = new Array(RANDOM_USER_COUNT) + for (let i = 0; i < RANDOM_USER_COUNT; i++) { + randomUsers[i] = { + firstName: name.firstName(), + lastName: name.lastName(), + email: internet.email(), + language: Math.random() < 0.5 ? 'en' : 'de', + } + } + await userFactoryBulk(randomUsers, homeCommunity) + console.info(`##seed## seeding ${RANDOM_USER_COUNT} random users successful ...`) + + // create GDD serial, must be called one after another because seeding don't use semaphore + const moderatorUser = userCreationIndexedByEmail.get('peter@lustig.de')! + await creationFactoryBulk(creations, userCreationIndexedByEmail, moderatorUser) + console.info(`##seed## seeding all creations successful ...`) + + // create Contribution Links + for (const contributionLink of contributionLinks) { + await contributionLinkFactory(contributionLink) + } + console.info(`##seed## seeding all contributionLinks successful ...`) + + // create Transaction Links + const movedTransactionLinks = transactionLinks.map(transactionLink => { + let createdAt = new Date(new Date().getTime() + 1000) + if (transactionLink.createdAt) { + createdAt = transactionLink.createdAt + } + return { + ...transactionLink, + createdAt: createdAt, + } + }) + await transactionLinkFactoryBulk(movedTransactionLinks, userCreationIndexedByEmail) + console.info(`##seed## seeding all transactionLinks successful ...`) + + await db.destroy() + console.info(`##seed## seeding successful...`) +} + +async function clearDatabase() { + await AppDatabase.getInstance().getDataSource().transaction(async trx => { + await trx.query(`SET FOREIGN_KEY_CHECKS = 0`) + await trx.query(`TRUNCATE TABLE contributions`) + 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`) + await trx.query(`SET FOREIGN_KEY_CHECKS = 1`) + }) +} + +run().catch((err) => { + // biome-ignore lint/suspicious/noConsole: no logger present + console.error('error on seeding', err) +}) + diff --git a/database/src/seeds/transactionLink/index.ts b/database/src/seeds/transactionLink/index.ts index 17683b580..2d5364aa5 100644 --- a/database/src/seeds/transactionLink/index.ts +++ b/database/src/seeds/transactionLink/index.ts @@ -1,4 +1,5 @@ import { TransactionLinkInterface } from './TransactionLinkInterface' +export type { TransactionLinkInterface } export const transactionLinks: TransactionLinkInterface[] = [ { diff --git a/database/src/seeds/users/index.ts b/database/src/seeds/users/index.ts index beb6c6f25..8149ffd4c 100644 --- a/database/src/seeds/users/index.ts +++ b/database/src/seeds/users/index.ts @@ -4,8 +4,19 @@ import { garrickOllivander } from './garrick-ollivander' import { peterLustig } from './peter-lustig' import { raeuberHotzenplotz } from './raeuber-hotzenplotz' import { stephenHawking } from './stephen-hawking' +import { UserInterface } from './UserInterface' -export const users = [ +export { + type UserInterface, + bibiBloxberg, + bobBaumeister, + garrickOllivander, + peterLustig, + raeuberHotzenplotz, + stephenHawking +} + +export const users: UserInterface[] = [ peterLustig, bibiBloxberg, bobBaumeister,