import mongoose = require ( 'mongoose' ) ; import winston = require ( 'winston' ) ; import User from '@app/core/user/model/User' ; var logger = winston . loggers . get ( 'default' ) ; let UserSchema = new mongoose . Schema ( { credential : { localId : { type : String , default : null } , localPw : { type : String , default : null } , fbName : { type : String , default : null } , fbId : { type : String , default : null } , appleEmail : { type : String , default : null } , appleSub : { type : String , default : null } , tempDate : { type : Date , default : null } , tempSeed : { type : Number , default : null } } , credentialHash : { type : String , default : null } , isAdmin : { type : Boolean , default : false } , regDate : Date , lastLoginTimestamp : Number , notificationCheckedAt : Date , email : String , fcmKey : String , active : { type : Boolean , default : true } } ) ; UserSchema . index ( { credentialHash : 1 } ) ; UserSchema . index ( { "credential.localId" : 1 } ) ; UserSchema . index ( { "credential.fbId" : 1 } ) ; let MongooseUserModel = mongoose . model ( 'User' , UserSchema , 'users' ) ; function fromMongoose ( mongooseDocument ) { if ( mongooseDocument === null ) { return null ; } let wrapper = < any > mongooseDocument ; return { _id : wrapper . _id , credential : wrapper . credential , credentialHash : wrapper . credentialHash , isAdmin : wrapper . isAdmin , regDate : wrapper . regDate , notificationCheckedAt : wrapper . notificationCheckedAt , email : wrapper . email , fcmKey : wrapper . fcmKey , active : wrapper . active , lastLoginTimestamp : wrapper . lastLoginTimestamp } ; } export async function findActiveByFb ( fbId ) { let mongooseDocument = await MongooseUserModel . findOne ( { 'credential.fbId' : fbId , 'active' : true } ) . exec ( ) ; return fromMongoose ( mongooseDocument ) ; } export async function findActiveByApple ( appleEmail ) { const mongooseDocument = await MongooseUserModel . findOne ( { 'credential.appleEmail' : appleEmail , 'active' : true } ) . exec ( ) ; return fromMongoose ( mongooseDocument ) ; } export async function findActiveByCredentialHash ( hash ) { let mongooseDocument = await MongooseUserModel . findOne ( { 'credentialHash' : hash , 'active' : true } ) . exec ( ) ; return fromMongoose ( mongooseDocument ) ; } export function findActiveByMongooseId ( mid ) { return MongooseUserModel . findOne ( { '_id' : mid , 'active' : true } ) . exec ( ) . then ( function ( userDocument ) { return fromMongoose ( userDocument ) ; } ) ; } export function findActiveByLocalId ( id ) { return MongooseUserModel . findOne ( { 'credential.localId' : id , 'active' : true } ) . exec ( ) . then ( function ( userDocument ) { return fromMongoose ( userDocument ) ; } ) ; } export function update ( user ) { return MongooseUserModel . findOne ( { '_id' : user . _id } ) . exec ( ) . then ( function ( userDocument ) { userDocument . credential = user . credential ; userDocument . credentialHash = user . credentialHash ; userDocument . isAdmin = user . isAdmin ; userDocument . regDate = user . regDate ; userDocument . notificationCheckedAt = user . notificationCheckedAt ; userDocument . email = user . email ; userDocument . fcmKey = user . fcmKey ; userDocument . active = user . active ; userDocument . lastLoginTimestamp = user . lastLoginTimestamp ; userDocument . save ( ) ; } ) ; } export async function insert ( user ) { let mongooseUserModel = new MongooseUserModel ( user ) ; await mongooseUserModel . save ( ) ; return fromMongoose ( mongooseUserModel ) ; } export function updateLastLoginTimestamp ( user ) { let timestamp = Date . now ( ) ; mongoose . connection . db . collection ( 'users' ) . updateOne ( { _id : user . _id } , { $set : { lastLoginTimestamp : timestamp } } ) . catch ( function ( err ) { logger . error ( "Failed to update timestamp" ) ; logger . error ( err ) ; } ) ; user . lastLoginTimestamp = timestamp ; }