Skip to content

Commit

Permalink
Show trained status on me page #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Haarhoff committed Aug 8, 2024
1 parent 457602b commit e7e7138
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
40 changes: 35 additions & 5 deletions src/queries/me/render.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import {pipe} from 'fp-ts/lib/function';
import * as RA from 'fp-ts/ReadonlyArray';
import {getGravatarThumbnail} from '../../templates/avatar';
import {html, sanitizeOption, safe, sanitizeString} from '../../types/html';
import {
html,
sanitizeOption,
safe,
sanitizeString,
joinHtml,
} from '../../types/html';
import {ViewModel} from './view-model';
import {pageTemplate} from '../../templates';
import {renderMemberNumber} from '../../templates/member-number';
import {superUserNav} from '../landing/render';
import {displayDate} from '../../templates/display-date';
import {DateTime} from 'luxon';

const editName = (viewModel: ViewModel) =>
html`<a href="/members/edit-name?member=${viewModel.member.memberNumber}"
Expand Down Expand Up @@ -55,9 +64,30 @@ const renderMemberDetails = (viewModel: ViewModel) => html`
</table>
`;

const renderTrainingStatus = () => html`
<p>You are currently not allowed to use any RED equipment.</p>
`;
const renderTrainingStatus = (trainedOn: ViewModel['member']['trainedOn']) =>
pipe(
trainedOn,
RA.map(
equipment =>
html`<li>
<a href="/equipment/${safe(equipment.id)}"
>${sanitizeString(equipment.name)}</a
>
(since ${displayDate(DateTime.fromJSDate(equipment.trainedAt))})
</li>`
),
RA.match(
() => html`
<p>You are currently not allowed to use any RED equipment.</p>
`,
listItems => html`
<p>You are permitted to use the following RED equipment:</p>
<ul>
${joinHtml(listItems)}
</ul>
`
)
);

export const render = (viewModel: ViewModel) =>
pipe(
Expand All @@ -66,7 +96,7 @@ export const render = (viewModel: ViewModel) =>
<h2>Your details</h2>
${renderMemberDetails(viewModel)}
<h2>Training status</h2>
${renderTrainingStatus()}
${renderTrainingStatus(viewModel.member.trainedOn)}
${viewModel.member.isSuperUser ? superUserNav : ''}
`,
pageTemplate(safe('Member'), viewModel.user)
Expand Down
2 changes: 1 addition & 1 deletion src/queries/me/view-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Member} from '../../read-models/members';
import {Member} from '../../read-models/shared-state/return-types';
import {User} from '../../types';

export type ViewModel = {
Expand Down
4 changes: 2 additions & 2 deletions src/read-models/shared-state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ export const trainedMemberstable = sqliteTable('trainedMembers', {
equipmentId: text('equipmentId')
.notNull()
.references(() => equipmentTable.id),
trainedAt: blob('trainedAt', {mode: 'json'}).notNull().$type<Date>(),
trainedAt: integer('trainedAt', {mode: 'timestamp'}).notNull(),
});

const createTrainedMembersTable = sql`
CREATE TABLE IF NOT EXISTS trainedMembers (
memberNumber INTEGER,
equipmentID TEXT,
trainedAt BLOB
trainedAt INTEGER
)
`;

Expand Down
5 changes: 2 additions & 3 deletions tests/read-models/shared-state/get-member.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe('get-via-shared-read-model', () => {
areaId: createArea.id,
};
const trainedAt = faker.date.future();
trainedAt.setMilliseconds(0);
beforeEach(async () => {
advanceTo(faker.date.past());
await framework.commands.area.create(createArea);
Expand All @@ -193,9 +194,7 @@ describe('get-via-shared-read-model', () => {

it('returns date they were marked as trained', () => {
const result = runQuery();
expect(result.trainedOn[0].trainedAt).toStrictEqual(
trainedAt.toISOString()
);
expect(result.trainedOn[0].trainedAt).toStrictEqual(trainedAt);
});
});
});
Expand Down

0 comments on commit e7e7138

Please sign in to comment.