@@ -2,6 +2,7 @@ import { prisma } from '@klicker-uzh/prisma'
22import {
33 AchievementType ,
44 CourseAuthType ,
5+ ElementBlockStatus ,
56 ElementType ,
67 ObjectAccess ,
78 PermissionLevel ,
@@ -21,6 +22,7 @@ import {
2122 ElementOptionsSelection ,
2223} from '@klicker-uzh/types'
2324import bcrypt from 'bcryptjs'
25+ import { createHash } from 'crypto'
2426import { defineConfig } from 'cypress'
2527import cypressSplit from 'cypress-split'
2628// import cypressCodeCoverage from '@cypress/code-coverage/task'
@@ -1708,6 +1710,132 @@ export default defineConfig({
17081710 throw error
17091711 }
17101712 } ,
1713+ async seedWordCloudLiveQuizResponses ( {
1714+ freeTextAnswer,
1715+ freeTextTitle,
1716+ numericalAnswer,
1717+ numericalTitle,
1718+ quizName,
1719+ secondFreeTextAnswer,
1720+ secondFreeTextTitle,
1721+ } : {
1722+ freeTextAnswer : string
1723+ freeTextTitle : string
1724+ numericalAnswer : string
1725+ numericalTitle : string
1726+ quizName : string
1727+ secondFreeTextAnswer : string
1728+ secondFreeTextTitle : string
1729+ } ) {
1730+ try {
1731+ const liveQuiz = await prisma . liveQuiz . findFirst ( {
1732+ where : { name : quizName , isDeleted : false } ,
1733+ include : {
1734+ blocks : {
1735+ include : { elements : true } ,
1736+ orderBy : { order : 'asc' } ,
1737+ } ,
1738+ } ,
1739+ } )
1740+
1741+ if ( ! liveQuiz ) {
1742+ throw new Error ( `Live quiz ${ quizName } not found` )
1743+ }
1744+
1745+ const block = liveQuiz . blocks [ 0 ]
1746+ if ( ! block ) {
1747+ throw new Error ( `Live quiz ${ quizName } has no blocks` )
1748+ }
1749+
1750+ const instances = liveQuiz . blocks . flatMap ( ( block ) => block . elements )
1751+ const getInstance = ( title : string , type : ElementType ) => {
1752+ const instance = instances . find (
1753+ ( element ) =>
1754+ element . elementType === type &&
1755+ typeof element . elementData === 'object' &&
1756+ element . elementData !== null &&
1757+ 'name' in element . elementData &&
1758+ element . elementData . name === title
1759+ )
1760+
1761+ if ( ! instance ) {
1762+ throw new Error (
1763+ `Instance ${ title } (${ type } ) not found in live quiz ${ quizName } `
1764+ )
1765+ }
1766+
1767+ return instance
1768+ }
1769+
1770+ const numericalInstance = getInstance (
1771+ numericalTitle ,
1772+ ElementType . NUMERICAL
1773+ )
1774+ const freeTextInstance = getInstance (
1775+ freeTextTitle ,
1776+ ElementType . FREE_TEXT
1777+ )
1778+ const secondFreeTextInstance = getInstance (
1779+ secondFreeTextTitle ,
1780+ ElementType . FREE_TEXT
1781+ )
1782+
1783+ const openResults = ( value : string , normalize : boolean ) => {
1784+ const normalizedValue = normalize
1785+ ? value . trim ( ) . toLowerCase ( )
1786+ : String ( parseFloat ( value ) )
1787+ const hash = createHash ( 'md5' )
1788+ . update ( normalizedValue )
1789+ . digest ( 'hex' )
1790+
1791+ return {
1792+ responses : {
1793+ [ hash ] : {
1794+ value : normalizedValue ,
1795+ count : 1 ,
1796+ } ,
1797+ } ,
1798+ total : 1 ,
1799+ }
1800+ }
1801+
1802+ await prisma . $transaction ( [
1803+ prisma . elementInstance . update ( {
1804+ where : { id : numericalInstance . id } ,
1805+ data : {
1806+ anonymousResults : openResults ( numericalAnswer , false ) ,
1807+ } ,
1808+ } ) ,
1809+ prisma . elementInstance . update ( {
1810+ where : { id : freeTextInstance . id } ,
1811+ data : {
1812+ anonymousResults : openResults ( freeTextAnswer , true ) ,
1813+ } ,
1814+ } ) ,
1815+ prisma . elementInstance . update ( {
1816+ where : { id : secondFreeTextInstance . id } ,
1817+ data : {
1818+ anonymousResults : openResults ( secondFreeTextAnswer , true ) ,
1819+ } ,
1820+ } ) ,
1821+ prisma . elementBlock . update ( {
1822+ where : { id : block . id } ,
1823+ data : {
1824+ closedAt : new Date ( ) ,
1825+ status : ElementBlockStatus . EXECUTED ,
1826+ } ,
1827+ } ) ,
1828+ prisma . liveQuiz . update ( {
1829+ where : { id : liveQuiz . id } ,
1830+ data : { activeBlockId : null } ,
1831+ } ) ,
1832+ ] )
1833+
1834+ return true
1835+ } catch ( error ) {
1836+ throw error
1837+ }
1838+ } ,
17111839 async deleteLiveQuiz ( { name } : { name : string } ) {
17121840 try {
17131841 const liveQuiz = await prisma . liveQuiz . findFirst ( {
0 commit comments