@@ -41,7 +41,12 @@ import {
41
41
tryMigrate ,
42
42
tryUpgrade
43
43
} from '@hcengineering/model'
44
- import { DOMAIN_SPACE , getAccountUuidBySocialKey , getSocialKeyByOldAccount } from '@hcengineering/model-core'
44
+ import {
45
+ DOMAIN_SPACE ,
46
+ getAccountUuidBySocialKey ,
47
+ getSocialIdFromOldAccount ,
48
+ getSocialKeyByOldAccount
49
+ } from '@hcengineering/model-core'
45
50
import setting , { DOMAIN_SETTING , type Integration } from '@hcengineering/setting'
46
51
import { DOMAIN_CALENDAR , DOMAIN_EVENT } from '.'
47
52
import calendar from './plugin'
@@ -312,6 +317,60 @@ async function fillUser (client: MigrationClient): Promise<void> {
312
317
}
313
318
}
314
319
320
+ async function migrateEventUserToNewAccounts ( client : MigrationClient ) : Promise < void > {
321
+ const socialKeyByAccount = await getSocialKeyByOldAccount ( client )
322
+ const socialIdBySocialKey = new Map < string , PersonId | null > ( )
323
+ const socialIdByOldAccount = new Map < string , PersonId | null > ( )
324
+
325
+ client . logger . log ( 'processing events user ' , { } )
326
+ const iterator = await client . traverse ( DOMAIN_EVENT , {
327
+ _class : { $in : [ calendar . class . Event , calendar . class . ReccuringEvent ] }
328
+ } )
329
+
330
+ try {
331
+ let processed = 0
332
+ while ( true ) {
333
+ const docs = await iterator . next ( 200 )
334
+ if ( docs === null || docs . length === 0 ) {
335
+ break
336
+ }
337
+
338
+ const operations : { filter : MigrationDocumentQuery < Doc > , update : MigrateUpdate < Doc > } [ ] = [ ]
339
+
340
+ for ( const doc of docs ) {
341
+ const event = doc as Event
342
+ const socialId = await getSocialIdFromOldAccount (
343
+ client ,
344
+ event . user ,
345
+ socialKeyByAccount ,
346
+ socialIdBySocialKey ,
347
+ socialIdByOldAccount
348
+ )
349
+ const newUser = socialId ?? event . user
350
+
351
+ if ( newUser === event . user ) continue
352
+
353
+ operations . push ( {
354
+ filter : { _id : doc . _id } ,
355
+ update : {
356
+ user : newUser
357
+ }
358
+ } )
359
+ }
360
+
361
+ if ( operations . length > 0 ) {
362
+ await client . bulk ( DOMAIN_EVENT , operations )
363
+ }
364
+
365
+ processed += docs . length
366
+ client . logger . log ( '...processed' , { count : processed } )
367
+ }
368
+ } finally {
369
+ await iterator . close ( )
370
+ }
371
+ client . logger . log ( 'finished processing events user ' , { } )
372
+ }
373
+
315
374
async function fillBlockTime ( client : MigrationClient ) : Promise < void > {
316
375
await client . update ( DOMAIN_EVENT , { blockTime : { $exists : false } , allDay : true } , { blockTime : false } )
317
376
await client . update ( DOMAIN_EVENT , { blockTime : { $exists : false } } , { blockTime : true } )
@@ -391,6 +450,11 @@ export const calendarOperation: MigrateOperation = {
391
450
state : 'fill-calendar-user-and-access' ,
392
451
mode : 'upgrade' ,
393
452
func : fillCalendarUserAndAccess
453
+ } ,
454
+ {
455
+ state : 'migrate-ev-user-to-new-accounts' ,
456
+ mode : 'upgrade' ,
457
+ func : migrateEventUserToNewAccounts
394
458
}
395
459
] )
396
460
} ,
0 commit comments