diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 25e901491..7ad0271ea 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,7 +12,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0073-introduce_foreign_user_in_users_table', + DB_VERSION: '0074-insert_communityuuid in_existing_users', 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/EmailOptinCodes.test.ts b/backend/src/graphql/resolver/EmailOptinCodes.test.ts index 640faad17..09ad743fe 100644 --- a/backend/src/graphql/resolver/EmailOptinCodes.test.ts +++ b/backend/src/graphql/resolver/EmailOptinCodes.test.ts @@ -8,6 +8,7 @@ import { GraphQLError } from 'graphql' import { testEnvironment, cleanDB } from '@test/helpers' import { CONFIG } from '@/config' +import { writeHomeCommunityEntry } from '@/seeds/community' import { createUser, setPassword, forgotPassword } from '@/seeds/graphql/mutations' import { queryOptIn } from '@/seeds/graphql/queries' @@ -46,6 +47,7 @@ describe('EmailOptinCodes', () => { lastName: 'Lustig', language: 'de', } + await writeHomeCommunityEntry() const { data: { createUser: user }, } = await mutate({ mutation: createUser, variables }) diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index a9c50553e..e16e0f0fc 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -34,6 +34,7 @@ import { import { EventType } from '@/event/Events' import { SecretKeyCryptographyCreateKey } from '@/password/EncryptorUtils' import { encryptPassword } from '@/password/PasswordEncryptor' +import { writeHomeCommunityEntry } from '@/seeds/community' import { contributionLinkFactory } from '@/seeds/factory/contributionLink' import { transactionLinkFactory } from '@/seeds/factory/transactionLink' import { userFactory } from '@/seeds/factory/user' @@ -125,9 +126,11 @@ describe('UserResolver', () => { let result: any let emailVerificationCode: string let user: User[] + let homeCom: DbCommunity beforeAll(async () => { jest.clearAllMocks() + homeCom = await writeHomeCommunityEntry() result = await mutate({ mutation: createUser, variables }) }) @@ -172,7 +175,7 @@ describe('UserResolver', () => { referrerId: null, contributionLinkId: null, passwordEncryptionType: PasswordEncryptionType.NO_PASSWORD, - communityUuid: null, + communityUuid: homeCom.communityUuid, foreign: false, }, ]) @@ -542,6 +545,7 @@ describe('UserResolver', () => { let newUser: User beforeAll(async () => { + await writeHomeCommunityEntry() await mutate({ mutation: createUser, variables: createUserVariables }) const emailContact = await UserContact.findOneOrFail({ where: { email: createUserVariables.email }, @@ -586,6 +590,7 @@ describe('UserResolver', () => { describe('no valid password', () => { beforeAll(async () => { + await writeHomeCommunityEntry() await mutate({ mutation: createUser, variables: createUserVariables }) const emailContact = await UserContact.findOneOrFail({ where: { email: createUserVariables.email }, diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 45ccd720e..1f21abbb9 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -275,6 +275,10 @@ export class UserResolver { { id: 0 } as DbUser, ) let dbUser = new DbUser() + const homeCom = await getHomeCommunity() + if (homeCom.communityUuid) { + dbUser.communityUuid = homeCom.communityUuid + } dbUser.gradidoID = gradidoID dbUser.firstName = firstName dbUser.lastName = lastName diff --git a/backend/src/seeds/community/index.ts b/backend/src/seeds/community/index.ts index 6a639ee44..84542e002 100644 --- a/backend/src/seeds/community/index.ts +++ b/backend/src/seeds/community/index.ts @@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid' import { CONFIG } from '@/config' -export async function writeHomeCommunityEntry(): Promise { +export async function writeHomeCommunityEntry(): Promise { try { // check for existing homeCommunity entry let homeCom = await DbCommunity.findOne({ where: { foreign: false } }) @@ -28,6 +28,7 @@ export async function writeHomeCommunityEntry(): Promise { homeCom.creationDate = new Date() await DbCommunity.insert(homeCom) } + return homeCom } catch (err) { throw new Error(`Seeding: Error writing HomeCommunity-Entry`) // : ${err}`) } diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index 65b0ff3bb..3ddddf336 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -5,8 +5,8 @@ import { ApolloServerTestClient } from 'apollo-server-testing' import { RoleNames } from '@enum/RoleNames' -import { getHomeCommunity } from '@/graphql/resolver/util/communities' 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' @@ -16,6 +16,8 @@ export const userFactory = async ( ): Promise => { const { mutate } = client + const homeCom = await writeHomeCommunityEntry() + const { data: { createUser: { id }, @@ -45,7 +47,6 @@ export const userFactory = async ( await dbUser.save() } try { - const homeCom = await getHomeCommunity() if (homeCom.communityUuid) { dbUser.communityUuid = homeCom.communityUuid await User.save(dbUser) diff --git a/database/migrations/0074-insert_communityuuid in_existing_users.ts b/database/migrations/0074-insert_communityuuid in_existing_users.ts new file mode 100644 index 000000000..11ddf7096 --- /dev/null +++ b/database/migrations/0074-insert_communityuuid in_existing_users.ts @@ -0,0 +1,18 @@ +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // read the community uuid of the homeCommunity + const result = await queryFn(`SELECT c.community_uuid from communities as c WHERE c.foreign = 0`) + // and if uuid exists enter the home_community_uuid for all local users + if (result && result[0]) { + await queryFn( + `UPDATE users as u SET u.community_uuid = "${result[0].community_uuid}" WHERE u.foreign = 0 AND u.community_uuid IS NULL`, + ) + } +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // dummy statement to satisfy linter and queryFn + await queryFn('select count(*) from communities') +} diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 2d88e0f92..7aed88ccd 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -4,7 +4,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0073-introduce_foreign_user_in_users_table', + DB_VERSION: '0074-insert_communityuuid in_existing_users', LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info LOG_LEVEL: process.env.LOG_LEVEL || 'info', diff --git a/federation/src/config/index.ts b/federation/src/config/index.ts index 7cc9ef37e..2770ada06 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -10,7 +10,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0073-introduce_foreign_user_in_users_table', + DB_VERSION: '0074-insert_communityuuid in_existing_users', 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