Skip to content

Commit

Permalink
test helpers for training events #1
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeenciu committed Jun 22, 2023
1 parent 9b0ff0e commit 4a15fb3
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 41 deletions.
53 changes: 12 additions & 41 deletions libs/test-helpers/src/firestore/delete-collection.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,15 @@
import { getDatabaseCollection, getDatabaseClient } from '@rovacc/clients'

export const deleteCollection1 = async (collection: string): Promise<void> => {
const collectionRef = getDatabaseCollection(collection)
const docs = await collectionRef.listDocuments()
const deletePromises = docs.map(async (doc) => await doc.delete())
console.log(deletePromises[0].then(console.log).catch(console.error));
await Promise.all(deletePromises)
console.log('=============')
}


export async function deleteCollection(collectionPath: string) {
const db = getDatabaseClient()
const collectionRef = getDatabaseCollection(collectionPath)
const query = collectionRef.orderBy('__name__').limit(100);

return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve).catch(reject);
});
}

async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();

const batchSize = snapshot.size;
if (batchSize === 0) {
resolve();
return;
const endpoint = process.env['FIRESTORE_EMULATOR_HOST'] as string
const project = process.env['GCLOUD_PROJECT'] as string

const response = await fetch(
`http://${endpoint}/emulator/v1/projects/${project}/databases/(default)/documents/${collectionPath}`,
{
method: 'DELETE',
}
);
if (response.status !== 200) {
throw new Error('Trouble clearing Emulator: ' + (await response.text()));
}

// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();

// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TrainingCompletedEvent, TrainingCompletedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingCompletedEventData['payload']>
}

export const buildTrainingCompletedEvent = (args: Args): TrainingCompletedEvent => ({
trainingId: args.trainingId,
name: 'training-completed',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
reason: 'completed',
reasonDetailed: 'rating_upgraded',
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TrainingCptPerformedEvent, TrainingCptPerformedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingCptPerformedEventData['payload']>
}

export const buildTrainingCptPerformedEvent = (args: Args): TrainingCptPerformedEvent => ({
trainingId: args.trainingId,
name: 'training-cpt-performed',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
assessedBy: {
id: 200200,
name: 'member-name'
},
report: {
purpose: "cpt_ots",
workload: "light",
complexity: "routine",
traffic: "light",
comments: 'Session went well'
},
passed: true,
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TrainingCptRequestedEvent } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingCptRequestedEvent['payload']>
}

export const buildTrainingCptRequestedEvent = (args: Args): TrainingCptRequestedEvent => ({
trainingId: args.trainingId,
name: 'training-cpt-requested',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
requestedBy: { id: 123123, name: 'Requested By Name' },
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TrainingCptScheduledEvent } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingCptScheduledEvent['payload']>
}

export const buildTrainingCptScheduledEvent = (args: Args): TrainingCptScheduledEvent => ({
trainingId: args.trainingId,
name: 'training-cpt-scheduled',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
scheduledAt: new Date(),
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { TrainingIntentConfirmationExpiredEvent, TrainingIntentConfirmationExpiredEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingIntentConfirmationExpiredEventData['payload']>
}

export const buildTrainingIntentConfirmationExpiredEvent =
(args: Args): TrainingIntentConfirmationExpiredEvent => ({
name: 'training-intent-confirmation-expired',
trainingId: args.trainingId,
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { TrainingIntentConfirmationRejectedEvent, TrainingIntentConfirmationRejectedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingIntentConfirmationRejectedEventData['payload']>
}

export const buildTrainingIntentConfirmationRejectedEvent =
(args: Args): TrainingIntentConfirmationRejectedEvent => ({
name: 'training-intent-confirmation-rejected',
trainingId: args.trainingId,
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TrainingIntentConfirmationRequestedEvent, TrainingIntentConfirmationRequestedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingIntentConfirmationRequestedEventData['payload']>
}

export const buildTrainingIntentConfirmationRequestedEvent = (args: Args): TrainingIntentConfirmationRequestedEvent => ({
trainingId: args.trainingId,
name: 'training-intent-confirmation-requested',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TrainingIntentConfirmationRespondedEvent, TrainingIntentConfirmationRespondedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingIntentConfirmationRespondedEventData['payload']>
}

export const buildTrainingIntentConfirmationRespondedEvent = (args: Args): TrainingIntentConfirmationRespondedEvent => ({
trainingId: args.trainingId,
name: 'training-intent-confirmation-responded',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { TrainingIntentEvent } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingIntentEvent>
}

export const buildTrainingIntentEvent = (args: Args): TrainingIntentEvent => ({
trainingId: args.trainingId,
name: 'training-intent',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
student: 123123123,
rating: 2,
purpose: 'acquire_rating',
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TrainingMentorAssignedEvent, TrainingMentorAssignedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingMentorAssignedEventData['payload']>
}

export const buildTrainingMentorAssignedEvent = (args: Args): TrainingMentorAssignedEvent => ({
trainingId: args.trainingId,
name: 'training-mentor-assigned',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
mentor: { id: 200000, name: 'Mentor Name' },
assignedBy: { id: 200200, name: 'Assigned By Name' },
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TrainingMentorReassignedEvent, TrainingMentorReassignedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingMentorReassignedEventData['payload']>
}

export const buildTrainingMentorReassignedEvent = (args: Args): TrainingMentorReassignedEvent => ({
trainingId: args.trainingId,
name: 'training-mentor-reassigned',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
mentor: { id: 200000, name: 'Mentor Name' },
assignedBy: { id: 200200, name: 'Assigned By Name' },
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TrainingSessionPerformedEvent, TrainingSessionPerformedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingSessionPerformedEventData['payload']>
}

export const buildTrainingSessionPerformedEvent = (args: Args): TrainingSessionPerformedEvent => ({
trainingId: args.trainingId,
name: 'training-session-performed',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
sessionId: 'session-id',
mentor: { id: 200000, name: 'Mentor Name' },
report: {
purpose: "cpt_ots",
workload: "light",
complexity: "routine",
traffic: "light",
comments: 'Session went well'
},
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TrainingSessionScheduledEvent, TrainingSessionScheduledEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingSessionScheduledEventData['payload']>
}

export const buildTrainingSessionScheduledEvent = (args: Args): TrainingSessionScheduledEvent => ({
trainingId: args.trainingId,
name: 'training-session-scheduled',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
sessionId: 'session-id',
scheduledAt: new Date(),
...args.override
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TrainingSoloPerformedEvent, TrainingSoloPerformedEventData } from '@rovacc/training-events-types'

type Args = {
trainingId: string,
eventId?: string,
emittedAt?: Date,
override?: Partial<TrainingSoloPerformedEventData['payload']>
}

export const buildTrainingSoloPerformedEvent = (args: Args): TrainingSoloPerformedEvent => ({
trainingId: args.trainingId,
name: 'training-solo-performed',
eventId: args.eventId ?? 'event-id',
emittedAt: args.emittedAt ?? new Date(),
system: 'rovacc-system-id',
correlationId: 'correlationId',
payload: {
requestedBy: { id: 200000, name: 'Requested By Name' },
passed: true,
report: {
purpose: "cpt_ots",
workload: "light",
complexity: "routine",
traffic: "light",
comments: 'Session went well'
},
...args.override
}
})
Loading

0 comments on commit 4a15fb3

Please sign in to comment.