mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Balance Model
This commit is contained in:
parent
165f86bdb1
commit
9ff3632021
@ -2,9 +2,10 @@
|
|||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
import { Resolver, Query, Ctx, Authorized } from 'type-graphql'
|
import { Resolver, Query, Ctx, Authorized } from 'type-graphql'
|
||||||
|
import { getCustomRepository } from 'typeorm'
|
||||||
import { Balance } from '../models/Balance'
|
import { Balance } from '../models/Balance'
|
||||||
import { User as dbUser } from '../../typeorm/entity/User'
|
import { BalanceRepository } from '../../typeorm/repository/Balance'
|
||||||
import { Balance as dbBalance } from '../../typeorm/entity/Balance'
|
import { UserRepository } from '../../typeorm/repository/User'
|
||||||
import { calculateDecay } from '../../util/decay'
|
import { calculateDecay } from '../../util/decay'
|
||||||
import { roundFloorFrom4 } from '../../util/round'
|
import { roundFloorFrom4 } from '../../util/round'
|
||||||
|
|
||||||
@ -14,8 +15,11 @@ export class BalanceResolver {
|
|||||||
@Query(() => Balance)
|
@Query(() => Balance)
|
||||||
async balance(@Ctx() context: any): Promise<Balance> {
|
async balance(@Ctx() context: any): Promise<Balance> {
|
||||||
// load user and balance
|
// load user and balance
|
||||||
const userEntity = await dbUser.findByPubkeyHex(context.pubKey)
|
const balanceRepository = getCustomRepository(BalanceRepository)
|
||||||
const balanceEntity = await dbBalance.findByUser(userEntity.id)
|
const userRepository = getCustomRepository(UserRepository)
|
||||||
|
|
||||||
|
const userEntity = await userRepository.findByPubkeyHex(context.pubKey)
|
||||||
|
const balanceEntity = await balanceRepository.findByUser(userEntity.id)
|
||||||
let balance: Balance
|
let balance: Balance
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
if (balanceEntity) {
|
if (balanceEntity) {
|
||||||
|
|||||||
@ -16,8 +16,4 @@ export class Balance extends BaseEntity {
|
|||||||
|
|
||||||
@Column({ type: 'bigint' })
|
@Column({ type: 'bigint' })
|
||||||
amount: number
|
amount: number
|
||||||
|
|
||||||
static findByUser(userId: number): Promise<Balance | undefined> {
|
|
||||||
return this.createQueryBuilder('balance').where('balance.userId = :userId', { userId }).getOne()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
11
backend/src/typeorm/repository/Balance.ts
Normal file
11
backend/src/typeorm/repository/Balance.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { EntityRepository, Repository } from 'typeorm'
|
||||||
|
import { Balance } from '../entity/Balance'
|
||||||
|
|
||||||
|
@EntityRepository(Balance)
|
||||||
|
export class BalanceRepository extends Repository<Balance> {
|
||||||
|
findByUser(userId: number): Promise<Balance | undefined> {
|
||||||
|
return this.createQueryBuilder('balance')
|
||||||
|
.where('balance.userId = :userId', { userId })
|
||||||
|
.getOneOrFail()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user