diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index 1ffa53b27..af313a9a7 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -1,4 +1,5 @@ import { IsNull, getConnection } from '@dbTools/typeorm' +import { Community as DbCommunity } from '@entity/Community' import { Contribution as DbContribution } from '@entity/Contribution' import { ContributionMessage } from '@entity/ContributionMessage' import { Transaction as DbTransaction } from '@entity/Transaction' @@ -447,6 +448,7 @@ export class ContributionResolver { if (user.deletedAt) { throw new LogError('Can not confirm contribution since the user was deleted') } + const homeCom = await DbCommunity.findOneOrFail({ where: { foreign: false } }) const creations = await getUserCreation(contribution.userId, clientTimezoneOffset, false) validateContribution( creations, @@ -480,6 +482,9 @@ export class ContributionResolver { transaction.typeId = TransactionTypeId.CREATION transaction.memo = contribution.memo transaction.userId = contribution.userId + if (homeCom.communityUuid) { + transaction.userCommunityUuid = homeCom.communityUuid + } transaction.userGradidoID = user.gradidoID transaction.userName = fullName(user.firstName, user.lastName) transaction.previous = lastTransaction ? lastTransaction.id : null diff --git a/backend/src/seeds/community/index.ts b/backend/src/seeds/community/index.ts new file mode 100644 index 000000000..d66c02c21 --- /dev/null +++ b/backend/src/seeds/community/index.ts @@ -0,0 +1,34 @@ +import { Community as DbCommunity } from '@entity/Community' +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 { + // 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.url = 'http://localhost/api/' + homeCom.name = CONFIG.COMMUNITY_NAME + homeCom.description = CONFIG.COMMUNITY_DESCRIPTION + homeCom.creationDate = new Date() + await DbCommunity.insert(homeCom) + } + } catch (err) { + throw new Error(`Seeding: Error writing HomeCommunity-Entry: ${err}`) + } +} diff --git a/backend/src/seeds/index.ts b/backend/src/seeds/index.ts index bc7950f26..b22409759 100644 --- a/backend/src/seeds/index.ts +++ b/backend/src/seeds/index.ts @@ -12,6 +12,7 @@ import { CONFIG } from '@/config' import { createServer } from '@/server/createServer' import { backendLogger as logger } from '@/server/logger' +import { writeHomeCommunityEntry } from './community' import { contributionLinks } from './contributionLink/index' import { creations } from './creation/index' import { contributionLinkFactory } from './factory/contributionLink' @@ -57,6 +58,9 @@ const run = async () => { await cleanDB() logger.info('##seed## clean database successful...') + // seed home community + await writeHomeCommunityEntry() + // seed the standard users for (const user of users) { await userFactory(seedClient, user) diff --git a/backend/src/util/virtualTransactions.ts b/backend/src/util/virtualTransactions.ts index a10e566d1..74a065d55 100644 --- a/backend/src/util/virtualTransactions.ts +++ b/backend/src/util/virtualTransactions.ts @@ -58,6 +58,8 @@ const virtualLinkTransaction = ( userName: null, linkedUserGradidoID: null, linkedUserName: null, + userCommunityUuid: '', + linkedUserCommunityUuid: null, } return new Transaction(linkDbTransaction, user) } @@ -92,6 +94,8 @@ const virtualDecayTransaction = ( userName: null, linkedUserGradidoID: null, linkedUserName: null, + userCommunityUuid: '', + linkedUserCommunityUuid: null, } return new Transaction(decayDbTransaction, user) }