-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0f892e
commit 2e40a23
Showing
3 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} |