comedians.ts

import { CHANNEL_IDS } from '../../utility/constants/values';

async function getComediansData(context, category) {
  const { firestore, envmt, activeDbId } = context;
  const snapshot = await firestore
    // TODO update the name to be dynamic based on environment and active shard
    .collection(`${envmt}-${category}-${activeDbId}`)
    .orderBy('statistics.viewCount', 'desc')
    .limit(100)
    .get();

  let comedians = [];
  snapshot.forEach((document) => {
    const data = { ...document.data() };
    const { comedianId = '', snippet = {} } = data;
    const comedian = {
      id: comedianId,
      name: snippet.title,
      photoURL: snippet?.thumbnails?.medium?.url
    };
    comedians.push(comedian);
  });

  return comedians;
}

export async function getComedians(firestore) {
  const comedians = await getComediansData(firestore, CHANNEL_IDS);
  return comedians;
}