import { Document } from 'mongoose' ; import * as redis from '../redis' ; import Entry , { IEntryModel , IEntrySchema } from '../models/Entry' ; import { IEntryQuery } from '../types' ; import { getEntryUpdates , stringifyValues } from '../utils' ; export const getNonBots = ( ) => Entry . getNonBots ( ) ; export const findOne = async ( query ) => { const queryString = stringifyValues ( query ) ; if ( query . status ) { const cachedEntry = await redis . get ( queryString ) ; if ( cachedEntry ) return JSON . parse ( cachedEntry ) ; } const entry = await Entry . findOne ( query ) . lean ( ) ; if ( entry && query . status ) { await redis . set ( queryString , JSON . stringify ( entry ) , 'EX' , 60 * 60 * 1 ) ; } return entry ; } ; export const findById = ( entry ) => Entry . findById ( entry ) . lean ( ) ; export const create = ( entry ) => Entry . create ( entry ) ; export const update = async ( username , body , isAdmin = false ) => { const updates = getEntryUpdates ( body , isAdmin ) ; const entry = await Entry . findOneAndUpdate ( { username } , updates , { new : true , runValidators : true , setDefaultsOnInsert : true , } ) ; const queryString = stringifyValues ( { status : entry . status , username : entry . username , } ) ; await redis . set ( queryString , JSON . stringify ( entry ) , 'EX' , 60 * 60 * 1 ) ; return entry ; } ; export const get = async ( query ) => Entry . getEntries ( query ) ;