mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
try to get repository working again
This commit is contained in:
parent
95e7888d90
commit
642d2202bf
@ -6,7 +6,7 @@ import { Decimal } from 'decimal.js-light'
|
|||||||
import { Resolver, Query, Ctx, Authorized } from 'type-graphql'
|
import { Resolver, Query, Ctx, Authorized } from 'type-graphql'
|
||||||
|
|
||||||
import { Balance } from '@model/Balance'
|
import { Balance } from '@model/Balance'
|
||||||
import { TransactionLinkRepository } from '@repository/TransactionLink'
|
import { transactionLinkRepository } from '@repository/TransactionLink'
|
||||||
|
|
||||||
import { RIGHTS } from '@/auth/RIGHTS'
|
import { RIGHTS } from '@/auth/RIGHTS'
|
||||||
import { Context, getUser } from '@/server/context'
|
import { Context, getUser } from '@/server/context'
|
||||||
@ -77,7 +77,6 @@ export class BalanceResolver {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// The final balance is reduced by the link amount withheld
|
// The final balance is reduced by the link amount withheld
|
||||||
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
|
||||||
const { sumHoldAvailableAmount } = context.sumHoldAvailableAmount
|
const { sumHoldAvailableAmount } = context.sumHoldAvailableAmount
|
||||||
? { sumHoldAvailableAmount: context.sumHoldAvailableAmount }
|
? { sumHoldAvailableAmount: context.sumHoldAvailableAmount }
|
||||||
: await transactionLinkRepository.summary(user.id, now)
|
: await transactionLinkRepository.summary(user.id, now)
|
||||||
|
|||||||
@ -76,14 +76,22 @@ export class TransactionLinkResolver {
|
|||||||
throw new LogError('Amount must be a positive number', amount)
|
throw new LogError('Amount must be a positive number', amount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Hallo')
|
||||||
|
|
||||||
const holdAvailableAmount = amount.minus(calculateDecay(amount, createdDate, validUntil).decay)
|
const holdAvailableAmount = amount.minus(calculateDecay(amount, createdDate, validUntil).decay)
|
||||||
|
|
||||||
|
console.log(holdAvailableAmount)
|
||||||
|
|
||||||
// validate amount
|
// validate amount
|
||||||
const sendBalance = await calculateBalance(user.id, holdAvailableAmount.mul(-1), createdDate)
|
const sendBalance = await calculateBalance(user.id, holdAvailableAmount.mul(-1), createdDate)
|
||||||
|
console.log('sendBalance', sendBalance)
|
||||||
|
|
||||||
if (!sendBalance) {
|
if (!sendBalance) {
|
||||||
throw new LogError('User has not enough GDD', user.id)
|
throw new LogError('User has not enough GDD', user.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(sendBalance)
|
||||||
|
|
||||||
const transactionLink = DbTransactionLink.create()
|
const transactionLink = DbTransactionLink.create()
|
||||||
transactionLink.userId = user.id
|
transactionLink.userId = user.id
|
||||||
transactionLink.amount = amount
|
transactionLink.amount = amount
|
||||||
|
|||||||
@ -16,7 +16,7 @@ import { TransactionTypeId } from '@enum/TransactionTypeId'
|
|||||||
import { Transaction } from '@model/Transaction'
|
import { Transaction } from '@model/Transaction'
|
||||||
import { TransactionList } from '@model/TransactionList'
|
import { TransactionList } from '@model/TransactionList'
|
||||||
import { User } from '@model/User'
|
import { User } from '@model/User'
|
||||||
import { TransactionLinkRepository } from '@repository/TransactionLink'
|
import { transactionLinkRepository } from '@repository/TransactionLink'
|
||||||
|
|
||||||
import { RIGHTS } from '@/auth/RIGHTS'
|
import { RIGHTS } from '@/auth/RIGHTS'
|
||||||
import {
|
import {
|
||||||
@ -245,7 +245,6 @@ export class TransactionResolver {
|
|||||||
const self = new User(user)
|
const self = new User(user)
|
||||||
const transactions: Transaction[] = []
|
const transactions: Transaction[] = []
|
||||||
|
|
||||||
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
|
||||||
const { sumHoldAvailableAmount, sumAmount, lastDate, firstDate, transactionLinkcount } =
|
const { sumHoldAvailableAmount, sumAmount, lastDate, firstDate, transactionLinkcount } =
|
||||||
await transactionLinkRepository.summary(user.id, now)
|
await transactionLinkRepository.summary(user.id, now)
|
||||||
context.linkCount = transactionLinkcount
|
context.linkCount = transactionLinkcount
|
||||||
|
|||||||
@ -24,12 +24,15 @@ export const transactionLinkFactory = async (
|
|||||||
memo: transactionLink.memo,
|
memo: transactionLink.memo,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const result = await mutate({ mutation: createTransactionLink, variables })
|
||||||
|
console.log(result)
|
||||||
|
|
||||||
// get the transaction links's id
|
// get the transaction links's id
|
||||||
const {
|
const {
|
||||||
data: {
|
data: {
|
||||||
createTransactionLink: { id },
|
createTransactionLink: { id },
|
||||||
},
|
},
|
||||||
} = await mutate({ mutation: createTransactionLink, variables })
|
} = result
|
||||||
|
|
||||||
if (transactionLink.createdAt || transactionLink.deletedAt) {
|
if (transactionLink.createdAt || transactionLink.deletedAt) {
|
||||||
const dbTransactionLink = await TransactionLink.findOneOrFail({ where: { id } })
|
const dbTransactionLink = await TransactionLink.findOneOrFail({ where: { id } })
|
||||||
|
|||||||
@ -52,6 +52,7 @@ const resetEntity = async (entity: any) => {
|
|||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
const server = await createServer(context)
|
const server = await createServer(context)
|
||||||
|
console.log(server)
|
||||||
const seedClient = createTestClient(server.apollo)
|
const seedClient = createTestClient(server.apollo)
|
||||||
const { con } = server
|
const { con } = server
|
||||||
await cleanDB()
|
await cleanDB()
|
||||||
|
|||||||
@ -37,6 +37,7 @@ export const createServer = async (
|
|||||||
logger.debug('createServer...')
|
logger.debug('createServer...')
|
||||||
|
|
||||||
// open mysql connection
|
// open mysql connection
|
||||||
|
console.log('Connection.getInstance')
|
||||||
const con = await Connection.getInstance()
|
const con = await Connection.getInstance()
|
||||||
if (!con?.isConnected) {
|
if (!con?.isConnected) {
|
||||||
logger.fatal(`Couldn't open connection to database!`)
|
logger.fatal(`Couldn't open connection to database!`)
|
||||||
|
|||||||
@ -1,41 +1,42 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||||
import { Repository, EntityRepository } from '@dbTools/typeorm'
|
import { getConnection } from '@dbTools/typeorm'
|
||||||
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||||
import { Decimal } from 'decimal.js-light'
|
import { Decimal } from 'decimal.js-light'
|
||||||
|
|
||||||
@EntityRepository(dbTransactionLink)
|
export const transactionLinkRepository = getConnection()
|
||||||
export class TransactionLinkRepository extends Repository<dbTransactionLink> {
|
.getRepository(dbTransactionLink)
|
||||||
async summary(
|
.extend({
|
||||||
userId: number,
|
async summary(
|
||||||
date: Date,
|
userId: number,
|
||||||
): Promise<{
|
date: Date,
|
||||||
sumHoldAvailableAmount: Decimal
|
): Promise<{
|
||||||
sumAmount: Decimal
|
sumHoldAvailableAmount: Decimal
|
||||||
lastDate: Date | null
|
sumAmount: Decimal
|
||||||
firstDate: Date | null
|
lastDate: Date | null
|
||||||
transactionLinkcount: number
|
firstDate: Date | null
|
||||||
}> {
|
transactionLinkcount: number
|
||||||
const { sumHoldAvailableAmount, sumAmount, lastDate, firstDate, count } =
|
}> {
|
||||||
await this.createQueryBuilder('transactionLinks')
|
const { sumHoldAvailableAmount, sumAmount, lastDate, firstDate, count } =
|
||||||
.select('SUM(transactionLinks.holdAvailableAmount)', 'sumHoldAvailableAmount')
|
await this.createQueryBuilder('transactionLinks')
|
||||||
.addSelect('SUM(transactionLinks.amount)', 'sumAmount')
|
.select('SUM(transactionLinks.holdAvailableAmount)', 'sumHoldAvailableAmount')
|
||||||
.addSelect('MAX(transactionLinks.validUntil)', 'lastDate')
|
.addSelect('SUM(transactionLinks.amount)', 'sumAmount')
|
||||||
.addSelect('MIN(transactionLinks.createdAt)', 'firstDate')
|
.addSelect('MAX(transactionLinks.validUntil)', 'lastDate')
|
||||||
.addSelect('COUNT(*)', 'count')
|
.addSelect('MIN(transactionLinks.createdAt)', 'firstDate')
|
||||||
.where('transactionLinks.userId = :userId', { userId })
|
.addSelect('COUNT(*)', 'count')
|
||||||
.andWhere('transactionLinks.redeemedAt is NULL')
|
.where('transactionLinks.userId = :userId', { userId })
|
||||||
.andWhere('transactionLinks.validUntil > :date', { date })
|
.andWhere('transactionLinks.redeemedAt is NULL')
|
||||||
.orderBy('transactionLinks.createdAt', 'DESC')
|
.andWhere('transactionLinks.validUntil > :date', { date })
|
||||||
.getRawOne()
|
.orderBy('transactionLinks.createdAt', 'DESC')
|
||||||
return {
|
.getRawOne()
|
||||||
sumHoldAvailableAmount: sumHoldAvailableAmount
|
return {
|
||||||
? new Decimal(sumHoldAvailableAmount)
|
sumHoldAvailableAmount: sumHoldAvailableAmount
|
||||||
: new Decimal(0),
|
? new Decimal(sumHoldAvailableAmount)
|
||||||
sumAmount: sumAmount ? new Decimal(sumAmount) : new Decimal(0),
|
: new Decimal(0),
|
||||||
lastDate: lastDate || null,
|
sumAmount: sumAmount ? new Decimal(sumAmount) : new Decimal(0),
|
||||||
firstDate: firstDate || null,
|
lastDate: lastDate || null,
|
||||||
transactionLinkcount: count || 0,
|
firstDate: firstDate || null,
|
||||||
}
|
transactionLinkcount: count || 0,
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
})
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
|||||||
import { Decimal } from 'decimal.js-light'
|
import { Decimal } from 'decimal.js-light'
|
||||||
|
|
||||||
import { Decay } from '@model/Decay'
|
import { Decay } from '@model/Decay'
|
||||||
import { TransactionLinkRepository } from '@repository/TransactionLink'
|
import { transactionLinkRepository } from '@repository/TransactionLink'
|
||||||
|
|
||||||
import { getLastTransaction } from '@/graphql/resolver/util/getLastTransaction'
|
import { getLastTransaction } from '@/graphql/resolver/util/getLastTransaction'
|
||||||
|
|
||||||
@ -26,10 +26,11 @@ async function calculateBalance(
|
|||||||
const lastTransaction = await getLastTransaction(userId)
|
const lastTransaction = await getLastTransaction(userId)
|
||||||
if (!lastTransaction) return null
|
if (!lastTransaction) return null
|
||||||
|
|
||||||
|
console.log(lastTransaction)
|
||||||
|
|
||||||
const decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, time)
|
const decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, time)
|
||||||
|
|
||||||
const balance = decay.balance.add(amount.toString())
|
const balance = decay.balance.add(amount.toString())
|
||||||
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
|
||||||
const { sumHoldAvailableAmount } = await transactionLinkRepository.summary(userId, time)
|
const { sumHoldAvailableAmount } = await transactionLinkRepository.summary(userId, time)
|
||||||
|
|
||||||
// If we want to redeem a link we need to make sure that the link amount is not considered as blocked
|
// If we want to redeem a link we need to make sure that the link amount is not considered as blocked
|
||||||
|
|||||||
@ -41,7 +41,7 @@ export const testEnvironment = async (testLogger = logger, testI18n = i18n) => {
|
|||||||
return { mutate, query, con }
|
return { mutate, query, con }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const resetEntity = async (entity: anny) => {
|
export const resetEntity = async (entity: any) => {
|
||||||
const items = await entity.find({ withDeleted: true })
|
const items = await entity.find({ withDeleted: true })
|
||||||
if (items.length > 0) {
|
if (items.length > 0) {
|
||||||
const ids = items.map((e: any) => e.id)
|
const ids = items.map((e: any) => e.id)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user