mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
28 lines
935 B
TypeScript
28 lines
935 B
TypeScript
import { User as dbUser } from '../typeorm/entity/User'
|
|
import { Balance as dbBalance } from '../typeorm/entity/Balance'
|
|
import { getRepository } from 'typeorm'
|
|
import { calculateDecay } from './decay'
|
|
|
|
function isStringBoolean(value: string): boolean {
|
|
const lowerValue = value.toLowerCase()
|
|
if (lowerValue === 'true' || lowerValue === 'false') {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
function isHexPublicKey(publicKey:string): boolean {
|
|
return /^[0-9A-Fa-f]{64}$/i.test(publicKey)
|
|
}
|
|
|
|
async function hasUserAmount(user:dbUser, amount:number): Promise<boolean> {
|
|
if(amount < 0) return false
|
|
const balanceRepository = getRepository(dbBalance)
|
|
const balance = await balanceRepository.findOne({ userId: user.id })
|
|
if(!balance) return false
|
|
|
|
const decay = await calculateDecay(balance.amount, balance.recordDate, new Date())
|
|
return decay > amount
|
|
}
|
|
|
|
export { isHexPublicKey, hasUserAmount, isStringBoolean } |