Skip to content

Commit

Permalink
Better reflect who trained who in csv output
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan2u committed Oct 20, 2024
1 parent fbf462a commit df828ff
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
12 changes: 11 additions & 1 deletion src/csv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
export function escapeCsv(cell: string | number): string {
import * as O from 'fp-ts/Option';

export function escapeCsv(
cell: string | number | boolean | O.None | O.Some<string | number | boolean>
): string {
if (typeof cell === 'number') {
return cell.toString();
}
if (typeof cell === 'boolean') {
return cell ? 'true' : 'false';
}
if (typeof cell === 'object') {
return O.isNone(cell) ? '' : escapeCsv(cell.value);
}
let requiresEscaping = false;
for (let i = 0; i < cell.length; ++i) {
const c = cell[i];
Expand Down
7 changes: 5 additions & 2 deletions src/queries/training-status-csv/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const render = (viewModel: ViewModel) => {
'',
'Trained Members',
'',
'area_id,area_name,equipment_id,equipment_name,member_number,email,name,trainedSince,markedTrainedBy',
'area_id,area_name,equipment_id,equipment_name,member_number,email,name,trainedSince,trainedByMemberNumber,trainedByEmail,markedTrainedBy,legacyImport',
];
for (const {
owners,
Expand Down Expand Up @@ -79,11 +79,14 @@ export const render = (viewModel: ViewModel) => {
equipmentName,
trained.memberNumber,
trained.emailAddress,
O.getOrElse(() => '')(trained.name),
trained.name,
trained.trainedSince.toISOString(),
trained.trainedByMemberNumber,
trained.trainedByEmail,
O.isSome(trained.markedTrainedByActor)
? renderActor(trained.markedTrainedByActor.value)
: '',
trained.legacyImport,
]
.map(escapeCsv)
.join(',')
Expand Down
30 changes: 22 additions & 8 deletions src/read-models/shared-state/equipment/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {UUID} from 'io-ts-types';
import {accumByMap} from '../../../util';
import {Actor} from '../../../types';
import {getAreaMinimal} from '../area/get';
import {getMemberCore} from '../member/get';

const expandTrainers =
(db: BetterSQLite3Database) =>
Expand Down Expand Up @@ -72,14 +73,27 @@ const expandTrainedMembers =
row => row.members.memberNumber,
rows => rows[rows.length - 1]
),
RA.map(result => ({
...result.members,
markedTrainedByActor: O.fromEither(
Actor.decode(result.trainedMembers.trainedByActor)
),
trainedSince: result.trainedMembers.trainedAt,
agreementSigned: O.fromNullable(result.members.agreementSigned),
})),
RA.map(result => {
const trainedByMemberNumber = O.fromNullable(
result.trainedMembers.trainedByMemberNumber
);
return {
...result.members,
markedTrainedByActor: O.fromEither(
Actor.decode(result.trainedMembers.markTrainedByActor)
),
trainedSince: result.trainedMembers.trainedAt,
agreementSigned: O.fromNullable(result.members.agreementSigned),
trainedByMemberNumber,
trainedByEmail: pipe(
trainedByMemberNumber,
O.map(getMemberCore(db)),
O.flatten,
O.map(trainedByDetails => trainedByDetails.emailAddress)
),
legacyImport: result.trainedMembers.legacyImport,
};
}),
trainedMembers => ({
...equipment,
trainedMembers,
Expand Down
4 changes: 2 additions & 2 deletions src/read-models/shared-state/member/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const expandTrainedOn =
id: trainedMemberstable.equipmentId,
name: equipmentTable.name,
trainedAt: trainedMemberstable.trainedAt,
trainedByActor: trainedMemberstable.trainedByActor,
trainedByActor: trainedMemberstable.markTrainedByActor,
})
.from(trainedMemberstable)
.leftJoin(
Expand All @@ -37,7 +37,7 @@ const expandTrainedOn =
RA.filter(fieldIsNotNull('name')),
RA.map(row => ({
...row,
trainedByActor: O.fromEither(Actor.decode(row.trainedByActor)),
markedTrainedByActor: O.fromEither(Actor.decode(row.trainedByActor)),
})),
trainedOn => ({
...member,
Expand Down
3 changes: 3 additions & 0 deletions src/read-models/shared-state/return-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ export type FailedQuizAttempt = MemberCoreInfo & {

export type TrainedMember = MemberCoreInfo & {
markedTrainedByActor: O.Option<Actor>;
trainedByMemberNumber: O.Option<number>;
trainedByEmail: O.Option<EmailAddress>;
trainedSince: Date;
legacyImport: boolean;
};

export type EpochTimestampMilliseconds = number & {
Expand Down
10 changes: 8 additions & 2 deletions src/read-models/shared-state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,21 @@ export const trainedMemberstable = sqliteTable('trainedMembers', {
.notNull()
.references(() => equipmentTable.id),
trainedAt: integer('trainedAt', {mode: 'timestamp'}).notNull(),
trainedByActor: text('trainedByActor', {mode: 'json'}).notNull(),
trainedByMemberNumber: integer('trainedByMemberNumber'),
legacyImport: integer('legacyImport', {mode: 'boolean'})
.notNull()
.default(false),
markTrainedByActor: text('markTrainedByActor', {mode: 'json'}).notNull(),
});

const createTrainedMembersTable = sql`
CREATE TABLE IF NOT EXISTS trainedMembers (
memberNumber INTEGER,
equipmentId TEXT,
trainedAt INTEGER,
trainedByActor TEXT
trainedByMemberNumber INTEGER,
legacyImport INTEGER,
markTrainedByActor TEXT
)
`;

Expand Down
7 changes: 5 additions & 2 deletions src/read-models/shared-state/update-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,19 @@ export const updateState =
})
.run();
break;
case 'MemberTrainedOnEquipment':
case 'MemberTrainedOnEquipment': {
db.insert(trainedMemberstable)
.values({
memberNumber: event.memberNumber,
equipmentId: event.equipmentId,
trainedAt: event.recordedAt,
trainedByActor: event.actor,
trainedByMemberNumber: event.trainedByMemberNumber,
legacyImport: event.legacyImport,
markTrainedByActor: event.actor,
})
.run();
break;
}
case 'OwnerAgreementSigned':
db.update(membersTable)
.set({agreementSigned: event.signedAt})
Expand Down

0 comments on commit df828ff

Please sign in to comment.