DoSomething/ds-mdata-responder — pull request #182 by tongxiang (2014-09-25 19:46:30+00:00)
Title: User kicked out fix

## What's this PR do?
PR-138 was written to cover an edge case--when a user is currently in game 1, and gets invited to and joins game 2, the other users in game 1 get thrown under the bus. They don't know that anyone left their game, and they also can't continue the game. PR-138 was written to notify the users of game 1 and close their game when someone joined another game.

But, implementing this fix caused another problem--if an alpha created a game during mobile-to-mobile game creation, the game invite messages were never sent. Because this problem could've been caused by any number of functions within the code, **debugging this was a monster.**

Turns out that the chained Mongoose promises within the createGame() function were the problem. While promises are awesome for readability and making sure that certain functions run at certain times, they also hide errors. If errors are thrown by the functions within the promises (created using the native Mongoose promise library), they aren't thrown by default. This made it **pretty difficult for us to debug.**

On line ~210, we have an optinArgs object, which contains references to the "doc" and "config" variables. Unfortunately, this promise **doesn't close either of those variables.** No optinArgs were ever sent. I also modified the betaOptInArray to be populated with beta phone numbers outside of forEach() loop making mongoose calls; otherwise, it seems like the betaOptIn array might've been affected asynchronous weirdness.

## How should this be manually tested?
Postman! Run a local instance, have two phones on hand, and begin a mobile-to-mobile game by using Postman calls to the requisite endpoints.

## What are the relevant tickets?
JIra (SMS-63): 500 error code returned when users respond to game invite when they're already in a game, and associated bug with mobile-to-mobile game creation. (PR-138)

## To do in future:
Add error handling in Mongoose chained promises.