 			jobs = users.map (u) ->
 				(cb) ->
 					console.log "[Removing user #{i++}/#{users.length}]"
-					HoldingAccountMigration.deleteUser u._id, (error) ->
+					HoldingAccountMigration.migrateGroupInvites u._id, u.email, (error) ->
 						return cb(error) if error?
-						HoldingAccountMigration.deleteUserProjects u._id, (error) ->
+						HoldingAccountMigration.deleteUser u._id, (error) ->
 							return cb(error) if error?
-							setTimeout cb, 50 # Small delay to not hammer DB
+							HoldingAccountMigration.deleteUserProjects u._id, (error) ->
+								return cb(error) if error?
+								setTimeout cb, 50 # Small delay to not hammer DB
 			async.series jobs, (error) ->
 				throw error if error?
 				console.log "[FINISHED]"

		else
			console.log "[Would have removed user]", user_id
			callback()

	run: (done = () ->) ->
		console.log "[Getting list of holding accounts]"

Settings = require "settings-sharelatex"
mongojs = require("mongojs")
ObjectId = mongojs.ObjectId
db = mongojs(Settings.mongo.url, ['users', 'projects', 'subscriptions'])
async = require "async"

module.exports = HoldingAccountMigration = 
