From 5f600e2ff92d5e1a88c08aa9cac0fbaef82b2ca2 Mon Sep 17 00:00:00 2001 From: Alexey Zinoviev Date: Wed, 20 Aug 2025 23:56:14 +0700 Subject: [PATCH] Qfix: calendar user migration (#9707) Signed-off-by: Alexey Zinoviev --- models/calendar/src/migration.ts | 66 +++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/models/calendar/src/migration.ts b/models/calendar/src/migration.ts index 8165459abd..14e53395a9 100644 --- a/models/calendar/src/migration.ts +++ b/models/calendar/src/migration.ts @@ -41,7 +41,12 @@ import { tryMigrate, tryUpgrade } from '@hcengineering/model' -import { DOMAIN_SPACE, getAccountUuidBySocialKey, getSocialKeyByOldAccount } from '@hcengineering/model-core' +import { + DOMAIN_SPACE, + getAccountUuidBySocialKey, + getSocialIdFromOldAccount, + getSocialKeyByOldAccount +} from '@hcengineering/model-core' import setting, { DOMAIN_SETTING, type Integration } from '@hcengineering/setting' import { DOMAIN_CALENDAR, DOMAIN_EVENT } from '.' import calendar from './plugin' @@ -312,6 +317,60 @@ async function fillUser (client: MigrationClient): Promise { } } +async function migrateEventUserToNewAccounts (client: MigrationClient): Promise { + const socialKeyByAccount = await getSocialKeyByOldAccount(client) + const socialIdBySocialKey = new Map() + const socialIdByOldAccount = new Map() + + client.logger.log('processing events user ', {}) + const iterator = await client.traverse(DOMAIN_EVENT, { + _class: { $in: [calendar.class.Event, calendar.class.ReccuringEvent] } + }) + + try { + let processed = 0 + while (true) { + const docs = await iterator.next(200) + if (docs === null || docs.length === 0) { + break + } + + const operations: { filter: MigrationDocumentQuery, update: MigrateUpdate }[] = [] + + for (const doc of docs) { + const event = doc as Event + const socialId = await getSocialIdFromOldAccount( + client, + event.user, + socialKeyByAccount, + socialIdBySocialKey, + socialIdByOldAccount + ) + const newUser = socialId ?? event.user + + if (newUser === event.user) continue + + operations.push({ + filter: { _id: doc._id }, + update: { + user: newUser + } + }) + } + + if (operations.length > 0) { + await client.bulk(DOMAIN_EVENT, operations) + } + + processed += docs.length + client.logger.log('...processed', { count: processed }) + } + } finally { + await iterator.close() + } + client.logger.log('finished processing events user ', {}) +} + async function fillBlockTime (client: MigrationClient): Promise { await client.update(DOMAIN_EVENT, { blockTime: { $exists: false }, allDay: true }, { blockTime: false }) await client.update(DOMAIN_EVENT, { blockTime: { $exists: false } }, { blockTime: true }) @@ -391,6 +450,11 @@ export const calendarOperation: MigrateOperation = { state: 'fill-calendar-user-and-access', mode: 'upgrade', func: fillCalendarUserAndAccess + }, + { + state: 'migrate-ev-user-to-new-accounts', + mode: 'upgrade', + func: migrateEventUserToNewAccounts } ]) },