From 18895fba5e3f379b1a546b012e58a4ed97f414ff Mon Sep 17 00:00:00 2001 From: George Enciu Date: Tue, 20 Jun 2023 21:19:40 +0200 Subject: [PATCH] #1 fix typos --- .../src/actions/emit-training-event.ts | 37 +++++++++++ libs/training-events/src/config.ts | 3 + .../training-intent-confirmation-expired.ts | 4 +- .../training-intent-confirmation-rejected.ts | 4 +- .../training-intent-confirmation-requested.ts | 4 +- .../training-intent-confirmation-responded.ts | 4 +- .../src/events/training-mentor-assigned.ts | 4 +- .../src/events/training-mentor-reassigned.ts | 4 +- .../src/events/training-session-performed.ts | 4 +- .../src/events/training-session-scheduled.ts | 4 +- .../exception/firestore-not-initialized.ts | 5 ++ libs/training-events/src/types.ts | 61 ++++++++++++------- package.json | 6 +- 13 files changed, 105 insertions(+), 39 deletions(-) create mode 100644 libs/training-events/src/actions/emit-training-event.ts create mode 100644 libs/training-events/src/config.ts create mode 100644 libs/training-events/src/exception/firestore-not-initialized.ts diff --git a/libs/training-events/src/actions/emit-training-event.ts b/libs/training-events/src/actions/emit-training-event.ts new file mode 100644 index 0000000..caf4098 --- /dev/null +++ b/libs/training-events/src/actions/emit-training-event.ts @@ -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 => { + + 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) + +} diff --git a/libs/training-events/src/config.ts b/libs/training-events/src/config.ts new file mode 100644 index 0000000..a7a6128 --- /dev/null +++ b/libs/training-events/src/config.ts @@ -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' diff --git a/libs/training-events/src/events/training-intent-confirmation-expired.ts b/libs/training-events/src/events/training-intent-confirmation-expired.ts index c0c84fd..3aeda38 100644 --- a/libs/training-events/src/events/training-intent-confirmation-expired.ts +++ b/libs/training-events/src/events/training-intent-confirmation-expired.ts @@ -3,13 +3,13 @@ import { Reducer, Training, TrainingEventMetadata } from "../types"; const name = 'training-intent-confirmation-expired'; -export type TrainingIntentConfirmationExpiredData = { +export type TrainingIntentConfirmationExpiredEventData = { name: typeof name trainingId: string payload: Record } -export type TrainingIntentConfirmationExpiredEvent = TrainingEventMetadata & TrainingIntentConfirmationExpiredData +export type TrainingIntentConfirmationExpiredEvent = TrainingEventMetadata & TrainingIntentConfirmationExpiredEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/events/training-intent-confirmation-rejected.ts b/libs/training-events/src/events/training-intent-confirmation-rejected.ts index d4a79cb..0cdce34 100644 --- a/libs/training-events/src/events/training-intent-confirmation-rejected.ts +++ b/libs/training-events/src/events/training-intent-confirmation-rejected.ts @@ -2,13 +2,13 @@ import { Reducer, Training, TrainingEventMetadata } from "../types"; const name = 'training-intent-confirmation-rejected'; -export type TrainingIntentConfirmationRejectedData = { +export type TrainingIntentConfirmationRejectedEventData = { name: typeof name trainingId: string payload: Record } -export type TrainingIntentConfirmationRejectedEvent = TrainingEventMetadata & TrainingIntentConfirmationRejectedData +export type TrainingIntentConfirmationRejectedEvent = TrainingEventMetadata & TrainingIntentConfirmationRejectedEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/events/training-intent-confirmation-requested.ts b/libs/training-events/src/events/training-intent-confirmation-requested.ts index 9ea7356..ec8c89a 100644 --- a/libs/training-events/src/events/training-intent-confirmation-requested.ts +++ b/libs/training-events/src/events/training-intent-confirmation-requested.ts @@ -3,13 +3,13 @@ import { Reducer, Training, TrainingEventMetadata } from "../types"; const name = 'training-intent-confirmation-requested'; -export type TrainingIntentConfirmationRequestedData = { +export type TrainingIntentConfirmationRequestedEventData = { name: typeof name trainingId: string payload: Record } -export type TrainingIntentConfirmationRequestedEvent = TrainingEventMetadata & TrainingIntentConfirmationRequestedData +export type TrainingIntentConfirmationRequestedEvent = TrainingEventMetadata & TrainingIntentConfirmationRequestedEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/events/training-intent-confirmation-responded.ts b/libs/training-events/src/events/training-intent-confirmation-responded.ts index 404ae5d..f5a0572 100644 --- a/libs/training-events/src/events/training-intent-confirmation-responded.ts +++ b/libs/training-events/src/events/training-intent-confirmation-responded.ts @@ -3,13 +3,13 @@ import { Reducer, Training, TrainingEventMetadata } from "../types"; const name = 'training-intent-confirmation-responded'; -export type TrainingIntentConfirmationRespondedData = { +export type TrainingIntentConfirmationRespondedEventData = { name: typeof name trainingId: string payload: Record } -export type TrainingIntentConfirmationRespondedEvent = TrainingEventMetadata & TrainingIntentConfirmationRespondedData +export type TrainingIntentConfirmationRespondedEvent = TrainingEventMetadata & TrainingIntentConfirmationRespondedEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/events/training-mentor-assigned.ts b/libs/training-events/src/events/training-mentor-assigned.ts index 953f9c2..f91b9ac 100644 --- a/libs/training-events/src/events/training-mentor-assigned.ts +++ b/libs/training-events/src/events/training-mentor-assigned.ts @@ -2,7 +2,7 @@ import { Member, Reducer, Training, TrainingEventMetadata } from "../types"; const name = 'training-mentor-assigned' -export type TrainingMentorAssignedData = { +export type TrainingMentorAssignedEventData = { name: typeof name trainingId: string payload: { @@ -11,7 +11,7 @@ export type TrainingMentorAssignedData = { } } -export type TrainingMentorAssignedEvent = TrainingEventMetadata & TrainingMentorAssignedData +export type TrainingMentorAssignedEvent = TrainingEventMetadata & TrainingMentorAssignedEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/events/training-mentor-reassigned.ts b/libs/training-events/src/events/training-mentor-reassigned.ts index b4108d4..9b1a2db 100644 --- a/libs/training-events/src/events/training-mentor-reassigned.ts +++ b/libs/training-events/src/events/training-mentor-reassigned.ts @@ -2,7 +2,7 @@ import { Member, Reducer, Training, TrainingEventMetadata } from "../types"; const name = 'training-mentor-reassigned' -export type TrainingMentorReassignedData = { +export type TrainingMentorReassignedEventData = { name: typeof name trainingId: string payload: { @@ -11,7 +11,7 @@ export type TrainingMentorReassignedData = { } } -export type TrainingMentorReassignedEvent = TrainingEventMetadata & TrainingMentorReassignedData +export type TrainingMentorReassignedEvent = TrainingEventMetadata & TrainingMentorReassignedEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/events/training-session-performed.ts b/libs/training-events/src/events/training-session-performed.ts index 5215095..c7fb8da 100644 --- a/libs/training-events/src/events/training-session-performed.ts +++ b/libs/training-events/src/events/training-session-performed.ts @@ -2,7 +2,7 @@ import { Member, Reducer, Training, TrainingEventMetadata, TrainingReport } from const name = 'training-session-performed' -export type TrainingSessionPerformedData = { +export type TrainingSessionPerformedEventData = { name: typeof name trainingId: string payload: { @@ -12,7 +12,7 @@ export type TrainingSessionPerformedData = { } } -export type TrainingSessionPerformedEvent = TrainingEventMetadata & TrainingSessionPerformedData +export type TrainingSessionPerformedEvent = TrainingEventMetadata & TrainingSessionPerformedEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/events/training-session-scheduled.ts b/libs/training-events/src/events/training-session-scheduled.ts index 3da50e7..4627ebd 100644 --- a/libs/training-events/src/events/training-session-scheduled.ts +++ b/libs/training-events/src/events/training-session-scheduled.ts @@ -2,7 +2,7 @@ import { Reducer, Training, TrainingEventMetadata } from "../types"; const name = 'training-session-scheduled' -export type TrainingSessionScheduledData = { +export type TrainingSessionScheduledEventData = { name: typeof name trainingId: string payload: { @@ -11,7 +11,7 @@ export type TrainingSessionScheduledData = { } } -export type TrainingSessionScheduledEvent = TrainingEventMetadata & TrainingSessionScheduledData +export type TrainingSessionScheduledEvent = TrainingEventMetadata & TrainingSessionScheduledEventData const reducer: Reducer = ( training: Training | null, diff --git a/libs/training-events/src/exception/firestore-not-initialized.ts b/libs/training-events/src/exception/firestore-not-initialized.ts new file mode 100644 index 0000000..ab227ee --- /dev/null +++ b/libs/training-events/src/exception/firestore-not-initialized.ts @@ -0,0 +1,5 @@ +export class FirestoreNotInitialzedException extends Error { + constructor() { + super('Firestore is not initialized'); + } +} diff --git a/libs/training-events/src/types.ts b/libs/training-events/src/types.ts index fb52708..dbeb6c0 100644 --- a/libs/training-events/src/types.ts +++ b/libs/training-events/src/types.ts @@ -1,22 +1,21 @@ - -import { TrainingCompletedEvent } from "./events/training-completed" -import { TrainingCptPerformedEvent } from "./events/training-cpt-performed" -import { TrainingCptRequestedEvent } from "./events/training-cpt-requested" -import { TrainingCptScheduledEvent } from "./events/training-cpt-scheduled" -import { TrainingIntentEvent } from "./events/training-intent" -import { TrainingIntentConfirmationExpiredEvent } from "./events/training-intent-confirmation-expired" -import { TrainingIntentConfirmationRejectedEvent } from "./events/training-intent-confirmation-rejected" -import { TrainingIntentConfirmationRequestedEvent } from "./events/training-intent-confirmation-requested" -import { TrainingIntentConfirmationRespondedEvent } from "./events/training-intent-confirmation-responded" -import { TrainingMentorAssignedEvent } from "./events/training-mentor-assigned" -import { TrainingMentorReassignedEvent } from "./events/training-mentor-reassigned" -import { TrainingSessionPerformedEvent } from "./events/training-session-performed" -import { TrainingSessionScheduledEvent } from "./events/training-session-scheduled" -import { TrainingSoloPerformedEvent } from "./events/training-solo-performed" -import { TrainingSoloRequestedEvent } from "./events/training-solo-requested" -import { TrainingSoloScheduledEvent } from "./events/training-solo-scheduled" -import { TrainingTestAssignedEvent } from "./events/training-test-assigned" -import { TrainingTestCompletedEvent } from "./events/training-test-completed" +import { TrainingCompletedEvent, TrainingCompletedEventData } from "./events/training-completed" +import { TrainingCptPerformedEvent, TrainingCptPerformedEventData } from "./events/training-cpt-performed" +import { TrainingCptRequestedEvent, TrainingCptRequestedEventData } from "./events/training-cpt-requested" +import { TrainingCptScheduledEvent, TrainingCptScheduledEventData } from "./events/training-cpt-scheduled" +import { TrainingIntentEvent, TrainingIntentEventData } from "./events/training-intent" +import { TrainingIntentConfirmationExpiredEvent, TrainingIntentConfirmationExpiredEventData } from "./events/training-intent-confirmation-expired" +import { TrainingIntentConfirmationRejectedEvent, TrainingIntentConfirmationRejectedEventData } from "./events/training-intent-confirmation-rejected" +import { TrainingIntentConfirmationRequestedEvent, TrainingIntentConfirmationRequestedEventData } from "./events/training-intent-confirmation-requested" +import { TrainingIntentConfirmationRespondedEvent, TrainingIntentConfirmationRespondedEventData } from "./events/training-intent-confirmation-responded" +import { TrainingMentorAssignedEvent, TrainingMentorAssignedEventData } from "./events/training-mentor-assigned" +import { TrainingMentorReassignedEvent, TrainingMentorReassignedEventData } from "./events/training-mentor-reassigned" +import { TrainingSessionPerformedEvent, TrainingSessionPerformedEventData } from "./events/training-session-performed" +import { TrainingSessionScheduledEvent, TrainingSessionScheduledEventData } from "./events/training-session-scheduled" +import { TrainingSoloPerformedEvent, TrainingSoloPerformedEventData } from "./events/training-solo-performed" +import { TrainingSoloRequestedEvent, TrainingSoloRequestedEventData } from "./events/training-solo-requested" +import { TrainingSoloScheduledEvent, TrainingSoloScheduledEventData } from "./events/training-solo-scheduled" +import { TrainingTestAssignedEvent, TrainingTestAssignedEventData } from "./events/training-test-assigned" +import { TrainingTestCompletedEvent, TrainingTestCompletedEventData } from "./events/training-test-completed" export type TrainingEvent = | TrainingCompletedEvent @@ -38,6 +37,26 @@ export type TrainingEvent = | TrainingTestCompletedEvent | TrainingCptRequestedEvent +export type TrainingEventData = + | TrainingCompletedEventData + | TrainingCptPerformedEventData + | TrainingCptScheduledEventData + | TrainingIntentConfirmationExpiredEventData + | TrainingIntentConfirmationRejectedEventData + | TrainingIntentConfirmationRequestedEventData + | TrainingIntentConfirmationRespondedEventData + | TrainingIntentEventData + | TrainingMentorAssignedEventData + | TrainingMentorReassignedEventData + | TrainingSessionPerformedEventData + | TrainingSessionScheduledEventData + | TrainingSoloPerformedEventData + | TrainingSoloRequestedEventData + | TrainingSoloScheduledEventData + | TrainingTestAssignedEventData + | TrainingTestCompletedEventData + | TrainingCptRequestedEventData + export type Member = { id: number name: string @@ -54,8 +73,6 @@ export type TrainingStatus = | "COMPLETED" | "TERMINATED" - - export type TrainingPurpose = "acquire_rating" | "revalidate_rating" | "visiting" export type TestResult = "passed" | "failed" export type TrainingReport = { @@ -133,7 +150,7 @@ export type Reducer = (training: Training | null, event: T) => Trai export type IsEmitted = (training: Training | null, event: T) => boolean export type TrainingEventMetadata = { - id: string + eventId: string emittedAt: Date system: string correlationId: string diff --git a/package.json b/package.json index 9986200..36c80ba 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,10 @@ "scripts": {}, "private": true, "dependencies": { - "firebase-admin": "^11.9.0" + "@nx/js": "^16.3.2", + "firebase-admin": "^11.9.0", + "firebase-functions": "^4.4.1", + "uuid": "^9.0.0" }, "devDependencies": { "@nx/node": "^16.3.2", @@ -15,6 +18,7 @@ "@nx/vite": "16.3.2", "@nx/workspace": "16.3.2", "@types/node": "18.7.1", + "@types/uuid": "^9.0.2", "@typescript-eslint/eslint-plugin": "^5.58.0", "@typescript-eslint/parser": "^5.58.0", "@vitest/coverage-c8": "^0.31.0",