-
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
91a9b3d
commit e8b8cb9
Showing
11 changed files
with
193 additions
and
114 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 |
---|---|---|
@@ -1,40 +1,48 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { Training, TrainingEvent, TrainingEventData } from "@rovacc/training-events-types"; | ||
import { SYSTEM_ID, TRAINING_COLLECTION, TRAINING_EVENTS_SUBCOLLECTION } from '../config' | ||
import { getDatabaseCollection } from '@rovacc/clients' | ||
import { isEmitted, reduceEvent } from "../events"; | ||
import { getDate } from "../helpers/get-date"; | ||
|
||
|
||
export const emitTrainingEvent = async (eventData: TrainingEventData, training: Training | null, correlationId: string): Promise<Training | null> => { | ||
|
||
const trainingId = eventData.trainingId | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { | ||
Training, | ||
TrainingEvent, | ||
TrainingEventData, | ||
} from '@rovacc/training-events-types'; | ||
import { | ||
SYSTEM_ID, | ||
TRAINING_COLLECTION, | ||
TRAINING_EVENTS_SUBCOLLECTION, | ||
} from '../config'; | ||
import { getDatabaseCollection } from '@rovacc/clients'; | ||
import { isEmitted, reduceEvent } from '../events'; | ||
import { getDate } from '../helpers/get-date'; | ||
|
||
export const emitTrainingEvent = async ( | ||
eventData: TrainingEventData, | ||
training: Training | null, | ||
correlationId: string | ||
): Promise<Training | null> => { | ||
const trainingId = eventData.trainingId; | ||
const event: TrainingEvent = { | ||
eventId: uuidv4(), | ||
emittedAt: getDate(), | ||
system: SYSTEM_ID, | ||
correlationId, | ||
...eventData, | ||
} | ||
}; | ||
|
||
if (isEmitted[event.name] && isEmitted[event.name](training, event)) { | ||
return training | ||
return training; | ||
} | ||
|
||
const trainingCollection = getDatabaseCollection(TRAINING_COLLECTION) | ||
const trainingCollection = getDatabaseCollection(TRAINING_COLLECTION); | ||
|
||
const trainingObj = await trainingCollection.doc(trainingId).get() | ||
const trainingObj = await trainingCollection.doc(trainingId).get(); | ||
if (!trainingObj.exists) { | ||
await trainingCollection | ||
.doc(trainingId) | ||
.set({ trainingId }) | ||
await trainingCollection.doc(trainingId).set({ trainingId }); | ||
} | ||
|
||
await trainingCollection | ||
.doc(trainingId) | ||
.collection(TRAINING_EVENTS_SUBCOLLECTION) | ||
.doc(event.eventId) | ||
.set(event) | ||
.set(event); | ||
|
||
return reduceEvent(training, event) | ||
} | ||
return reduceEvent(training, 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
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
Oops, something went wrong.