1- import { Account , Group , type co , type ID , deleteCoValues } from "jazz-tools"
2- import { NotificationSettings } from "#shared/schema/user"
3- import { ServerAccount } from "#shared/schema/server"
4-
5- export {
6- migrateNotificationSettings ,
7- addServerToGroup ,
8- copyNotificationSettingsData ,
9- }
10- export type { MigrationContext , NotificationSettingsInput }
1+ export { copyNotificationSettingsData }
2+ export type { NotificationSettingsInput }
113
124type NotificationSettingsInput = {
135 version : 1
@@ -24,11 +16,6 @@ type NotificationSettingsInput = {
2416 } >
2517}
2618
27- type MigrationContext = {
28- loadAs : Account
29- rootLanguage ?: "de" | "en"
30- }
31-
3219type NotificationSettingsLike = {
3320 timezone ?: string
3421 notificationTime ?: string
@@ -65,68 +52,3 @@ function copyNotificationSettingsData(
6552 } ) ) ,
6653 }
6754}
68-
69- async function migrateNotificationSettings (
70- oldSettings : co . loaded < typeof NotificationSettings > ,
71- serverAccountId : string ,
72- context : MigrationContext ,
73- ) : Promise < {
74- newSettings : co . loaded < typeof NotificationSettings >
75- cleanup : ( ) => Promise < void >
76- } > {
77- let group = Group . create ( )
78-
79- await addServerToGroup ( group , serverAccountId , context )
80-
81- let settingsData = copyNotificationSettingsData (
82- oldSettings ,
83- context . rootLanguage ,
84- )
85-
86- let newSettings = NotificationSettings . create ( settingsData , { owner : group } )
87-
88- let cleanup = async ( ) => {
89- let owner = oldSettings . $jazz . owner
90- if ( owner instanceof Group ) {
91- let hasAdminPermission = owner . members . some (
92- m =>
93- m . account ?. $jazz . id === context . loadAs . $jazz . id && m . role === "admin" ,
94- )
95- if ( ! hasAdminPermission ) {
96- console . error (
97- "[NotificationSettingsMigration] Caller lacks admin permission on owning group" ,
98- )
99- throw new Error ( "Caller lacks admin permission on owning group" )
100- }
101- }
102-
103- try {
104- await deleteCoValues ( NotificationSettings , oldSettings . $jazz . id )
105- } catch ( error ) {
106- console . error (
107- "[NotificationSettingsMigration] Failed to delete old settings:" ,
108- error ,
109- )
110- throw error
111- }
112- }
113-
114- return { newSettings, cleanup }
115- }
116-
117- async function addServerToGroup (
118- group : Group ,
119- serverAccountId : string ,
120- context : MigrationContext ,
121- ) : Promise < void > {
122- let serverAccount = await ServerAccount . load (
123- serverAccountId as ID < typeof ServerAccount > ,
124- { loadAs : context . loadAs } ,
125- )
126-
127- if ( ! serverAccount || ! serverAccount . $isLoaded ) {
128- throw new Error ( "Failed to load server account" )
129- }
130-
131- group . addMember ( serverAccount , "writer" )
132- }
0 commit comments