|
| 1 | +/** |
| 2 | + * Optional pre-step for seedCourseAwards.ts: derives the round's `derivedAward` |
| 3 | + * from in-platform behaviour instead of from the lecturer workbook. |
| 4 | + * |
| 5 | + * Rule: a participant qualifies when they answered every element instance of |
| 6 | + * every non-deleted microlearning in the course ("completed all microlearnings"). |
| 7 | + * |
| 8 | + * Completion is read from QuestionResponse, never from |
| 9 | + * ParticipantActivityPerformance.completion or MicroLearning.completedCount / |
| 10 | + * startedCount: for the Summer School 2026 course all three are empty (zero rows, |
| 11 | + * zero counters) even though responses exist, so they derive zero awards silently |
| 12 | + * instead of failing. |
| 13 | + * |
| 14 | + * Reads the gitignored `_local/<round>_base.json` (workbook-derived rows without |
| 15 | + * the derived award) and writes `_local/<round>_data.json` for the seed. Executes |
| 16 | + * zero database writes; freezing the result into the payload keeps the seed's |
| 17 | + * payload-hash replay safety intact. |
| 18 | + * |
| 19 | + * Usage: |
| 20 | + * ROUND=<key> pnpm --filter @klicker-uzh/prisma-data seed:prod:course-awards:prepare |
| 21 | + */ |
| 22 | +import { prisma } from '@klicker-uzh/prisma' |
| 23 | +import fs from 'node:fs' |
| 24 | +import { resolveRound, roundFile } from './courseAwardRounds.js' |
| 25 | + |
| 26 | +interface BaseEntry { |
| 27 | + username: string |
| 28 | + points?: number |
| 29 | + awards?: string[] |
| 30 | +} |
| 31 | + |
| 32 | +const { key: ROUND, config } = resolveRound() |
| 33 | +const baseUrl = roundFile(ROUND, 'base.json') |
| 34 | +const outputUrl = roundFile(ROUND, 'data.json') |
| 35 | + |
| 36 | +async function main() { |
| 37 | + const derivedAward = config.derivedAward |
| 38 | + if (!derivedAward) { |
| 39 | + throw new Error(`Round ${ROUND} declares no derivedAward`) |
| 40 | + } |
| 41 | + if (!config.awards.some((award) => award.key === derivedAward)) { |
| 42 | + throw new Error( |
| 43 | + `Round ${ROUND} derives ${derivedAward}, which is not one of its awards` |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + const base: BaseEntry[] = JSON.parse(fs.readFileSync(baseUrl, 'utf-8')) |
| 48 | + console.log(`Base payload entries: ${base.length}`) |
| 49 | + |
| 50 | + const microLearnings = await prisma.microLearning.findMany({ |
| 51 | + where: { courseId: config.courseId, isDeleted: false }, |
| 52 | + select: { |
| 53 | + id: true, |
| 54 | + name: true, |
| 55 | + status: true, |
| 56 | + stacks: { select: { elements: { select: { id: true } } } }, |
| 57 | + }, |
| 58 | + orderBy: { scheduledStartAt: 'asc' }, |
| 59 | + }) |
| 60 | + |
| 61 | + if (microLearnings.length === 0) { |
| 62 | + throw new Error(`Course ${config.courseId} has no microlearnings`) |
| 63 | + } |
| 64 | + |
| 65 | + const required = microLearnings.map((microLearning) => { |
| 66 | + const instanceIds = microLearning.stacks.flatMap((stack) => |
| 67 | + stack.elements.map((element) => element.id) |
| 68 | + ) |
| 69 | + if (instanceIds.length === 0) { |
| 70 | + throw new Error( |
| 71 | + `Microlearning ${microLearning.name} has no element instances` |
| 72 | + ) |
| 73 | + } |
| 74 | + return { ...microLearning, instanceIds } |
| 75 | + }) |
| 76 | + |
| 77 | + console.log( |
| 78 | + `\nMicrolearnings in course ${config.courseId}: ${required.length}` |
| 79 | + ) |
| 80 | + for (const microLearning of required) { |
| 81 | + console.log( |
| 82 | + ` ${microLearning.name} | ${microLearning.status} | ${microLearning.instanceIds.length} instances` |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + const participants = await prisma.participant.findMany({ |
| 87 | + where: { |
| 88 | + OR: base.map((entry) => ({ |
| 89 | + username: { equals: entry.username, mode: 'insensitive' as const }, |
| 90 | + })), |
| 91 | + }, |
| 92 | + select: { id: true, username: true }, |
| 93 | + }) |
| 94 | + |
| 95 | + const byUsername = new Map<string, typeof participants>() |
| 96 | + for (const participant of participants) { |
| 97 | + const key = participant.username.toLocaleLowerCase('en-US') |
| 98 | + byUsername.set(key, [...(byUsername.get(key) ?? []), participant]) |
| 99 | + } |
| 100 | + |
| 101 | + const resolved = base.map((entry) => { |
| 102 | + const candidates = byUsername.get(entry.username.toLocaleLowerCase('en-US')) |
| 103 | + const candidate = candidates?.[0] |
| 104 | + if (candidates?.length !== 1 || !candidate) { |
| 105 | + throw new Error( |
| 106 | + `Expected exactly one participant match for ${entry.username}, found ${candidates?.length ?? 0}` |
| 107 | + ) |
| 108 | + } |
| 109 | + return { ...entry, participantId: candidate.id } |
| 110 | + }) |
| 111 | + |
| 112 | + const responses = await prisma.questionResponse.findMany({ |
| 113 | + where: { |
| 114 | + microLearningId: { in: required.map((item) => item.id) }, |
| 115 | + participantId: { in: resolved.map((entry) => entry.participantId) }, |
| 116 | + }, |
| 117 | + select: { |
| 118 | + participantId: true, |
| 119 | + microLearningId: true, |
| 120 | + elementInstanceId: true, |
| 121 | + }, |
| 122 | + }) |
| 123 | + |
| 124 | + const answered = new Map<string, Set<number>>() |
| 125 | + for (const response of responses) { |
| 126 | + const key = `${response.participantId}:${response.microLearningId}` |
| 127 | + const set = answered.get(key) ?? new Set<number>() |
| 128 | + set.add(response.elementInstanceId) |
| 129 | + answered.set(key, set) |
| 130 | + } |
| 131 | + |
| 132 | + const completedPerMicroLearning = new Map<string, number>() |
| 133 | + const histogram = new Map<number, number>() |
| 134 | + |
| 135 | + const entries = resolved.map((entry) => { |
| 136 | + const completed = required.filter((microLearning) => { |
| 137 | + const set = |
| 138 | + answered.get(`${entry.participantId}:${microLearning.id}`) ?? |
| 139 | + new Set<number>() |
| 140 | + return microLearning.instanceIds.every((id) => set.has(id)) |
| 141 | + }) |
| 142 | + |
| 143 | + for (const microLearning of completed) { |
| 144 | + completedPerMicroLearning.set( |
| 145 | + microLearning.name, |
| 146 | + (completedPerMicroLearning.get(microLearning.name) ?? 0) + 1 |
| 147 | + ) |
| 148 | + } |
| 149 | + histogram.set(completed.length, (histogram.get(completed.length) ?? 0) + 1) |
| 150 | + |
| 151 | + const { participantId: _participantId, ...rest } = entry |
| 152 | + const awards = (rest.awards ?? []).filter((award) => award !== derivedAward) |
| 153 | + return { |
| 154 | + ...rest, |
| 155 | + awards: |
| 156 | + completed.length === required.length |
| 157 | + ? [...awards, derivedAward] |
| 158 | + : awards, |
| 159 | + } |
| 160 | + }) |
| 161 | + |
| 162 | + console.log('\nFully completed, per microlearning:') |
| 163 | + for (const microLearning of required) { |
| 164 | + console.log( |
| 165 | + ` ${microLearning.name}: ${completedPerMicroLearning.get(microLearning.name) ?? 0} of ${entries.length}` |
| 166 | + ) |
| 167 | + } |
| 168 | + console.log('Fully completed microlearnings per participant:') |
| 169 | + for (const [completed, count] of [...histogram].sort((a, b) => a[0] - b[0])) { |
| 170 | + console.log(` ${completed}/${required.length}: ${count} participants`) |
| 171 | + } |
| 172 | + |
| 173 | + const derivedCount = entries.filter((entry) => |
| 174 | + entry.awards.includes(derivedAward) |
| 175 | + ).length |
| 176 | + console.log(`\n${derivedAward} awards: ${derivedCount}`) |
| 177 | + console.log( |
| 178 | + `Point delta: ${entries.reduce((sum, entry) => sum + (entry.points ?? 0), 0)}` |
| 179 | + ) |
| 180 | + for (const award of config.awards) { |
| 181 | + console.log( |
| 182 | + `${award.key}: ${entries.filter((entry) => entry.awards.includes(award.key)).length}` |
| 183 | + ) |
| 184 | + } |
| 185 | + |
| 186 | + fs.mkdirSync(new URL('.', outputUrl), { recursive: true }) |
| 187 | + fs.writeFileSync(outputUrl, `${JSON.stringify(entries, null, 2)}\n`) |
| 188 | + console.log(`\nSeed payload written: ${outputUrl.pathname}`) |
| 189 | +} |
| 190 | + |
| 191 | +main() |
| 192 | + .catch((error) => { |
| 193 | + console.error(error) |
| 194 | + process.exitCode = 1 |
| 195 | + }) |
| 196 | + .finally(async () => { |
| 197 | + await prisma.$disconnect() |
| 198 | + }) |
0 commit comments