import { Connection , createConnection , LessThan , MoreThan } from "typeorm" ; import { ApiKey } from "./entity/ApiKey" ; import { BN } from "avalanche" ; import { Transaction } from "./entity/Transaction" ; var md5 = require ( 'md5' ) ; const uuidAPIKey = require ( 'uuid-apikey' ) ; let dbConnection ; async function connectDB ( ) { let connection = await createConnection ( ) . catch ( ( e ) => { throw e ; } ) ; dbConnection = connection ; return connection ; } async function validateKey ( key ) { if ( ! dbConnection ) throw new Error ( "No database connection." ) ; let keyHash = md5 ( key ) ; let repo = await dbConnection . getRepository ( ApiKey ) ; let res = await repo . findOne ( { hash : keyHash } ) ; if ( ! res ) return false ; return true ; } async function addTransaction ( key , amount , to ) { if ( ! dbConnection ) throw new Error ( "No database connection." ) ; let tx = new Transaction ( ) ; tx . api_key = key ; tx . amount = amount . toNumber ( ) ; tx . to = to ; let res = await dbConnection . manager . save ( tx ) ; return res ; } async function getDailyUsage ( key ) { if ( ! dbConnection ) throw new Error ( "No database connection." ) ; let now = Date . now ( ) ; const HOUR_MS = 1000 * 60 * 60 ; const DAY_MS = HOUR_MS * 24 ; let yesterday = new Date ( now - DAY_MS ) ; let repo = await dbConnection . getRepository ( Transaction ) ; let res = await repo . find ( { where : { api_key : key , createdAt : MoreThan ( yesterday ) } } ) ; let sum = res . reduce ( ( acc , tx ) => { return acc . add ( new BN ( tx . amount ) ) ; } , new BN ( 0 ) ) ; return sum ; } async function getApiKey ( key ) { if ( ! dbConnection ) throw new Error ( "No database connection." ) ; let keyHash = md5 ( key ) ; let repo = await dbConnection . getRepository ( ApiKey ) ; let res = await repo . findOne ( { hash : keyHash } ) ; if ( ! res ) return null ; return res ; } async function createApiKey ( name , dailyLimit ) { if ( ! dbConnection ) throw new Error ( "No database connection." ) ; let uuidKey = uuidAPIKey . create ( ) ; let apiKey = uuidKey . apiKey ; let hash = md5 ( apiKey ) ; const key = new ApiKey ( ) ; key . name = name ; key . hash = hash ; key . daily_limit = dailyLimit ; let res = await dbConnection . manager . save ( key ) ; return apiKey ; } export { connectDB , createApiKey , validateKey , getApiKey , addTransaction , getDailyUsage } ;