Skip to content

Commit

Permalink
Connecting training quiz results back up, accidentally added to State…
Browse files Browse the repository at this point in the history
… but that seems deprecated
  • Loading branch information
Lan2u committed Sep 15, 2024
1 parent a4e8d3e commit d338b87
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 71 deletions.
26 changes: 25 additions & 1 deletion src/read-models/shared-state/get-equipment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {pipe} from 'fp-ts/lib/function';
import * as O from 'fp-ts/Option';
import {BetterSQLite3Database} from 'drizzle-orm/better-sqlite3';
import {eq} from 'drizzle-orm';
import {eq, isNull} from 'drizzle-orm';
import {SharedReadModel} from '.';
import * as RA from 'fp-ts/ReadonlyArray';
import {
Expand All @@ -10,7 +10,9 @@ import {
membersTable,
trainedMemberstable,
trainersTable,
trainingQuizTable,
} from './state';
import { P } from 'pino';

export const getEquipment =
(db: BetterSQLite3Database): SharedReadModel['equipment']['get'] =>
Expand Down Expand Up @@ -57,11 +59,33 @@ export const getEquipment =
}))
);

const getTrainingQuizResults = () =>
pipe(
db
.select()
.from(trainingQuizTable)
.leftJoin( // If member info is null then this is an orphaned result.

)
.where(eq(trainingQuizTable.equipmentId, id))
.all(),
);

const getOrphanedTrainingQuizes = () =>
pipe(
db
.select().from(trainingQuizTable)
.where(isNull(trainingQuizTable.memberNumber))
.all(),
);

return pipe(
db.select().from(equipmentTable).where(eq(equipmentTable.id, id)).get(),
O.fromNullable,
O.let('trainers', getTrainers),
O.let('trainedMembers', getTrainedMembers),
O.let('membersAwaitingTraining', getTrainingQuizResults),
O.let('orphanedPassedQuizes', getOrphanedTrainingQuizes),
O.bind('area', ({areaId}) => getArea(areaId)),
foo => foo
);
Expand Down
1 change: 0 additions & 1 deletion src/read-models/shared-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {getEquipment} from './get-equipment';
import {Client} from '@libsql/client/.';
import {asyncRefresh} from './async-refresh';
import {updateState} from './update-state';
import { TrainingQuizResults, getTrainingQuizResults } from './training-results';

export {replayState} from './deprecated-replay';

Expand Down
39 changes: 38 additions & 1 deletion src/read-models/shared-state/return-types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import * as O from 'fp-ts/Option';
import {EmailAddress, GravatarHash} from '../../types';
import { DateTime } from 'luxon';

type OrphanedPassedQuiz = {
id: string;
score: number;
maxScore: number;
percentage: number;
passed: boolean;
timestamp: DateTime;

memberNumberProvided: O.Option<number>;
emailProvided: O.Option<string>;
};

type FailedQuizAttempt = MemberCoreInfo & {
quizId: string;
score: number;
maxScore: number;
percentage: number;
timestamp: DateTime;
quizAnswers: Record<string, string>;
}

type TrainedMember = MemberCoreInfo & {
trainedBy: number;
trainedSince: DateTime;
}

export type Equipment = {
id: string;
name: string;
trainers: ReadonlyArray<MemberCoreInfo>;
trainedMembers: ReadonlyArray<MemberCoreInfo>;
trainedMembers: ReadonlyArray<TrainedMember>;
area: {
id: string;
name: string;
};
membersAwaitingTraining: ReadonlyArray<MemberAwaitingTraining>;
orphanedPassedQuizes: ReadonlyArray<OrphanedPassedQuiz>;
failedQuizAttempts: ReadonlyArray<FailedQuizAttempt>;
trainingSheetId: O.Option<string>;
};

type TrainedOn = {
Expand All @@ -35,6 +66,12 @@ type MemberCoreInfo = {
gravatarHash: GravatarHash;
};

type MemberAwaitingTraining = MemberCoreInfo & {
quizId: string;
memberNumber: number;
waitingSince: DateTime;
};

export type Member = MemberCoreInfo & {
trainedOn: ReadonlyArray<TrainedOn>;
ownerOf: ReadonlyArray<OwnerOf>;
Expand Down
20 changes: 3 additions & 17 deletions src/read-models/shared-state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ export const equipmentTable = sqliteTable('equipment', {
areaId: text('areaId')
.notNull()
.references(() => areasTable.id),
trainingSheetId: text('trainingSheetId'),
});

const createEquipmentTable = sql`
CREATE TABLE IF NOT EXISTS equipment (
id TEXT,
name TEXT,
areaId TEXT
areaId TEXT,
trainingSheetId TEXT
);
`;

Expand Down Expand Up @@ -178,26 +180,10 @@ type Area = {
owners: Set<number>;
};

type UserAwaitingTraining = {
// Implies the user has passed the quiz.
quizId: string;
memberNumber: number;
waitingSince: DateTime;
}

type TrainedUser = {
memberNumber: number,
trainedSince: DateTime,
trainedByMemberNumber: number;
}

type Equipment = {
id: string;
name: string;
areaId: Area['id'];
trainedUsers: ReadonlyArray<TrainedUser>;
usersAwaitingTraining: ReadonlyArray<UserAwaitingTraining>;
orphanedPassedQuizes: ReadonlyArray<UserAwaitingTraining>;
};

type FailedLinking = {
Expand Down
51 changes: 0 additions & 51 deletions src/read-models/shared-state/training-results.ts

This file was deleted.

17 changes: 17 additions & 0 deletions src/read-models/shared-state/update-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ownersTable,
trainedMemberstable,
trainersTable,
trainingQuizTable,
} from './state';
import {BetterSQLite3Database} from 'drizzle-orm/better-sqlite3';
import {eq} from 'drizzle-orm';
Expand Down Expand Up @@ -116,7 +117,23 @@ export const updateState =
})
.run();
break;
case 'EquipmentTrainingQuizResult':
db.insert(trainingQuizTable)
.values({
memberNumberProvided: event.memberNumberProvided,


})
.run();
case 'EquipmentTrainingQuizEmailUpdated':
case 'EquipmentTrainingQuizMemberNumberUpdated':
break;
case 'EquipmentTrainingSheetRegistered':
db.update(equipmentTable)
.set({trainingSheetId: event.trainingSheetId})
.where(eq(equipmentTable.id, event.equipmentId))
.run();
break;
default:
break;
}
Expand Down

0 comments on commit d338b87

Please sign in to comment.