import uniqid from "uniqid" ; import { ApiKeyModel } from "../models/ApiKeyModel" ; import md5 from "md5" ; export async function createApiKey ( organization , description ? ) { const key = "key_" + md5 ( uniqid ( ) ) . substr ( 0 , 16 ) ; await ApiKeyModel . create ( { organization , key , description , dateCreated : new Date ( ) , } ) ; return key ; } export async function deleteByOrganizationAndApiKey ( organization , key ) { await ApiKeyModel . deleteOne ( { organization , key , } ) ; return ; } export async function lookupOrganizationByApiKey ( key ) { const doc = await ApiKeyModel . findOne ( { key , } ) ; if ( ! doc ) return null ; return doc . organization || null ; } export async function getAllApiKeysByOrganization ( organization ) { return ApiKeyModel . find ( { organization , } ) ; } export async function getFirstApiKey ( organization ) { return ApiKeyModel . findOne ( { organization , } ) ; }