diff --git a/dlt-connector/src/config/index.ts b/dlt-connector/src/config/index.ts index fc8c780b8..c129af2ea 100644 --- a/dlt-connector/src/config/index.ts +++ b/dlt-connector/src/config/index.ts @@ -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: { diff --git a/dlt-connector/src/data/Transaction.builder.ts b/dlt-connector/src/data/Transaction.builder.ts index 321dda76b..23820bb4d 100644 --- a/dlt-connector/src/data/Transaction.builder.ts +++ b/dlt-connector/src/data/Transaction.builder.ts @@ -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 { // 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 { // 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( diff --git a/dlt-connector/src/graphql/model/TransactionRecipe.ts b/dlt-connector/src/graphql/model/TransactionRecipe.ts index 3b37ba4ea..a4dacd9e9 100644 --- a/dlt-connector/src/graphql/model/TransactionRecipe.ts +++ b/dlt-connector/src/graphql/model/TransactionRecipe.ts @@ -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) diff --git a/dlt-connector/src/interactions/backendToDb/transaction/CommunityRootTransaction.role.ts b/dlt-connector/src/interactions/backendToDb/transaction/CommunityRootTransaction.role.ts index aa09f89b2..240c86dc4 100644 --- a/dlt-connector/src/interactions/backendToDb/transaction/CommunityRootTransaction.role.ts +++ b/dlt-connector/src/interactions/backendToDb/transaction/CommunityRootTransaction.role.ts @@ -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))) diff --git a/dlt-connector/src/interactions/backendToDb/transaction/TransactionRecipe.role.ts b/dlt-connector/src/interactions/backendToDb/transaction/TransactionRecipe.role.ts index caebb4fc4..6b59e5eaa 100644 --- a/dlt-connector/src/interactions/backendToDb/transaction/TransactionRecipe.role.ts +++ b/dlt-connector/src/interactions/backendToDb/transaction/TransactionRecipe.role.ts @@ -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 } diff --git a/dlt-connector/src/typeorm/DataSource.ts b/dlt-connector/src/typeorm/DataSource.ts index 62cdbf755..a5db6443d 100644 --- a/dlt-connector/src/typeorm/DataSource.ts +++ b/dlt-connector/src/typeorm/DataSource.ts @@ -71,12 +71,16 @@ export class Connection { } async checkDBVersion(DB_VERSION: string): Promise { - 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' }`, ) } diff --git a/dlt-database/entity/0003-refactor_transaction_recipe/Community.ts b/dlt-database/entity/0003-refactor_transaction_recipe/Community.ts index f403064b4..1233d0832 100644 --- a/dlt-database/entity/0003-refactor_transaction_recipe/Community.ts +++ b/dlt-database/entity/0003-refactor_transaction_recipe/Community.ts @@ -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[] } diff --git a/dlt-database/entity/0003-refactor_transaction_recipe/Transaction.ts b/dlt-database/entity/0003-refactor_transaction_recipe/Transaction.ts index e9d8c6a0e..06c06927b 100644 --- a/dlt-database/entity/0003-refactor_transaction_recipe/Transaction.ts +++ b/dlt-database/entity/0003-refactor_transaction_recipe/Transaction.ts @@ -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', diff --git a/dlt-database/migrations/0003-refactor_transaction_recipe.ts b/dlt-database/migrations/0003-refactor_transaction_recipe.ts index cf5657485..fc842d49d 100644 --- a/dlt-database/migrations/0003-refactor_transaction_recipe.ts +++ b/dlt-database/migrations/0003-refactor_transaction_recipe.ts @@ -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; `, )