Skip to content

Commit

Permalink
fix(apps/func-migration-v3-import): fix creation of question instance…
Browse files Browse the repository at this point in the history
…s and linking of imported tags to questions
  • Loading branch information
rschlaefli committed Dec 29, 2023
1 parent b1d609e commit f63d29f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 737 deletions.
4 changes: 2 additions & 2 deletions apps/func-incoming-responses/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dist/"
],
"dependencies": {
"@azure/functions": "4.0.0",
"@azure/functions": "4.1.0",
"@azure/service-bus": "7.9.0",
"@sentry/node": "7.52.1",
"jsonwebtoken": "9.0.1"
Expand All @@ -19,7 +19,7 @@
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^18.17.4",
"@uzh-bf/design-system": "2.4.7",
"azure-functions-core-tools": "4.0.5390",
"azure-functions-core-tools": "4.0.5455",
"cross-env": "7.0.3",
"dotenv": "16.0.3",
"eslint": "8.45.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/func-migration-v2-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dist/"
],
"dependencies": {
"@azure/functions": "4.0.0",
"@azure/functions": "4.1.0",
"@azure/storage-blob": "12.15.0",
"jsonwebtoken": "9.0.1",
"mongodb": "5.7.0"
Expand All @@ -18,7 +18,7 @@
"@tsconfig/recommended": "^1.0.2",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^18.17.4",
"azure-functions-core-tools": "4.0.5390",
"azure-functions-core-tools": "4.0.5455",
"cross-env": "7.0.3",
"eslint": "8.45.0",
"npm-run-all": "4.1.5",
Expand Down
4 changes: 2 additions & 2 deletions apps/func-migration-v3-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dist/"
],
"dependencies": {
"@azure/functions": "4.0.0",
"@azure/functions": "4.1.0",
"@azure/storage-blob": "12.15.0",
"@klicker-uzh/prisma": "workspace:*",
"axios": "1.4.0",
Expand All @@ -21,7 +21,7 @@
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^18.17.4",
"@types/uuid": "^9.0.2",
"azure-functions-core-tools": "4.0.5390",
"azure-functions-core-tools": "4.0.5455",
"cross-env": "7.0.3",
"eslint": "8.45.0",
"npm-run-all": "4.1.5",
Expand Down
4 changes: 2 additions & 2 deletions apps/func-migration-v3-import/src/importQuestionInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const importQuestionInstances = async (
return []
})

await Promise.allSettled(
await Promise.all(
preparedQuestionInstances.map((questionInstance) =>
prisma.$transaction(async (prisma) => {
const newQuestionInstance = await prisma.questionInstance.create({
Expand All @@ -176,7 +176,7 @@ export const importQuestionInstances = async (
if (questionInstance.questionData.id) {
await prisma.element.update({
where: {
id: questionInstance.questionData.id,
id: questionInstance.questionData.questionId,
},
data: {
instances: {
Expand Down
88 changes: 48 additions & 40 deletions apps/func-migration-v3-import/src/importSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export const importSessions = async (
let mappedSessionIds: Record<string, string> = {}
const sessionsInDb = await prisma.liveSession.findMany({
where: {
originalId: {
not: null,
},
owner: {
id: user.id,
},
Expand All @@ -66,13 +69,9 @@ export const importSessions = async (
return []
}

if (sessionExists) {
mappedSessionIds[session._id] = sessionExists.id
return []
}

return [
{
sessionExists,
originalData: session,
prismaData: {
data: {
Expand Down Expand Up @@ -188,47 +187,56 @@ export const importSessions = async (
})

await Promise.allSettled(
preparedSessions.map(async ({ originalData, prismaData }) => {
return prisma.$transaction(async (prisma) => {
const createdSession = await prisma.liveSession.create(prismaData)
mappedSessionIds[createdSession.originalId] = createdSession.id

// Update sessionBlockId of each QuestionInstance connected to the newly created SessionBlock and restore ordering of QuestionInstances
for (const block of createdSession.blocks) {
if (!block || !block.instances) {
continue // skip to the next iteration if there are no instances
}

const reducedOldBlocks = originalData.blocks.reduce(
(acc, block) => {
acc[block._id] = block
return acc
},
{}
)
preparedSessions.map(
async ({ sessionExists, originalData, prismaData }) => {
return prisma.$transaction(async (prisma) => {
const createdSession = sessionExists
? await prisma.liveSession.update({
where: {
id: sessionExists.id,
},
data: prismaData.data,
})
: await prisma.liveSession.create(prismaData)
mappedSessionIds[createdSession.originalId] = createdSession.id

// Update sessionBlockId of each QuestionInstance connected to the newly created SessionBlock and restore ordering of QuestionInstances
for (const block of createdSession.blocks) {
if (!block || !block.instances) {
continue // skip to the next iteration if there are no instances
}

for (const instance of block.instances) {
const oldBlock = reducedOldBlocks[block.originalId]
const i = oldBlock?.instances?.findIndex(
(id) => id === instance.originalId
const reducedOldBlocks = originalData.blocks.reduce(
(acc, block) => {
acc[block._id] = block
return acc
},
{}
)

let updateData = {
sessionBlockId: block.id,
}
for (const instance of block.instances) {
const oldBlock = reducedOldBlocks[block.originalId]
const i = oldBlock?.instances?.findIndex(
(id) => id === instance.originalId
)

if (i || i === 0) {
updateData['order'] = i
}
let updateData = {
sessionBlockId: block.id,
}

if (i || i === 0) {
updateData['order'] = i
}

await prisma.questionInstance.update({
where: { id: instance.id },
data: updateData,
})
await prisma.questionInstance.update({
where: { id: instance.id },
data: updateData,
})
}
}
}
})
})
})
}
)
)
}

Expand Down
2 changes: 1 addition & 1 deletion apps/func-migration-v3-import/src/importTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export async function importTags(

console.log('ordered tags: ', orderedTags)

return orderedTags
return mappedTags
} catch (error) {
context.error('Something went wrong while importing tags: ', error)
await sendTeamsNotifications(
Expand Down
4 changes: 2 additions & 2 deletions apps/func-response-processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dist/"
],
"dependencies": {
"@azure/functions": "4.0.0",
"@azure/functions": "4.1.0",
"@klicker-uzh/grading": "workspace:*",
"@sentry/node": "7.52.1",
"ioredis": "5.3.2",
Expand All @@ -24,7 +24,7 @@
"@types/node": "^18.17.4",
"@types/ramda": "^0.29.3",
"@uzh-bf/design-system": "2.4.7",
"azure-functions-core-tools": "4.0.5390",
"azure-functions-core-tools": "4.0.5455",
"cross-env": "7.0.3",
"dotenv": "16.0.3",
"eslint": "8.45.0",
Expand Down
Loading

0 comments on commit f63d29f

Please sign in to comment.