async function joinGame()

in backend/Functions/HTTP/game_play/app.js [31:74]


async function joinGame(sk, pk) {
  const Key = { pk, sk };
  let activeGameResponse;
  try {
    activeGameResponse = await ddb.get({
      TableName: gamesTableName,
      Key,
    }).promise();
    if (!Object.prototype.hasOwnProperty.call(activeGameResponse, 'Item')) {
      console.error(`Game not found ${JSON.stringify(activeGameResponse)}`);
      console.error(`Key Data ${JSON.stringify(Key)}`);
      return { statusCode: 500, body: JSON.stringify({ data: 'Looks like an invalid game' }) };
    }
  } catch (e) {
    console.error(`Error getting game ${JSON.stringify(e.stack)}`);
    console.error(`Key Data ${JSON.stringify(Key)}`);
    return { statusCode: 500, body: 'Error occurred retrieving game' };
  }

  // now to query the questions table and build the response object
  const pe = 'gameId, questionNumber, question, answerA,'
    + 'answerB, answerC, answerD, questionURI';
  const queryparms = {
    TableName: questionsTableName,
    ExpressionAttributeValues: { ':v1': sk },
    KeyConditionExpression: 'gameId = :v1',
    ProjectionExpression: pe,
  };
  const questionlist = activeGameResponse.Item;
  delete questionlist.starttime;

  try {
    const questions = await ddb.query(queryparms).promise();
    questionlist.questions = questions.Items;
    await logMetricEMF('GamesJoined', Unit.Count, 1,
      { service: 'game_play', operation: 'joinGame' });
    return { statusCode: 200, body: JSON.stringify(questionlist) };
  } catch (e) {
    await logMetricEMF('GamesJoinedFailed', Unit.Count, 1,
      { service: 'game_play', operation: 'joinGame' });
    console.error(`Invalid Game ${JSON.stringify(e.stack)}`);
    return { statusCode: 500, body: JSON.stringify({ message: 'Looks like an invalid game' }) };
  }
}