From ad40e41de39beee04e47690fc51d6bf24224158d Mon Sep 17 00:00:00 2001 From: George Enciu Date: Tue, 20 Jun 2023 20:21:00 +0200 Subject: [PATCH] #1 adds training events and reducer --- libs/training-events/src/events/index.ts | 18 ++++++ libs/training-events/src/events/reducer.ts | 60 +++++++++++++++++++ .../src/events/training-completed.ts | 31 ++++++++++ .../src/events/training-cpt-performed.ts | 34 +++++++++++ .../src/events/training-cpt-requested.ts | 33 ++++++++++ .../src/events/training-cpt-scheduled.ts | 32 ++++++++++ .../training-intent-confirmation-expired.ts | 32 ++++++++++ .../training-intent-confirmation-rejected.ts | 31 ++++++++++ .../training-intent-confirmation-requested.ts | 31 ++++++++++ .../training-intent-confirmation-responded.ts | 33 ++++++++++ .../src/events/training-intent.ts | 34 +++++++++++ .../src/events/training-mentor-assigned.ts | 33 ++++++++++ .../src/events/training-mentor-reassigned.ts | 34 +++++++++++ .../src/events/training-session-performed.ts | 39 ++++++++++++ .../src/events/training-session-scheduled.ts | 37 ++++++++++++ .../src/events/training-solo-performed.ts | 34 +++++++++++ .../src/events/training-solo-requested.ts | 33 ++++++++++ .../src/events/training-solo-scheduled.ts | 32 ++++++++++ .../src/events/training-test-assigned.ts | 32 ++++++++++ .../src/events/training-test-completed.ts | 35 +++++++++++ 20 files changed, 678 insertions(+) create mode 100644 libs/training-events/src/events/index.ts create mode 100644 libs/training-events/src/events/reducer.ts create mode 100644 libs/training-events/src/events/training-completed.ts create mode 100644 libs/training-events/src/events/training-cpt-performed.ts create mode 100644 libs/training-events/src/events/training-cpt-requested.ts create mode 100644 libs/training-events/src/events/training-cpt-scheduled.ts create mode 100644 libs/training-events/src/events/training-intent-confirmation-expired.ts create mode 100644 libs/training-events/src/events/training-intent-confirmation-rejected.ts create mode 100644 libs/training-events/src/events/training-intent-confirmation-requested.ts create mode 100644 libs/training-events/src/events/training-intent-confirmation-responded.ts create mode 100644 libs/training-events/src/events/training-intent.ts create mode 100644 libs/training-events/src/events/training-mentor-assigned.ts create mode 100644 libs/training-events/src/events/training-mentor-reassigned.ts create mode 100644 libs/training-events/src/events/training-session-performed.ts create mode 100644 libs/training-events/src/events/training-session-scheduled.ts create mode 100644 libs/training-events/src/events/training-solo-performed.ts create mode 100644 libs/training-events/src/events/training-solo-requested.ts create mode 100644 libs/training-events/src/events/training-solo-scheduled.ts create mode 100644 libs/training-events/src/events/training-test-assigned.ts create mode 100644 libs/training-events/src/events/training-test-completed.ts diff --git a/libs/training-events/src/events/index.ts b/libs/training-events/src/events/index.ts new file mode 100644 index 0000000..1d823e1 --- /dev/null +++ b/libs/training-events/src/events/index.ts @@ -0,0 +1,18 @@ +export { trainingCompleted } from "./training-completed" +export { trainingCptPerformed } from "./training-cpt-performed" +export { trainingCptRequested } from "./training-cpt-requested" +export { trainingCptScheduled } from "./training-cpt-scheduled" +export { trainingIntent } from "./training-intent"; +export { trainingIntentConfirmationExpired } from "./training-intent-confirmation-expired"; +export { trainingIntentConfirmationRejected } from "./training-intent-confirmation-rejected"; +export { trainingIntentConfirmationRequested } from "./training-intent-confirmation-requested"; +export { trainingIntentConfirmationResponded } from "./training-intent-confirmation-responded"; +export { trainingMentorAssigned } from "./training-mentor-assigned"; +export { trainingMentorReassigned } from "./training-mentor-reassigned"; +export { trainingSessionPerformed } from "./training-session-performed"; +export { trainingSessionScheduled } from "./training-session-scheduled"; +export { trainingSoloPerformed } from "./training-solo-performed" +export { trainingSoloRequested } from "./training-solo-requested" +export { trainingSoloScheduled } from "./training-solo-scheduled" +export { trainingTestAssigned } from "./training-test-assigned"; +export { trainingTestCompleted } from "./training-test-completed"; diff --git a/libs/training-events/src/events/reducer.ts b/libs/training-events/src/events/reducer.ts new file mode 100644 index 0000000..aeb4c54 --- /dev/null +++ b/libs/training-events/src/events/reducer.ts @@ -0,0 +1,60 @@ +import { IsEmitted, Reducer, Training, TrainingEvent } from "../types"; +import { trainingCompleted } from "./training-completed"; +import { trainingCptPerformed } from "./training-cpt-performed"; +import { trainingCptRequested } from "./training-cpt-requested"; +import { trainingCptScheduled } from "./training-cpt-scheduled"; +import { trainingIntent } from "./training-intent"; +import { trainingIntentConfirmationExpired } from "./training-intent-confirmation-expired"; +import { trainingIntentConfirmationRejected } from "./training-intent-confirmation-rejected"; +import { trainingIntentConfirmationRequested } from "./training-intent-confirmation-requested"; +import { trainingIntentConfirmationResponded } from "./training-intent-confirmation-responded"; +import { trainingMentorAssigned } from "./training-mentor-assigned"; +import { trainingMentorReassigned } from "./training-mentor-reassigned"; +import { trainingSessionPerformed } from "./training-session-performed"; +import { trainingSessionScheduled } from "./training-session-scheduled"; +import { trainingSoloPerformed } from "./training-solo-performed"; +import { trainingSoloRequested } from "./training-solo-requested"; +import { trainingSoloScheduled } from "./training-solo-scheduled"; +import { trainingTestAssigned } from "./training-test-assigned"; +import { trainingTestCompleted } from "./training-test-completed"; + +const events = [ + trainingCompleted, + trainingCptPerformed, + trainingCptRequested, + trainingCptScheduled, + trainingIntent, + trainingIntentConfirmationExpired, + trainingIntentConfirmationRejected, + trainingIntentConfirmationRequested, + trainingIntentConfirmationResponded, + trainingMentorAssigned, + trainingMentorReassigned, + trainingSessionPerformed, + trainingSessionScheduled, + trainingSoloPerformed, + trainingSoloRequested, + trainingSoloScheduled, + trainingTestAssigned, + trainingTestCompleted, +] as const + +type Names = typeof events[number]['name'] + +const { reducer, isEmitted } = events.reduce( + (acc, event) => ({ + reducer: { ...acc.reducer, [event.name]: event.reducer }, + isEmitted: { ...acc.isEmitted, [event.name]: event.isEmitted } + }), { reducer: {} as Record, isEmitted: {} as Record }) + +export const applyReducer = (training: Training | null, event: TrainingEvent): Training | null => { + if (!reducer[event.name]) { + console.log('Event not found', event) + return training + } else { + return !isEmitted[event.name](training, event) + ? reducer[event.name](training, event) + : training + } +} + diff --git a/libs/training-events/src/events/training-completed.ts b/libs/training-events/src/events/training-completed.ts new file mode 100644 index 0000000..cce3bd8 --- /dev/null +++ b/libs/training-events/src/events/training-completed.ts @@ -0,0 +1,31 @@ + +import { OutcomeReason, OutcomeReasonDetailed, Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-completed' + +export type TrainingCompletedEventData = { + trainingId: string, + name: typeof name + payload: { + reason: OutcomeReason + reasonDetailed: OutcomeReasonDetailed + } +} + +export type TrainingCompletedEvent = TrainingEventMetadata & TrainingCompletedEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingCompletedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: event.payload.reason === 'completed' ? 'COMPLETED' : 'TERMINATED', + ...event.payload, + completedAt: event.emittedAt, +}) + +const isEmitted = (training: Training | null) => !!training?.completedAt + +export const trainingCompleted = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-cpt-performed.ts b/libs/training-events/src/events/training-cpt-performed.ts new file mode 100644 index 0000000..45d2290 --- /dev/null +++ b/libs/training-events/src/events/training-cpt-performed.ts @@ -0,0 +1,34 @@ + +import { Member, Reducer, Training, TrainingEventMetadata, TrainingReport } from "../types"; + +const name = 'training-cpt-performed' + +export type TrainingCptPerformedEventData = { + trainingId: string, + name: typeof name + payload: { + assessedBy: Member + report: TrainingReport + passed: boolean + } +} + +export type TrainingCptPerformedEvent = TrainingEventMetadata & TrainingCptPerformedEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingCptPerformedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: event.payload.passed ? 'COMPLETED' : 'AWAITING_CPT', + cpt: { + ...training?.cpt, + ...event.payload + } +}) + +const isEmitted = (training: Training | null) => !!training?.cpt && !!training?.cpt.assessedBy + +export const trainingCptPerformed = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-cpt-requested.ts b/libs/training-events/src/events/training-cpt-requested.ts new file mode 100644 index 0000000..b8aa9a5 --- /dev/null +++ b/libs/training-events/src/events/training-cpt-requested.ts @@ -0,0 +1,33 @@ + +import { Member, Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-cpt-requested' + +export type TrainingCptRequestedEventData = { + trainingId: string, + name: typeof name + payload: { + requestedBy: Member + } +} + +export type TrainingCptRequestedEvent = TrainingEventMetadata & TrainingCptRequestedEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingCptRequestedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'AWAITING_CPT', + cpt: { + ...training?.cpt, + ...event.payload, + requestedAt: event.emittedAt, + } +}) + +const isEmitted = (training: Training | null) => !!training?.cpt && !!training?.cpt.requestedAt + +export const trainingCptRequested = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-cpt-scheduled.ts b/libs/training-events/src/events/training-cpt-scheduled.ts new file mode 100644 index 0000000..703a591 --- /dev/null +++ b/libs/training-events/src/events/training-cpt-scheduled.ts @@ -0,0 +1,32 @@ + +import { Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-cpt-scheduled' + +export type TrainingCptScheduledEventData = { + trainingId: string, + name: typeof name + payload: { + scheduledAt: Date + } +} + +export type TrainingCptScheduledEvent = TrainingEventMetadata & TrainingCptScheduledEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingCptScheduledEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'AWAITING_CPT', + cpt: { + ...training?.cpt, + scheduledAt: event.payload.scheduledAt + } +}) + +const isEmitted = (training: Training | null) => !!training?.cpt && !!training?.cpt.scheduledAt + +export const trainingCptScheduled = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-intent-confirmation-expired.ts b/libs/training-events/src/events/training-intent-confirmation-expired.ts new file mode 100644 index 0000000..c0c84fd --- /dev/null +++ b/libs/training-events/src/events/training-intent-confirmation-expired.ts @@ -0,0 +1,32 @@ + +import { Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-intent-confirmation-expired'; + +export type TrainingIntentConfirmationExpiredData = { + name: typeof name + trainingId: string + payload: Record +} + +export type TrainingIntentConfirmationExpiredEvent = TrainingEventMetadata & TrainingIntentConfirmationExpiredData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingIntentConfirmationExpiredEvent +): Training => ({ + ...training, + status: 'QUEUED', + trainingId: event.trainingId, + intentConfirmation: { + ...training?.intentConfirmation, + expiredAt: event.emittedAt, + } +}) + +const isEmitted = + (training: Training | null) => + training && training?.intentConfirmation && !!training.intentConfirmation.expiredAt + +export const trainingIntentConfirmationExpired = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-intent-confirmation-rejected.ts b/libs/training-events/src/events/training-intent-confirmation-rejected.ts new file mode 100644 index 0000000..d4a79cb --- /dev/null +++ b/libs/training-events/src/events/training-intent-confirmation-rejected.ts @@ -0,0 +1,31 @@ +import { Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-intent-confirmation-rejected'; + +export type TrainingIntentConfirmationRejectedData = { + name: typeof name + trainingId: string + payload: Record +} + +export type TrainingIntentConfirmationRejectedEvent = TrainingEventMetadata & TrainingIntentConfirmationRejectedData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingIntentConfirmationRejectedEvent +): Training => ({ + ...training, + status: 'QUEUED', + trainingId: event.trainingId, + intentConfirmation: { + ...training?.intentConfirmation, + rejectedAt: event.emittedAt, + } +}) + +const isEmitted = + (training: Training | null) => + training && training?.intentConfirmation && training.intentConfirmation.rejectedAt + +export const trainingIntentConfirmationRejected = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-intent-confirmation-requested.ts b/libs/training-events/src/events/training-intent-confirmation-requested.ts new file mode 100644 index 0000000..9ea7356 --- /dev/null +++ b/libs/training-events/src/events/training-intent-confirmation-requested.ts @@ -0,0 +1,31 @@ + +import { Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-intent-confirmation-requested'; + +export type TrainingIntentConfirmationRequestedData = { + name: typeof name + trainingId: string + payload: Record +} + +export type TrainingIntentConfirmationRequestedEvent = TrainingEventMetadata & TrainingIntentConfirmationRequestedData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingIntentConfirmationRequestedEvent +): Training => ({ + ...training, + status: 'QUEUED', + trainingId: event.trainingId, + intentConfirmation: { + requestedAt: event.emittedAt, + } +}) + +const isEmitted = + (training: Training | null) => + training && training?.intentConfirmation && !!training.intentConfirmation.requestedAt + +export const trainingIntentConfirmationRequested = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-intent-confirmation-responded.ts b/libs/training-events/src/events/training-intent-confirmation-responded.ts new file mode 100644 index 0000000..404ae5d --- /dev/null +++ b/libs/training-events/src/events/training-intent-confirmation-responded.ts @@ -0,0 +1,33 @@ + +import { Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-intent-confirmation-responded'; + +export type TrainingIntentConfirmationRespondedData = { + name: typeof name + trainingId: string + payload: Record +} + +export type TrainingIntentConfirmationRespondedEvent = TrainingEventMetadata & TrainingIntentConfirmationRespondedData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingIntentConfirmationRespondedEvent +): Training => ({ + ...training, + status: 'QUEUED', + trainingId: event.trainingId, + intentConfirmation: { + ...training?.intentConfirmation, + respondedAt: event.emittedAt, + + } +}) + +const isEmitted = + (training: Training | null) => + training && training?.intentConfirmation && training.intentConfirmation.respondedAt + +export const trainingIntentConfirmationResponded = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-intent.ts b/libs/training-events/src/events/training-intent.ts new file mode 100644 index 0000000..b0faa02 --- /dev/null +++ b/libs/training-events/src/events/training-intent.ts @@ -0,0 +1,34 @@ + +import { Reducer, Training, TrainingEventMetadata, TrainingPurpose } from "../types"; + +const name = 'training-intent'; + +export type TrainingIntentEventData = { + trainingId: string, + name: typeof name + payload: { + student: number + rating: number + purpose: TrainingPurpose + } +} + +export type TrainingIntentEvent = TrainingEventMetadata & TrainingIntentEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingIntentEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'QUEUED', + purpose: event.payload.purpose, + rating: event.payload.rating, + student: event.payload.student, + requestedAt: event.emittedAt, +}) + +const isEmitted = (training: Training | null) => !!training?.student + +export const trainingIntent = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-mentor-assigned.ts b/libs/training-events/src/events/training-mentor-assigned.ts new file mode 100644 index 0000000..953f9c2 --- /dev/null +++ b/libs/training-events/src/events/training-mentor-assigned.ts @@ -0,0 +1,33 @@ +import { Member, Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-mentor-assigned' + +export type TrainingMentorAssignedData = { + name: typeof name + trainingId: string + payload: { + mentor: Member + assigned_by: Member + } +} + +export type TrainingMentorAssignedEvent = TrainingEventMetadata & TrainingMentorAssignedData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingMentorAssignedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'STARTED', + mentor: { + member: event.payload.mentor, + assignedAt: event.emittedAt, + assignedBy: event.payload.assigned_by, + } +}) + +const isEmitted = (training: Training | null) => !!training?.mentor + +export const trainingMentorAssigned = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-mentor-reassigned.ts b/libs/training-events/src/events/training-mentor-reassigned.ts new file mode 100644 index 0000000..b4108d4 --- /dev/null +++ b/libs/training-events/src/events/training-mentor-reassigned.ts @@ -0,0 +1,34 @@ +import { Member, Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-mentor-reassigned' + +export type TrainingMentorReassignedData = { + name: typeof name + trainingId: string + payload: { + mentor: Member + assigned_by: Member + } +} + +export type TrainingMentorReassignedEvent = TrainingEventMetadata & TrainingMentorReassignedData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingMentorReassignedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'IN_PROGRESS', + mentor: { + member: event.payload.mentor, + assignedAt: event.emittedAt, + assignedBy: event.payload.assigned_by, + } +}) + +const isEmitted = + (training: Training | null, event: TrainingMentorReassignedEvent) => training && training?.mentor?.member.id !== event.payload.mentor.id + +export const trainingMentorReassigned = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-session-performed.ts b/libs/training-events/src/events/training-session-performed.ts new file mode 100644 index 0000000..5215095 --- /dev/null +++ b/libs/training-events/src/events/training-session-performed.ts @@ -0,0 +1,39 @@ +import { Member, Reducer, Training, TrainingEventMetadata, TrainingReport } from "../types"; + +const name = 'training-session-performed' + +export type TrainingSessionPerformedData = { + name: typeof name + trainingId: string + payload: { + sessionId: string + mentor: Member, + report: TrainingReport + } +} + +export type TrainingSessionPerformedEvent = TrainingEventMetadata & TrainingSessionPerformedData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingSessionPerformedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'IN_PROGRESS', + sessions: { + ...training?.sessions, + [event.payload.sessionId]: { + ...training?.sessions?.[event.payload.sessionId], + mentor: event.payload.mentor, + report: event.payload.report, + } + } +}) + +const isEmitted = + (training: Training | null, event: TrainingSessionPerformedEvent) => + !!training?.sessions && !!training.sessions[event.payload.sessionId] && !!training.sessions[event.payload.sessionId].report + +export const trainingSessionPerformed = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-session-scheduled.ts b/libs/training-events/src/events/training-session-scheduled.ts new file mode 100644 index 0000000..3da50e7 --- /dev/null +++ b/libs/training-events/src/events/training-session-scheduled.ts @@ -0,0 +1,37 @@ +import { Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-session-scheduled' + +export type TrainingSessionScheduledData = { + name: typeof name + trainingId: string + payload: { + sessionId: string + scheduledAt: Date + } +} + +export type TrainingSessionScheduledEvent = TrainingEventMetadata & TrainingSessionScheduledData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingSessionScheduledEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'IN_PROGRESS', + sessions: { + ...training?.sessions, + [event.payload.sessionId]: { + ...training?.sessions?.[event.payload.sessionId], + scheduledAt: event.payload.scheduledAt, + } + } +}) + +const isEmitted = + (training: Training | null, event: TrainingSessionScheduledEvent) => + !!training?.sessions && !!training.sessions[event.payload.sessionId] && !!training.sessions[event.payload.sessionId].scheduledAt + +export const trainingSessionScheduled = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-solo-performed.ts b/libs/training-events/src/events/training-solo-performed.ts new file mode 100644 index 0000000..ac0dd3d --- /dev/null +++ b/libs/training-events/src/events/training-solo-performed.ts @@ -0,0 +1,34 @@ + +import { Member, Reducer, Training, TrainingEventMetadata, TrainingReport } from "../types"; + +const name = 'training-solo-performed' + +export type TrainingSoloPerformedEventData = { + trainingId: string, + name: typeof name + payload: { + requestedBy: Member + passed: boolean + report: TrainingReport + } +} + +export type TrainingSoloPerformedEvent = TrainingEventMetadata & TrainingSoloPerformedEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingSoloPerformedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: event.payload.passed ? 'SOLO' : 'AWAITING_SOLO', + solo: { + ...training?.solo, + ...event.payload, + } +}) + +const isEmitted = (training: Training | null) => !!training?.solo && !!training?.solo.performedBy + +export const trainingSoloPerformed = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-solo-requested.ts b/libs/training-events/src/events/training-solo-requested.ts new file mode 100644 index 0000000..1aa24ef --- /dev/null +++ b/libs/training-events/src/events/training-solo-requested.ts @@ -0,0 +1,33 @@ + +import { Member, Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-solo-requested' + +export type TrainingSoloRequestedEventData = { + trainingId: string, + name: typeof name + payload: { + requestedBy: Member + } +} + +export type TrainingSoloRequestedEvent = TrainingEventMetadata & TrainingSoloRequestedEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingSoloRequestedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'AWAITING_SOLO', + solo: { + ...training?.solo, + requestedAt: event.emittedAt, + requestedBy: event.payload.requestedBy + } +}) + +const isEmitted = (training: Training | null) => !!training?.solo && !!training?.solo.requestedAt + +export const trainingSoloRequested = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-solo-scheduled.ts b/libs/training-events/src/events/training-solo-scheduled.ts new file mode 100644 index 0000000..afb8f4d --- /dev/null +++ b/libs/training-events/src/events/training-solo-scheduled.ts @@ -0,0 +1,32 @@ + +import { Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-solo-scheduled' + +export type TrainingSoloScheduledEventData = { + trainingId: string, + name: typeof name + payload: { + scheduledAt: Date + } +} + +export type TrainingSoloScheduledEvent = TrainingEventMetadata & TrainingSoloScheduledEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingSoloScheduledEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: 'AWAITING_SOLO', + solo: { + ...training?.solo, + scheduledAt: event.emittedAt, + } +}) + +const isEmitted = (training: Training | null) => !!training?.solo && !!training?.solo.scheduledAt + +export const trainingSoloScheduled = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-test-assigned.ts b/libs/training-events/src/events/training-test-assigned.ts new file mode 100644 index 0000000..810e8c5 --- /dev/null +++ b/libs/training-events/src/events/training-test-assigned.ts @@ -0,0 +1,32 @@ + +import { Member, Reducer, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-test-assigned' + +export type TrainingTestAssignedEventData = { + trainingId: string, + name: typeof name + payload: { + assignedBy: Member + } +} + +export type TrainingTestAssignedEvent = TrainingEventMetadata & TrainingTestAssignedEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingTestAssignedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: "AWAITING_TEST", + test: { + assignedAt: event.emittedAt, + assignedBy: event.payload.assignedBy, + } +}) + +const isEmitted = (training: Training | null) => !!training?.test + +export const trainingTestAssigned = { name, reducer, isEmitted } as const + diff --git a/libs/training-events/src/events/training-test-completed.ts b/libs/training-events/src/events/training-test-completed.ts new file mode 100644 index 0000000..a0f0068 --- /dev/null +++ b/libs/training-events/src/events/training-test-completed.ts @@ -0,0 +1,35 @@ +import { Reducer, TestResult, Training, TrainingEventMetadata } from "../types"; + +const name = 'training-test-completed' + +export type TrainingTestCompletedEventData = { + trainingId: string, + name: typeof name + payload: { + passed: boolean + result: TestResult + willExpireAt?: Date + } +} + +export type TrainingTestCompletedEvent = TrainingEventMetadata & TrainingTestCompletedEventData + +const reducer: Reducer = ( + training: Training | null, + event: TrainingTestCompletedEvent +): Training => ({ + ...training, + trainingId: event.trainingId, + status: event.payload.passed ? "IN_PROGRESS" : "AWAITING_TEST", + test: { + ...training?.test, + completedAt: event.emittedAt, + result: event.payload.result, + willExpireAt: event.payload.willExpireAt + } +}) + +const isEmitted = (training: Training | null) => !!training?.test && !!training?.test?.completedAt + +export const trainingTestCompleted = { name, reducer, isEmitted } as const +