import mongoose = require ( 'mongoose' ) ; import FcmLog from '@app/core/fcm/model/FcmLog' ; var FcmLogSchema = new mongoose . Schema ( { date : Date , author : String , to : String , message : String , cause : String , response : String } ) ; FcmLogSchema . index ( { date : - 1 } ) ; var mongooseModel = mongoose . model ( 'FcmLog' , FcmLogSchema , 'fcmlogs' ) ; export async function insertFcmLog ( fcmLog ) { var log = new mongooseModel ( fcmLog ) ; log . save ( ) ; } export async function findRecentFcmLog ( ) { let mongoDocs = await mongooseModel . find ( ) . sort ( { date : - 1 } ) . limit ( 10 ) . exec ( ) ; return mongoDocs . map ( fromMongooseModel ) ; } export function deleteBeforeDate ( date ) { return mongooseModel . deleteMany ( { date : { $lt : date } } ) . exec ( ) ; } function fromMongooseModel ( mongooseDoc ) { let fcmLog = { date : mongooseDoc . date , author : mongooseDoc . author , cause : mongooseDoc . cause , to : mongooseDoc . to , message : mongooseDoc . message , response : mongooseDoc . response } ; return fcmLog ; }