mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #1610 from gradido/link-transaction-to-transaction-link
feat: Link Transaction to Transaction Link on Redeem
This commit is contained in:
commit
f127bb289b
@ -10,7 +10,7 @@ Decimal.set({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const constants = {
|
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
|
DECAY_START_TIME: new Date('2021-05-13 17:46:31'), // GMT+0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -30,6 +30,7 @@ export class Transaction {
|
|||||||
this.creationDate = transaction.creationDate
|
this.creationDate = transaction.creationDate
|
||||||
this.linkedUser = linkedUser
|
this.linkedUser = linkedUser
|
||||||
this.linkedTransactionId = transaction.linkedTransactionId
|
this.linkedTransactionId = transaction.linkedTransactionId
|
||||||
|
this.transactionLinkId = transaction.transactionLinkId
|
||||||
}
|
}
|
||||||
|
|
||||||
@Field(() => Number)
|
@Field(() => Number)
|
||||||
@ -67,4 +68,8 @@ export class Transaction {
|
|||||||
|
|
||||||
@Field(() => Number, { nullable: true })
|
@Field(() => Number, { nullable: true })
|
||||||
linkedTransactionId?: number | null
|
linkedTransactionId?: number | null
|
||||||
|
|
||||||
|
// Links to the TransactionLink when transaction was created by a link
|
||||||
|
@Field(() => Number, { nullable: true })
|
||||||
|
transactionLinkId?: number | null
|
||||||
}
|
}
|
||||||
|
|||||||
@ -156,14 +156,13 @@ export class TransactionLinkResolver {
|
|||||||
throw new Error('Transaction Link already redeemed.')
|
throw new Error('Transaction Link already redeemed.')
|
||||||
}
|
}
|
||||||
|
|
||||||
await executeTransaction(transactionLink.amount, transactionLink.memo, linkedUser, user)
|
await executeTransaction(
|
||||||
|
transactionLink.amount,
|
||||||
// TODO: Rollback transaction when updating links fails
|
transactionLink.memo,
|
||||||
transactionLink.redeemedAt = now
|
linkedUser,
|
||||||
transactionLink.redeemedBy = user.id
|
user,
|
||||||
transactionLink.save().catch(() => {
|
transactionLink,
|
||||||
throw new Error('Could not update transaction link.')
|
)
|
||||||
})
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import { TransactionLinkRepository } from '@repository/TransactionLink'
|
|||||||
|
|
||||||
import { User as dbUser } from '@entity/User'
|
import { User as dbUser } from '@entity/User'
|
||||||
import { Transaction as dbTransaction } from '@entity/Transaction'
|
import { Transaction as dbTransaction } from '@entity/Transaction'
|
||||||
|
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||||
|
|
||||||
import { apiPost } from '@/apis/HttpRequest'
|
import { apiPost } from '@/apis/HttpRequest'
|
||||||
import { TransactionTypeId } from '@enum/TransactionTypeId'
|
import { TransactionTypeId } from '@enum/TransactionTypeId'
|
||||||
@ -39,6 +40,7 @@ export const executeTransaction = async (
|
|||||||
memo: string,
|
memo: string,
|
||||||
sender: dbUser,
|
sender: dbUser,
|
||||||
recipient: dbUser,
|
recipient: dbUser,
|
||||||
|
transactionLink?: dbTransactionLink | null,
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
if (sender.id === recipient.id) {
|
if (sender.id === recipient.id) {
|
||||||
throw new Error('Sender and Recipient are the same.')
|
throw new Error('Sender and Recipient are the same.')
|
||||||
@ -67,6 +69,7 @@ export const executeTransaction = async (
|
|||||||
transactionSend.decay = sendBalance.decay.decay
|
transactionSend.decay = sendBalance.decay.decay
|
||||||
transactionSend.decayStart = sendBalance.decay.start
|
transactionSend.decayStart = sendBalance.decay.start
|
||||||
transactionSend.previous = sendBalance.lastTransactionId
|
transactionSend.previous = sendBalance.lastTransactionId
|
||||||
|
transactionSend.transactionLinkId = transactionLink ? transactionLink.id : null
|
||||||
await queryRunner.manager.insert(dbTransaction, transactionSend)
|
await queryRunner.manager.insert(dbTransaction, transactionSend)
|
||||||
|
|
||||||
const transactionReceive = new dbTransaction()
|
const transactionReceive = new dbTransaction()
|
||||||
@ -82,12 +85,23 @@ export const executeTransaction = async (
|
|||||||
transactionReceive.decayStart = receiveBalance ? receiveBalance.decay.start : null
|
transactionReceive.decayStart = receiveBalance ? receiveBalance.decay.start : null
|
||||||
transactionReceive.previous = receiveBalance ? receiveBalance.lastTransactionId : null
|
transactionReceive.previous = receiveBalance ? receiveBalance.lastTransactionId : null
|
||||||
transactionReceive.linkedTransactionId = transactionSend.id
|
transactionReceive.linkedTransactionId = transactionSend.id
|
||||||
|
transactionReceive.transactionLinkId = transactionLink ? transactionLink.id : null
|
||||||
await queryRunner.manager.insert(dbTransaction, transactionReceive)
|
await queryRunner.manager.insert(dbTransaction, transactionReceive)
|
||||||
|
|
||||||
// Save linked transaction id for send
|
// Save linked transaction id for send
|
||||||
transactionSend.linkedTransactionId = transactionReceive.id
|
transactionSend.linkedTransactionId = transactionReceive.id
|
||||||
await queryRunner.manager.update(dbTransaction, { id: transactionSend.id }, transactionSend)
|
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()
|
await queryRunner.commitTransaction()
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await queryRunner.rollbackTransaction()
|
await queryRunner.rollbackTransaction()
|
||||||
|
|||||||
@ -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
|
||||||
|
}
|
||||||
@ -1 +1 @@
|
|||||||
export { Transaction } from './0029-clean_transaction_table/Transaction'
|
export { Transaction } from './0032-add-transaction-link-to-transaction/Transaction'
|
||||||
|
|||||||
@ -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`;')
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user