Skip to content

Commit 2a13cbf

Browse files
Cleanup
1 parent e27ac91 commit 2a13cbf

7 files changed

Lines changed: 435 additions & 209 deletions

File tree

app/filters/nunjucks.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ const log = (a, description = null) => {
88
}
99

1010
/**
11-
* Get user name by user ID
11+
* Get user name by user ID with format options
1212
* @param {string} userId - ID of the user
13-
* @returns {string} User's name
13+
* @param {Object} options - Display options
14+
* @param {boolean} [options.identifyCurrentUser=false] - Whether to add "(you)" for current user
15+
* @param {string} [options.format='full'] - Name format: 'full', 'short', or 'initial'
16+
* @returns {string} User's name in requested format
1417
*/
1518
const getUsername = function(userId, options={}) {
1619
if (!userId) return '';
@@ -21,12 +24,28 @@ const getUsername = function(userId, options={}) {
2124
const user = users.find(u => u.id === userId);
2225
if (!user) return userId;
2326

27+
// Format options: full (default), short (initial + surname), initial (just initials)
28+
const format = options.format || 'full';
29+
30+
let formattedName;
31+
switch (format) {
32+
case 'short':
33+
formattedName = `${user.firstName.charAt(0)}. ${user.lastName}`;
34+
break;
35+
case 'initial':
36+
formattedName = `${user.firstName.charAt(0)}${user.lastName.charAt(0)}`;
37+
break;
38+
case 'full':
39+
default:
40+
formattedName = `${user.firstName} ${user.lastName}`;
41+
}
42+
2443
const currentUser = this.ctx.data.currentUser;
2544
if (options.identifyCurrentUser && user.id === currentUser.id) {
26-
return `${user.firstName} ${user.lastName} (you)`;
45+
return `${formattedName} (you)`;
2746
}
2847

29-
else return `${user.firstName} ${user.lastName}`;
48+
return formattedName;
3049
}
3150

3251
/**

app/lib/generators/reading-generator.js

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,90 @@ const generateReadingData = (events, users) => {
138138
console.log(`Added first and second reads to ${count} events in the 2 oldest clinics`)
139139
}
140140

141+
// NEW: Add clinic where both reads are completed, but neither by the current user
142+
if (clinics.length >= 3) {
143+
let count = 0
144+
// Use the next clinic for this scenario
145+
const clinic = clinics[2]
146+
console.log(`Adding a clinic with both reads completed by users other than current user to clinic ${clinic.id}`)
147+
148+
// Use the same base time for all reads in this clinic, then advance by 1 minute for each event
149+
let baseReadTime = dayjs(generateRecentTimestamp(clinic.date, 30, 48))
150+
151+
// Add full first reads by third user
152+
clinic.events.forEach(event => {
153+
// Skip if already updated
154+
if (updatedEventIds.has(event.id)) return
155+
156+
// Find the event in our array
157+
const eventIndex = updatedEvents.findIndex(e => e.id === event.id)
158+
if (eventIndex === -1) return
159+
160+
// Ensure the imageReading structure exists
161+
if (!updatedEvents[eventIndex].imageReading) {
162+
updatedEvents[eventIndex].imageReading = { reads: {} }
163+
}
164+
165+
// Advance time by 1 minute for each read
166+
baseReadTime = baseReadTime.add(1, 'minute')
167+
168+
// First read (by third user)
169+
const firstResult = weighted.select(readResults)
170+
const firstReadTime = baseReadTime.toISOString()
171+
172+
updatedEvents[eventIndex].imageReading.reads[thirdReader.id] = {
173+
result: firstResult,
174+
readerId: thirdReader.id,
175+
readerType: thirdReader.role,
176+
timestamp: firstReadTime
177+
}
178+
179+
updatedEventIds.add(event.id)
180+
count++
181+
})
182+
183+
// Add second reads by second user to 60% of events
184+
const eventsForSecondRead = clinic.events
185+
.filter(event => updatedEventIds.has(event.id))
186+
.slice(0, Math.ceil(clinic.events.length * 0.6)) // Take 60% of events for second read
187+
188+
baseReadTime = dayjs(generateRecentTimestamp(clinic.date, 12, 24)) // More recent timestamp for second reads
189+
190+
eventsForSecondRead.forEach(event => {
191+
const eventIndex = updatedEvents.findIndex(e => e.id === event.id)
192+
if (eventIndex === -1) return
193+
194+
// Get the first read result
195+
const firstRead = updatedEvents[eventIndex].imageReading.reads[thirdReader.id]
196+
if (!firstRead) return
197+
198+
// Second read (by second user) - 80% chance of agreement
199+
let secondResult = firstRead.result
200+
if (Math.random() > 0.8) {
201+
// Different result for disagreement
202+
const otherResults = Object.keys(readResults).filter(r => r !== firstRead.result)
203+
secondResult = otherResults[Math.floor(Math.random() * otherResults.length)]
204+
}
205+
206+
// Advance time by 1-2 minutes for each read
207+
baseReadTime = baseReadTime.add(1 + Math.floor(Math.random() * 2), 'minute')
208+
const secondReadTime = baseReadTime.toISOString()
209+
210+
updatedEvents[eventIndex].imageReading.reads[secondReader.id] = {
211+
result: secondResult,
212+
readerId: secondReader.id,
213+
readerType: secondReader.role,
214+
timestamp: secondReadTime
215+
}
216+
})
217+
218+
console.log(`Added a clinic with ${clinic.events.length} first reads and ${eventsForSecondRead.length} second reads, both done by users other than current user`)
219+
}
220+
141221
// NEXT TWO CLINICS: First user (current user) reads all first, but no second reads
142-
if (clinics.length >= 4) {
222+
if (clinics.length >= 5) {
143223
let count = 0
144-
for (let i = 2; i < 4 && i < clinics.length; i++) {
224+
for (let i = 3; i < 5 && i < clinics.length; i++) {
145225
const clinic = clinics[i]
146226
console.log(`Adding first reads by current user to clinic ${clinic.id}`)
147227

@@ -183,9 +263,9 @@ const generateReadingData = (events, users) => {
183263
}
184264

185265
// NEXT TWO CLINICS: Second user reads all first, waiting for first user (current user) to do second reads
186-
if (clinics.length >= 6) {
266+
if (clinics.length >= 7) {
187267
let count = 0
188-
for (let i = 4; i < 6 && i < clinics.length; i++) {
268+
for (let i = 5; i < 7 && i < clinics.length; i++) {
189269
const clinic = clinics[i]
190270
console.log(`Adding first reads by second user to clinic ${clinic.id}`)
191271

@@ -227,9 +307,9 @@ const generateReadingData = (events, users) => {
227307
}
228308

229309
// NEXT TWO OLDEST CLINICS: 75% first read by third user
230-
if (clinics.length >= 8) {
310+
if (clinics.length >= 9) {
231311
let count = 0
232-
for (let i = 6; i < 8 && i < clinics.length; i++) {
312+
for (let i = 7; i < 9 && i < clinics.length; i++) {
233313
const clinic = clinics[i]
234314
console.log(`Adding partial first reads to clinic ${clinic.id}`)
235315

app/routes/reading.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,18 @@ module.exports = router => {
407407

408408
// Route for viewing a batch
409409
router.get('/reading/batch/:batchId', (req, res) => {
410+
// Default to "your-reads" view
411+
res.redirect(`/reading/batch/${req.params.batchId}/your-reads`)
412+
})
413+
414+
// Route for viewing a batch with specific view
415+
router.get('/reading/batch/:batchId/:view', (req, res) => {
410416
const data = req.session.data
411-
const { batchId } = req.params
417+
const { batchId, view } = req.params
418+
const validViews = ['your-reads', 'all-reads']
419+
420+
// Validate view parameter
421+
const selectedView = validViews.includes(view) ? view : 'your-reads'
412422

413423
// Get the batch
414424
const batch = getReadingBatch(data, batchId)
@@ -450,9 +460,58 @@ module.exports = router => {
450460
events: enhancedEvents,
451461
readingStatus,
452462
firstUserReadableEvent,
453-
clinic
463+
clinic,
464+
view: selectedView
454465
})
455466
})
467+
// Route for viewing a batch
468+
// router.get('/reading/batch/:batchId', (req, res) => {
469+
// const data = req.session.data
470+
// const { batchId } = req.params
471+
472+
// // Get the batch
473+
// const batch = getReadingBatch(data, batchId)
474+
// if (!batch) {
475+
// // req.flash('error', 'Batch not found')
476+
// return res.redirect('/reading')
477+
// }
478+
479+
// // Get enhanced events with reading metadata
480+
// const enhancedEvents = batch.eventIds
481+
// .map(eventId => data.events.find(e => e.id === eventId))
482+
// .filter(Boolean)
483+
// .map(event => {
484+
// // Add participant data and reading metadata
485+
// const participant = data.participants.find(p => p.id === event.participantId)
486+
// const metadata = getReadingMetadata(event)
487+
488+
// return {
489+
// ...event,
490+
// participant,
491+
// readingMetadata: metadata
492+
// }
493+
// })
494+
495+
// // Get reading status for the batch
496+
// const readingStatus = getReadingStatusForEvents(enhancedEvents, data.currentUser.id)
497+
498+
// // Find first event user can read
499+
// const firstUserReadableEvent = getFirstUserReadableEvent(enhancedEvents, data.currentUser.id)
500+
501+
// // Get clinic data if this is a clinic batch
502+
// let clinic = null
503+
// if (batch.clinicId) {
504+
// clinic = data.clinics.find(c => c.id === batch.clinicId)
505+
// }
506+
507+
// res.render('reading/batch', {
508+
// batch,
509+
// events: enhancedEvents,
510+
// readingStatus,
511+
// firstUserReadableEvent,
512+
// clinic
513+
// })
514+
// })
456515

457516
// Add middleware for batch-based events similar to the clinic-based ones
458517
router.use('/reading/batch/:batchId/events/:eventId', (req, res, next) => {

0 commit comments

Comments
 (0)