Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
3.5 kB
3
Indexable
Never
exports.getWishlistBalance = functions.https.onCall(async (data, context) => {
  // Check if the user is authenticated
  if (!context.auth) {
    throw new functions.https.HttpsError("unauthenticated", "User is not authenticated.");
  }

  // Get the email of the authenticated user
  const email = context.auth.token.email;

  // Get the publisher ID of the authenticated user
  const usersMapDataset = bigquery.dataset("xxx.metrics_games");
  const usersMapTable = usersMapDataset.table("users_map");
  const usersMapQuery = `SELECT pub_id FROM xxx.metrics_games.users_map WHERE email = '${email}'`;
  functions.logger.log(`!!!! QUERY: '${usersMapQuery}'`);
  const usersMapOptions = {
    query: usersMapQuery,
    location: "US",
  };
  const [usersMapRows] = await bigquery.query(usersMapOptions);
  const userPubId = usersMapRows[0].pub_id;

  // Log the fetched users_map data
  functions.logger.log(`!!!! Fetched users_map data: ${JSON.stringify(usersMapRows)}`);

  // Get the game ID and period of time from the client request
  const gameId = data.gameId;
  const startDate = data.startDate;
  const endDate = data.endDate;

  // Check if the game and the querying user are assigned to the same publisher
  const gamesDataset = bigquery.dataset("xxx.metrics_games");
  const gamesTable = gamesDataset.table("games");
  const gamesQuery = `SELECT pub_id FROM xxx.metrics_games.games WHERE _id_ = ${gameId}`;
  // Print query to the console
  functions.logger.log(`!!!! QUERY: '${gamesQuery}'`);
  const gamesOptions = {
    query: gamesQuery,
    location: "US",
  };
  const [gamesRows] = await bigquery.query(gamesOptions);
  const gamePubId = gamesRows[0].pub_id;
  functions.logger.log(`!!!! Fetched game pub_id: ${gamePubId}`);
  if (userPubId !== gamePubId && userPubId !== -1) {
    throw new functions.https.HttpsError("permission-denied", "User does not have permission to access this data.");
  }

  // Query the wishlist balance data for the selected period of time and game ID
  const steamStatsDataset = bigquery.dataset("xxx.metrics_games");
  const steamStatsTable = steamStatsDataset.table("steam_stats");
  const steamStatsQuery = `SELECT stat_date, country_id, wishlist_balance FROM xxx.metrics_games.steam_stats WHERE game_id = ${gameId} AND stat_date >= '${startDate}' AND stat_date <= '${endDate}'`;
  functions.logger.log(`!!!! QUERY: '${steamStatsQuery}'`);
  const steamStatsOptions = {
    query: steamStatsQuery,
    location: "US",
  };
  const [steamStatsRows] = await bigquery.query(steamStatsOptions);

  // Log the fetched steam_stats first row
  // print number of rows
  functions.logger.log(`!!!! Number of rows: ${steamStatsRows.length} Fetched steam_stats data: ${JSON.stringify(steamStatsRows[0])}`);
  //functions.logger.log(`!!!! Fetched steam_stats data: ${JSON.stringify(steamStatsRows)}`);

  // Map the queried rows to WishlistStats objects
  const wishlistStats = steamStatsRows.map(row => {
    // Parse date string 
    const date = new Date(row.stat_date.value);
    // Handle invalid date
    if(!date) {
      return null; 
    }
    // Convert to string  
    const dateString = date.toISOString();
    //functions.logger.log(`!!!! Date: '${JSON.stringify(date)}'`);
    return {
      date: dateString,
      country_id: row.country_id,
      wishlistBalance: row.wishlist_balance
    }
  });

  // Return the queried data
  return wishlistStats;
});