|
| 1 | +import type { UUID } from 'digital-fuesim-manv-shared'; |
| 2 | +import { applyMigrations } from 'digital-fuesim-manv-shared'; |
| 3 | +import type { EntityManager } from 'typeorm'; |
| 4 | +import { RestoreError } from '../utils/restore-error'; |
| 5 | +import { ActionWrapperEntity } from './entities/action-wrapper.entity'; |
| 6 | +import { ExerciseWrapperEntity } from './entities/exercise-wrapper.entity'; |
| 7 | + |
| 8 | +export async function migrateInDatabase( |
| 9 | + exerciseId: UUID, |
| 10 | + entityManager: EntityManager |
| 11 | +): Promise<void> { |
| 12 | + const exercise = await entityManager.findOne(ExerciseWrapperEntity, { |
| 13 | + where: { id: exerciseId }, |
| 14 | + }); |
| 15 | + if (exercise === null) { |
| 16 | + throw new RestoreError( |
| 17 | + 'Cannot find exercise to convert in database', |
| 18 | + exerciseId |
| 19 | + ); |
| 20 | + } |
| 21 | + const initialState = JSON.parse(exercise.initialStateString); |
| 22 | + const currentState = JSON.parse(exercise.currentStateString); |
| 23 | + const actions = ( |
| 24 | + await entityManager.find(ActionWrapperEntity, { |
| 25 | + where: { exercise: { id: exerciseId } }, |
| 26 | + select: { actionString: true }, |
| 27 | + order: { index: 'ASC' }, |
| 28 | + }) |
| 29 | + ).map((action) => JSON.parse(action.actionString)); |
| 30 | + const newVersion = applyMigrations(exercise.stateVersion, { |
| 31 | + currentState, |
| 32 | + history: { |
| 33 | + initialState, |
| 34 | + actions, |
| 35 | + }, |
| 36 | + }); |
| 37 | + exercise.stateVersion = newVersion; |
| 38 | + // Save exercise wrapper |
| 39 | + const patch: Partial<ExerciseWrapperEntity> = { |
| 40 | + stateVersion: exercise.stateVersion, |
| 41 | + }; |
| 42 | + patch.initialStateString = JSON.stringify(initialState); |
| 43 | + patch.currentStateString = JSON.stringify(currentState); |
| 44 | + await entityManager.update( |
| 45 | + ExerciseWrapperEntity, |
| 46 | + { id: exerciseId }, |
| 47 | + patch |
| 48 | + ); |
| 49 | + // Save actions |
| 50 | + if (actions !== undefined) { |
| 51 | + let patchedActionsIndex = 0; |
| 52 | + const indicesToRemove: number[] = []; |
| 53 | + const actionsToUpdate: { |
| 54 | + previousIndex: number; |
| 55 | + newIndex: number; |
| 56 | + actionString: string; |
| 57 | + }[] = []; |
| 58 | + actions.forEach((action, i) => { |
| 59 | + if (action === null) { |
| 60 | + indicesToRemove.push(i); |
| 61 | + return; |
| 62 | + } |
| 63 | + actionsToUpdate.push({ |
| 64 | + previousIndex: i, |
| 65 | + newIndex: patchedActionsIndex++, |
| 66 | + actionString: JSON.stringify(action), |
| 67 | + }); |
| 68 | + }); |
| 69 | + if (indicesToRemove.length > 0) { |
| 70 | + await entityManager |
| 71 | + .createQueryBuilder() |
| 72 | + .delete() |
| 73 | + .from(ActionWrapperEntity) |
| 74 | + // eslint-disable-next-line unicorn/string-content |
| 75 | + .where('index IN (:...ids)', { ids: indicesToRemove }) |
| 76 | + .execute(); |
| 77 | + } |
| 78 | + if (actionsToUpdate.length > 0) { |
| 79 | + await Promise.all( |
| 80 | + actionsToUpdate.map( |
| 81 | + async ({ previousIndex, newIndex, actionString }) => |
| 82 | + entityManager.update( |
| 83 | + ActionWrapperEntity, |
| 84 | + { |
| 85 | + index: previousIndex, |
| 86 | + exercise: { id: exerciseId }, |
| 87 | + }, |
| 88 | + { actionString, index: newIndex } |
| 89 | + ) |
| 90 | + ) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments