Merge pull request #1610 from gradido/link-transaction-to-transaction-link

feat: Link Transaction to Transaction Link on Redeem
This commit is contained in:
Moriz Wahl 2022-03-11 17:10:05 +01:00 committed by GitHub
commit f127bb289b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 10 deletions

View File

@ -10,7 +10,7 @@ Decimal.set({
})
const constants = {
DB_VERSION: '0031-remove_sendEmail_from_transaction_link',
DB_VERSION: '0032-add-transaction-link-to-transaction',
DECAY_START_TIME: new Date('2021-05-13 17:46:31'), // GMT+0
}

View File

@ -30,6 +30,7 @@ export class Transaction {
this.creationDate = transaction.creationDate
this.linkedUser = linkedUser
this.linkedTransactionId = transaction.linkedTransactionId
this.transactionLinkId = transaction.transactionLinkId
}
@Field(() => Number)
@ -67,4 +68,8 @@ export class Transaction {
@Field(() => Number, { nullable: true })
linkedTransactionId?: number | null
// Links to the TransactionLink when transaction was created by a link
@Field(() => Number, { nullable: true })
transactionLinkId?: number | null
}

View File

@ -156,14 +156,13 @@ export class TransactionLinkResolver {
throw new Error('Transaction Link already redeemed.')
}
await executeTransaction(transactionLink.amount, transactionLink.memo, linkedUser, user)
// TODO: Rollback transaction when updating links fails
transactionLink.redeemedAt = now
transactionLink.redeemedBy = user.id
transactionLink.save().catch(() => {
throw new Error('Could not update transaction link.')
})
await executeTransaction(
transactionLink.amount,
transactionLink.memo,
linkedUser,
user,
transactionLink,
)
return true
}

View File

@ -23,6 +23,7 @@ import { TransactionLinkRepository } from '@repository/TransactionLink'
import { User as dbUser } from '@entity/User'
import { Transaction as dbTransaction } from '@entity/Transaction'
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
import { apiPost } from '@/apis/HttpRequest'
import { TransactionTypeId } from '@enum/TransactionTypeId'
@ -39,6 +40,7 @@ export const executeTransaction = async (
memo: string,
sender: dbUser,
recipient: dbUser,
transactionLink?: dbTransactionLink | null,
): Promise<boolean> => {
if (sender.id === recipient.id) {
throw new Error('Sender and Recipient are the same.')
@ -67,6 +69,7 @@ export const executeTransaction = async (
transactionSend.decay = sendBalance.decay.decay
transactionSend.decayStart = sendBalance.decay.start
transactionSend.previous = sendBalance.lastTransactionId
transactionSend.transactionLinkId = transactionLink ? transactionLink.id : null
await queryRunner.manager.insert(dbTransaction, transactionSend)
const transactionReceive = new dbTransaction()
@ -82,12 +85,23 @@ export const executeTransaction = async (
transactionReceive.decayStart = receiveBalance ? receiveBalance.decay.start : null
transactionReceive.previous = receiveBalance ? receiveBalance.lastTransactionId : null
transactionReceive.linkedTransactionId = transactionSend.id
transactionReceive.transactionLinkId = transactionLink ? transactionLink.id : null
await queryRunner.manager.insert(dbTransaction, transactionReceive)
// Save linked transaction id for send
transactionSend.linkedTransactionId = transactionReceive.id
await queryRunner.manager.update(dbTransaction, { id: transactionSend.id }, transactionSend)
if (transactionLink) {
transactionLink.redeemedAt = receivedCallDate
transactionLink.redeemedBy = recipient.id
await queryRunner.manager.update(
dbTransactionLink,
{ id: transactionLink.id },
transactionLink,
)
}
await queryRunner.commitTransaction()
} catch (e) {
await queryRunner.rollbackTransaction()

View File

@ -0,0 +1,94 @@
import Decimal from 'decimal.js-light'
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
import { DecimalTransformer } from '../../src/typeorm/DecimalTransformer'
@Entity('transactions')
export class Transaction extends BaseEntity {
@PrimaryGeneratedColumn('increment', { unsigned: true })
id: number
@Column({ name: 'user_id', unsigned: true, nullable: false })
userId: number
@Column({ type: 'int', unsigned: true, nullable: true, default: null })
previous: number | null
@Column({ name: 'type_id', unsigned: true, nullable: false })
typeId: number
@Column({
type: 'decimal',
precision: 40,
scale: 20,
nullable: false,
transformer: DecimalTransformer,
})
amount: Decimal
@Column({
type: 'decimal',
precision: 40,
scale: 20,
nullable: false,
transformer: DecimalTransformer,
})
balance: Decimal
@Column({
name: 'balance_date',
type: 'datetime',
default: () => 'CURRENT_TIMESTAMP',
nullable: false,
})
balanceDate: Date
@Column({
type: 'decimal',
precision: 40,
scale: 20,
nullable: false,
transformer: DecimalTransformer,
})
decay: Decimal
@Column({
name: 'decay_start',
type: 'datetime',
nullable: true,
default: null,
})
decayStart: Date | null
@Column({ length: 255, nullable: false, collation: 'utf8mb4_unicode_ci' })
memo: string
@Column({ name: 'creation_date', type: 'datetime', nullable: true, default: null })
creationDate: Date | null
@Column({
name: 'linked_user_id',
type: 'int',
unsigned: true,
nullable: true,
default: null,
})
linkedUserId?: number | null
@Column({
name: 'linked_transaction_id',
type: 'int',
unsigned: true,
nullable: true,
default: null,
})
linkedTransactionId?: number | null
@Column({
name: 'transaction_link_id',
type: 'int',
unsigned: true,
nullable: true,
default: null,
})
transactionLinkId?: number | null
}

View File

@ -1 +1 @@
export { Transaction } from './0029-clean_transaction_table/Transaction'
export { Transaction } from './0032-add-transaction-link-to-transaction/Transaction'

View File

@ -0,0 +1,14 @@
/* MIGRATION TO ADD transactionLinkId FIELDTO TRANSACTION */
/* 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>>) {
await queryFn(
'ALTER TABLE `transactions` ADD COLUMN `transaction_link_id` int UNSIGNED DEFAULT NULL AFTER `linked_transaction_id`;',
)
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
await queryFn('ALTER TABLE `transactions` DROP COLUMN `transaction_link_id`;')
}