correct seeding with home community

This commit is contained in:
Claus-Peter Huebner 2023-09-18 21:49:08 +02:00
parent 9a20a85ec9
commit b88ff9b3df
4 changed files with 47 additions and 0 deletions

View File

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

View File

@ -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<void> {
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}`)
}
}

View File

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

View File

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