Skip to content

Commit

Permalink
adds actions for training events #1
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeenciu committed Jun 21, 2023
1 parent c0f892e commit 2e40a23
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
6 changes: 0 additions & 6 deletions libs/training-events/src/actions/emit-training-event.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
Expand All @@ -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()
Expand Down
50 changes: 48 additions & 2 deletions libs/training-events/src/actions/get-training-events.ts
Original file line number Diff line number Diff line change
@@ -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<TrainingEvent[] | undefined> => {
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<TrainingEvent[]> => {
const trainingEvents = await getTrainingEvents(trainingId)
if (!trainingEvents) {
throw new TrainingNotFound(trainingId)
}
return trainingEvents
}

export const getTraining = async (trainingId: string): Promise<Training | undefined> => {
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<Training> => {
const training = await getTraining(trainingId)
if (!training) {
throw new TrainingNotFound(trainingId)
}
return training
}
5 changes: 5 additions & 0 deletions libs/training-events/src/exception/training-not-found.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class TrainingNotFound extends Error {
constructor(trainingId: string) {
super(`Training ${trainingId} not found`);
}
}

0 comments on commit 2e40a23

Please sign in to comment.