// Import shared types
import Occurrence from '../types/Occurrence';

// Import shared helpers
import { getOccurrenceCollection } from '../../initCollections';

/**
 * Find an occurrence by its ID, merging basic info (occurrence collection
 *   document) with grouper/attendance-only info
 * @author Gabe Abrams
 * @author Ben Arnarsson
 */
const findOccurrence = async (
  occurrenceId: string,
): Promise<Occurrence | null> => {
  // since it is unique per day and append-only
  // so we can use occurrenceId as key
  const occurrenceCollection = getOccurrenceCollection();
  const occurrenceDocs = await occurrenceCollection.find({ id: occurrenceId });

  // If no occurrence was found, return null
  if (occurrenceDocs.length === 0) {
    return null;
  }

  // Extract occurrence
  const { id, ...occurrence } = occurrenceDocs[0];
  return occurrence;
};

export default findOccurrence;
