Merge pull request #3232 from gradido/3231-bug-new-local-user-without-communitiyuuid

fix(backend): new local user without communitiyuuid
This commit is contained in:
clauspeterhuebner 2023-11-07 23:47:46 +01:00 committed by GitHub
commit d29e41e025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 7 deletions

View File

@ -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

View File

@ -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 })

View File

@ -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 },

View File

@ -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

View File

@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid'
import { CONFIG } from '@/config'
export async function writeHomeCommunityEntry(): Promise<void> {
export async function writeHomeCommunityEntry(): Promise<DbCommunity> {
try {
// check for existing homeCommunity entry
let homeCom = await DbCommunity.findOne({ where: { foreign: false } })
@ -28,6 +28,7 @@ export async function writeHomeCommunityEntry(): Promise<void> {
homeCom.creationDate = new Date()
await DbCommunity.insert(homeCom)
}
return homeCom
} catch (err) {
throw new Error(`Seeding: Error writing HomeCommunity-Entry`) // : ${err}`)
}

View File

@ -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<User> => {
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)

View File

@ -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<Array<any>>) {
// 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<Array<any>>) {
// dummy statement to satisfy linter and queryFn
await queryFn('select count(*) from communities')
}

View File

@ -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',

View File

@ -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