import { ApiScope } from '@ulangi/ulangi-common/enums' ; import { User } from '@ulangi/ulangi-common/interfaces' ; import * as knex from 'knex' ; import * as _ from 'lodash' ; import { TableName } from '../enums/TableName' ; import { ApiKeyRow } from '../interfaces/ApiKeyRow' ; import { UserModel } from '../models/UserModel' ; import { ApiKeyRowResolver } from '../resolvers/ApiKeyRowResolver' ; import { promisifyQuery } from '../utils/promisifyQuery' ; export class ApiKeyModel { private apiKeyRowResolver = new ApiKeyRowResolver ( ) ; private userModel : UserModel ; public constructor ( userModel ) { this . userModel = userModel ; } public getValidApiKeyByUserIdAndScope ( db , userId , apiScope ) { return new Promise ( async ( resolve , reject ) : Promise < void > => { try { const result = await promisifyQuery ( db . select ( ) . from ( TableName . API_KEY ) . where ( { userId , apiScope } ) . whereRaw ( '(expiredAt IS NULL OR NOW() <= expiredAt)' ) . limit ( 1 ) ) ; const first = _ . first ( result ) ; if ( typeof first === 'undefined' ) { resolve ( null ) ; } else { const apiKeyRow = this . apiKeyRowResolver . resolve ( first , true ) ; resolve ( { apiKey : apiKeyRow . apiKey , expiredAt : apiKeyRow . expiredAt , } ) ; } resolve ( ) ; } catch ( error ) { reject ( error ) ; } } ) ; } public getUserByApiKeyAndScope ( db , apiKey , apiScope , stripUnknown ) { return new Promise ( async ( resolve , reject ) : Promise < void > => { try { const result = await promisifyQuery ( db . select ( ) . from ( TableName . API_KEY ) . where ( { apiKey , apiScope } ) . limit ( 1 ) ) ; const first = _ . first ( result ) ; if ( typeof first === 'undefined' ) { resolve ( null ) ; } else { const apiKeyRow = this . apiKeyRowResolver . resolve ( first , stripUnknown ) ; resolve ( await this . userModel . getUserById ( db , apiKeyRow . userId , stripUnknown ) ) ; } resolve ( ) ; } catch ( error ) { reject ( error ) ; } } ) ; } public isApiKeyBelongingToUser ( db , apiKey , userId ) { return new Promise ( async ( resolve , reject ) : Promise < void > => { try { const result = await promisifyQuery ( db . select ( ) . from ( TableName . API_KEY ) . where ( { apiKey , userId } ) . limit ( 1 ) ) ; resolve ( result . length > 0 ) ; resolve ( ) ; } catch ( error ) { reject ( error ) ; } } ) ; } public insertApiKey ( db , apiKeyRow ) { return new Promise ( async ( resolve , reject ) : Promise < void > => { try { await promisifyQuery ( db . insert ( apiKeyRow ) . into ( TableName . API_KEY ) ) ; resolve ( ) ; } catch ( error ) { reject ( error ) ; } } ) ; } public deleteApiKey ( db , apiKey ) { return new Promise ( async ( resolve , reject ) : Promise < void > => { try { await promisifyQuery ( db . delete ( ) . from ( TableName . API_KEY ) . where ( { apiKey } ) ) ; resolve ( ) ; } catch ( error ) { reject ( error ) ; } } ) ; } }