-
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
aa636c4
commit 18895fb
Showing
13 changed files
with
105 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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"; | ||
|
||
export const emitTrainingEvent = async (eventData: TrainingEventData, correlationId: string): Promise<void> => { | ||
|
||
const trainingId = eventData.trainingId | ||
const event: TrainingEvent = { | ||
eventId: uuidv4(), | ||
emittedAt: new Date(), | ||
system: SYSTEM_ID, | ||
correlationId, | ||
...eventData, | ||
} | ||
|
||
const db = getFirestore(); | ||
if (!db) { | ||
throw new FirestoreNotInitialzedException() | ||
} | ||
const trainingCollection = db.collection(TRAINING_COLLECTION) | ||
|
||
const training = await trainingCollection.doc(trainingId).get() | ||
if (!training.exists) { | ||
await trainingCollection | ||
.doc(trainingId) | ||
.set({ trainingId }) | ||
} | ||
|
||
await trainingCollection | ||
.doc(trainingId) | ||
.collection(TRAINING_EVENTS_SUBCOLLECTION) | ||
.doc(event.eventId) | ||
.set(event) | ||
|
||
} |
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,3 @@ | ||
export const SYSTEM_ID = process.env['SYSTEM_ID'] as string | ||
export const TRAINING_COLLECTION = 'training' | ||
export const TRAINING_EVENTS_SUBCOLLECTION = 'events' |
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
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
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
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
5 changes: 5 additions & 0 deletions
5
libs/training-events/src/exception/firestore-not-initialized.ts
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 FirestoreNotInitialzedException extends Error { | ||
constructor() { | ||
super('Firestore is not initialized'); | ||
} | ||
} |
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