Skip to content

Commit 5f600e2

Browse files
authored
Qfix: calendar user migration (#9707)
Signed-off-by: Alexey Zinoviev <[email protected]>
1 parent 33b597d commit 5f600e2

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

models/calendar/src/migration.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ import {
4141
tryMigrate,
4242
tryUpgrade
4343
} 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'
4550
import setting, { DOMAIN_SETTING, type Integration } from '@hcengineering/setting'
4651
import { DOMAIN_CALENDAR, DOMAIN_EVENT } from '.'
4752
import calendar from './plugin'
@@ -312,6 +317,60 @@ async function fillUser (client: MigrationClient): Promise<void> {
312317
}
313318
}
314319

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+
315374
async function fillBlockTime (client: MigrationClient): Promise<void> {
316375
await client.update(DOMAIN_EVENT, { blockTime: { $exists: false }, allDay: true }, { blockTime: false })
317376
await client.update(DOMAIN_EVENT, { blockTime: { $exists: false } }, { blockTime: true })
@@ -391,6 +450,11 @@ export const calendarOperation: MigrateOperation = {
391450
state: 'fill-calendar-user-and-access',
392451
mode: 'upgrade',
393452
func: fillCalendarUserAndAccess
453+
},
454+
{
455+
state: 'migrate-ev-user-to-new-accounts',
456+
mode: 'upgrade',
457+
func: migrateEventUserToNewAccounts
394458
}
395459
])
396460
},

0 commit comments

Comments
 (0)