summaryrefslogtreecommitdiff
path: root/apps/web/src/services/storage/migrations/transformers/v5-to-v6.ts
blob: ca8551494baf743dbd13b3dcfeef7ff881c5a974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import type { MigrationResult, ProjectRecord } from "./types";
import { getProjectId, isRecord } from "./utils";

export function transformProjectV5ToV6({
	project,
}: {
	project: ProjectRecord;
}): MigrationResult<ProjectRecord> {
	const projectId = getProjectId({ project });
	if (!projectId) {
		return { project, skipped: true, reason: "no project id" };
	}

	if (isV6Project({ project })) {
		return { project, skipped: true, reason: "already v6" };
	}

	const migratedProject = migrateProjectBookmarks({ project });

	return {
		project: {
			...migratedProject,
			version: 6,
		},
		skipped: false,
	};
}

function migrateProjectBookmarks({
	project,
}: {
	project: ProjectRecord;
}): ProjectRecord {
	const scenesValue = project.scenes;
	if (!Array.isArray(scenesValue)) {
		return project;
	}

	let hasSceneChanges = false;
	const migratedScenes = scenesValue.map((scene) => {
		const migratedScene = migrateSceneBookmarks({ scene });
		if (migratedScene !== scene) {
			hasSceneChanges = true;
		}
		return migratedScene;
	});

	if (!hasSceneChanges) {
		return project;
	}

	return {
		...project,
		scenes: migratedScenes,
	};
}

function migrateSceneBookmarks({ scene }: { scene: unknown }): unknown {
	if (!isRecord(scene)) {
		return scene;
	}

	const bookmarksValue = scene.bookmarks;
	if (!Array.isArray(bookmarksValue)) {
		return scene;
	}

	const needsMigration = bookmarksValue.some(
		(bookmark) => typeof bookmark === "number",
	);
	if (!needsMigration) {
		return scene;
	}

	const migratedBookmarks = bookmarksValue.map((bookmark) =>
		typeof bookmark === "number" ? { time: bookmark } : bookmark,
	);

	return {
		...scene,
		bookmarks: migratedBookmarks,
	};
}

function isV6Project({ project }: { project: ProjectRecord }): boolean {
	const versionValue = project.version;
	return typeof versionValue === "number" && versionValue >= 6;
}