update community field names in transactions table, fix bug in DataSource

This commit is contained in:
einhorn_b 2023-12-11 14:36:10 +01:00
parent 443a7308a1
commit b8ee84b7be
9 changed files with 50 additions and 48 deletions

View File

@ -4,7 +4,7 @@ dotenv.config()
const constants = {
LOG4JS_CONFIG: 'log4js-config.json',
DB_VERSION: '0002-refactor_add_community',
DB_VERSION: '0003-refactor_transaction_recipe',
// default log level on production should be info
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
CONFIG_VERSION: {

View File

@ -51,8 +51,8 @@ export class TransactionBuilder {
return this.transaction
}
public getSenderCommunity(): Community {
return this.transaction.senderCommunity
public getCommunity(): Community {
return this.transaction.community
}
public setSigningAccount(signingAccount: Account): TransactionBuilder {
@ -65,21 +65,21 @@ export class TransactionBuilder {
return this
}
public setSenderCommunity(senderCommunity: Community): TransactionBuilder {
this.transaction.senderCommunity = senderCommunity
public setCommunity(community: Community): TransactionBuilder {
this.transaction.community = community
return this
}
public setRecipientCommunity(recipientCommunity?: Community): TransactionBuilder {
if (!this.transaction.senderCommunity) {
throw new LogError('Please set sender community first!')
public setOtherCommunity(otherCommunity?: Community): TransactionBuilder {
if (!this.transaction.community) {
throw new LogError('Please set community first!')
}
this.transaction.recipientCommunity =
recipientCommunity &&
this.transaction.senderCommunity &&
this.transaction.senderCommunity.id !== recipientCommunity.id
? recipientCommunity
this.transaction.otherCommunity =
otherCommunity &&
this.transaction.community &&
this.transaction.community.id !== otherCommunity.id
? otherCommunity
: undefined
return this
}
@ -98,21 +98,19 @@ export class TransactionBuilder {
senderUser: UserIdentifier,
): Promise<TransactionBuilder> {
// get sender community
const senderCommunity = await CommunityRepository.getCommunityForUserIdentifier(senderUser)
if (!senderCommunity) {
throw new LogError("couldn't find sender community for transaction")
const community = await CommunityRepository.getCommunityForUserIdentifier(senderUser)
if (!community) {
throw new LogError("couldn't find community for transaction")
}
return this.setSenderCommunity(senderCommunity)
return this.setCommunity(community)
}
public async setRecipientCommunityFromRecipientUser(
public async setOtherCommunityFromRecipientUser(
recipientUser: UserIdentifier,
): Promise<TransactionBuilder> {
// get recipient community
const recipientCommunity = await CommunityRepository.getCommunityForUserIdentifier(
recipientUser,
)
return this.setRecipientCommunity(recipientCommunity)
const otherCommunity = await CommunityRepository.getCommunityForUserIdentifier(recipientUser)
return this.setOtherCommunity(otherCommunity)
}
public async fromGradidoTransactionSearchForAccounts(

View File

@ -4,11 +4,11 @@ import { Transaction } from '@entity/Transaction'
@ObjectType()
export class TransactionRecipe {
public constructor({ id, createdAt, type, senderCommunity }: Transaction) {
public constructor({ id, createdAt, type, community }: Transaction) {
this.id = id
this.createdAt = createdAt.toString()
this.type = type
this.topic = senderCommunity.iotaTopic
this.topic = community.iotaTopic
}
@Field(() => Int)

View File

@ -15,7 +15,7 @@ export class CommunityRootTransactionRole extends TransactionRecipeRole {
.fromCommunityDraft(communityDraft, community)
.build()
// build transaction entity
this.transactionBuilder.fromTransactionBody(transactionBody).setSenderCommunity(community)
this.transactionBuilder.fromTransactionBody(transactionBody).setCommunity(community)
const transaction = this.transactionBuilder.getTransaction()
// sign
this.transactionBuilder.setSignature(sign(transaction.bodyBytes, new KeyPair(community)))

View File

@ -47,12 +47,12 @@ export class TransactionRecipeRole {
.setBackendTransactionId(transactionDraft.backendTransactionId)
await this.transactionBuilder.setSenderCommunityFromSenderUser(senderUser)
if (recipientUser.communityUuid !== senderUser.communityUuid) {
await this.transactionBuilder.setRecipientCommunityFromRecipientUser(recipientUser)
await this.transactionBuilder.setOtherCommunityFromRecipientUser(recipientUser)
}
const transaction = this.transactionBuilder.getTransaction()
// sign
this.transactionBuilder.setSignature(
sign(transaction.bodyBytes, new KeyPair(this.transactionBuilder.getSenderCommunity())),
sign(transaction.bodyBytes, new KeyPair(this.transactionBuilder.getCommunity())),
)
return this
}

View File

@ -71,12 +71,16 @@ export class Connection {
}
async checkDBVersion(DB_VERSION: string): Promise<void> {
const dbVersion = await Migration.findOneOrFail({ order: { version: 'DESC' } })
const dbVersion = await Migration.find({ order: { version: 'DESC' }, take: 1 })
if (!dbVersion || dbVersion.length < 1) {
throw new LogError('found no db version in migrations, could dlt-database run successfully?')
}
console.log(dbVersion[0].fileName)
// return dbVersion ? dbVersion.fileName : null
if (!dbVersion.fileName.includes(DB_VERSION)) {
if (!dbVersion[0].fileName.includes(DB_VERSION)) {
throw new LogError(
`Wrong database version detected - the backend requires '${DB_VERSION}' but found '${
dbVersion ?? 'None'
dbVersion[0].fileName ?? 'None'
}`,
)
}

View File

@ -56,9 +56,9 @@ export class Community extends BaseEntity {
@JoinColumn({ name: 'community_id' })
accountCommunities: AccountCommunity[]
@OneToMany(() => Transaction, (recipe) => recipe.senderCommunity)
transactionSender?: Transaction[]
@OneToMany(() => Transaction, (transaction) => transaction.community)
transactions?: Transaction[]
@OneToMany(() => Transaction, (recipe) => recipe.recipientCommunity)
transactionRecipient?: Transaction[]
@OneToMany(() => Transaction, (transaction) => transaction.otherCommunity)
friendCommunitiesTransactions?: Transaction[]
}

View File

@ -46,21 +46,21 @@ export class Transaction extends BaseEntity {
@Column({ name: 'recipient_account_id', type: 'int', unsigned: true, nullable: true })
recipientAccountId?: number
@ManyToOne(() => Community, (community) => community.transactionSender, {
@ManyToOne(() => Community, (community) => community.transactions, {
eager: true,
})
@JoinColumn({ name: 'sender_community_id' })
senderCommunity: Community
@JoinColumn({ name: 'community_id' })
community: Community
@Column({ name: 'sender_community_id', type: 'int', unsigned: true })
senderCommunityId: number
@Column({ name: 'community_id', type: 'int', unsigned: true })
communityId: number
@ManyToOne(() => Community, (community) => community.transactionRecipient)
@JoinColumn({ name: 'recipient_community_id' })
recipientCommunity?: Community
@ManyToOne(() => Community, (community) => community.friendCommunitiesTransactions)
@JoinColumn({ name: 'other_community_id' })
otherCommunity?: Community
@Column({ name: 'recipient_community_id', type: 'int', unsigned: true, nullable: true })
recipientCommunityId?: number
@Column({ name: 'other_community_id', type: 'int', unsigned: true, nullable: true })
otherCommunityId?: number
@Column({
type: 'decimal',

View File

@ -34,8 +34,8 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
\`paring_transaction_id\` bigint unsigned NULL DEFAULT NULL,
\`signing_account_id\` int unsigned NULL DEFAULT NULL,
\`recipient_account_id\` int unsigned NULL DEFAULT NULL,
\`sender_community_id\` int unsigned NOT NULL,
\`recipient_community_id\` int unsigned NULL DEFAULT NULL,
\`community_id\` int unsigned NOT NULL,
\`other_community_id\` int unsigned NULL DEFAULT NULL,
\`amount\` decimal(40, 20) NULL DEFAULT NULL,
\`account_balance_created_at\` decimal(40, 20) NOT NULL,
\`type\` tinyint NOT NULL,
@ -52,8 +52,8 @@ export async function upgrade(queryFn: (query: string, values?: any[]) => Promis
UNIQUE KEY \`signature\` (\`signature\`),
FOREIGN KEY (\`signing_account_id\`) REFERENCES accounts(id),
FOREIGN KEY (\`recipient_account_id\`) REFERENCES accounts(id),
FOREIGN KEY (\`sender_community_id\`) REFERENCES communities(id),
FOREIGN KEY (\`recipient_community_id\`) REFERENCES communities(id)
FOREIGN KEY (\`community_id\`) REFERENCES communities(id),
FOREIGN KEY (\`other_community_id\`) REFERENCES communities(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
`,
)