diff --git a/libs/training-events/src/actions/emit-training-event.ts b/libs/training-events/src/actions/emit-training-event.ts index cbafa2e..4e99397 100644 --- a/libs/training-events/src/actions/emit-training-event.ts +++ b/libs/training-events/src/actions/emit-training-event.ts @@ -1,8 +1,6 @@ import { v4 as uuidv4 } from "uuid"; import { TrainingEvent, TrainingEventData } from "../types"; import { SYSTEM_ID, TRAINING_COLLECTION, TRAINING_EVENTS_SUBCOLLECTION } from '../config' -import { getFirestore } from "firebase-admin/firestore"; -import { FirestoreNotInitialzedException } from "../exception/firestore-not-initialized"; import { getDatabaseCollection } from '@rovacc/clients' export const emitTrainingEvent = async (eventData: TrainingEventData, correlationId: string): Promise => { @@ -16,10 +14,6 @@ export const emitTrainingEvent = async (eventData: TrainingEventData, correlatio ...eventData, } - const db = getFirestore(); - if (!db) { - throw new FirestoreNotInitialzedException() - } const trainingCollection = getDatabaseCollection(TRAINING_COLLECTION) const training = await trainingCollection.doc(trainingId).get() diff --git a/libs/training-events/src/actions/get-training-events.ts b/libs/training-events/src/actions/get-training-events.ts index cedd515..8e7ff56 100644 --- a/libs/training-events/src/actions/get-training-events.ts +++ b/libs/training-events/src/actions/get-training-events.ts @@ -1,4 +1,50 @@ -import { TrainingEvent } from "../types"; -export const getTrainingEvents = (trainingId: string): TrainingEvent[] => { +import { getDatabaseCollection } from "@rovacc/clients"; +import { Training, TrainingEvent } from "../types"; +import { TRAINING_COLLECTION, TRAINING_EVENTS_SUBCOLLECTION } from "../config"; +import { TrainingNotFound } from "../exception/training-not-found"; +export const getTrainingEvents = async (trainingId: string): Promise => { + const trainingCollection = getDatabaseCollection(TRAINING_COLLECTION) + + const trainingEvents = await trainingCollection + .doc(trainingId) + .collection(TRAINING_EVENTS_SUBCOLLECTION) + .get() + + if (!trainingEvents || !trainingEvents.docs || trainingEvents.docs.length === 0) { + return undefined + } + + return trainingEvents.docs.map(event => event.data() as TrainingEvent) +} + +export const tryGetTrainingEvents = async (trainingId: string): Promise => { + const trainingEvents = await getTrainingEvents(trainingId) + if (!trainingEvents) { + throw new TrainingNotFound(trainingId) + } + return trainingEvents +} + +export const getTraining = async (trainingId: string): Promise => { + const trainingCollection = getDatabaseCollection(TRAINING_COLLECTION) + + const training = await trainingCollection + .doc(trainingId) + .get() + + if (!training.exists) { + return undefined + } + + return training.data() as Training +} + + +export const tryGetTraining = async (trainingId: string): Promise => { + const training = await getTraining(trainingId) + if (!training) { + throw new TrainingNotFound(trainingId) + } + return training } diff --git a/libs/training-events/src/exception/training-not-found.ts b/libs/training-events/src/exception/training-not-found.ts new file mode 100644 index 0000000..ed55f89 --- /dev/null +++ b/libs/training-events/src/exception/training-not-found.ts @@ -0,0 +1,5 @@ +export class TrainingNotFound extends Error { + constructor(trainingId: string) { + super(`Training ${trainingId} not found`); + } +}