import * as mongoose from 'mongoose' ; import { IUnitModel , Unit } from '../../models/units/Unit' ; import { ChatRoom } from '../../models/ChatRoom' ; class UnitV2Migration { async up ( ) { console . log ( 'Unit V2 up was called' ) ; try { const units = await Unit . find ( { 'chatRoom' : { $exists : false } } ) ; await Promise . all ( units . map ( async ( unit ) => { const unitObj = unit . toObject ( ) ; const chatRoom = await ChatRoom . create ( { room : { roomType : 'Unit' , roomFor : unit . _id } } ) ; const chatRoomObj = chatRoom . toObject ( ) ; unitObj . chatRoom = chatRoomObj . _id ; unitObj . _id = mongoose . Types . ObjectId ( unitObj . _id ) ; await mongoose . connection . collection ( 'units' ) . findOneAndReplace ( { '_id' : unit . _id } , unitObj ) ; } ) ) ; } catch ( error ) { console . log ( '1: ' + error ) ; } console . log ( 'Chat rooms have been successfully added to units!' ) ; return true ; } } export = UnitV2Migration ;