From 70dc3f4387ab143fe2553a03878e9c3d148a474b Mon Sep 17 00:00:00 2001 From: Alexey Zinoviev Date: Tue, 6 May 2025 06:12:41 +0400 Subject: [PATCH] uberf-10255: migrate accounts in saved filters (#8846) --- .vscode/launch.json | 4 +- models/view/src/migration.ts | 84 +++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 3fd00f7070..d0ac923e28 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -459,14 +459,14 @@ "MINIO_ENDPOINT": "localhost:9000", "TRANSACTOR_URL": "ws://localhost:3332", "ACCOUNTS_URL": "http://localhost:3000", - "ACCOUNT_DB_URL": "mongodb://localhost:27017", + "ACCOUNT_DB_URL": "postgresql://root@huly.local:26257/defaultdb?sslmode=disable", // "ACCOUNT_DB_URL": "postgresql://postgres:example@localhost:5433", // "DB_URL": "postgresql://postgres:example@localhost:5433", "DB_URL": "postgresql://root@huly.local:26257/defaultdb?sslmode=disable", "MONGO_URL": "mongodb://localhost:27017", "TELEGRAM_DATABASE": "telegram-service", "REKONI_URL": "http://localhost:4004", - "MODEL_VERSION": "0.7.1" + "MODEL_VERSION": "0.7.75" }, "runtimeArgs": ["--nolazy", "-r", "ts-node/register"], "sourceMaps": true, diff --git a/models/view/src/migration.ts b/models/view/src/migration.ts index 228d1b5a73..fc6046fb90 100644 --- a/models/view/src/migration.ts +++ b/models/view/src/migration.ts @@ -23,8 +23,8 @@ import { } from '@hcengineering/model' import { DOMAIN_PREFERENCE } from '@hcengineering/preference' import view, { type Filter, type FilteredView, type ViewletPreference, viewId } from '@hcengineering/view' -import { getSocialKeyByOldAccount, getUniqueAccounts } from '@hcengineering/model-core' -import { type AccountUuid, MeasureMetricsContext } from '@hcengineering/core' +import { getSocialIdFromOldAccount, getSocialKeyByOldAccount, getUniqueAccounts } from '@hcengineering/model-core' +import core, { type AccountUuid, MeasureMetricsContext, type PersonId } from '@hcengineering/core' import { DOMAIN_VIEW } from '.' @@ -171,6 +171,81 @@ async function migrateSocialIdsToGlobalAccounts (client: MigrationClient): Promi ctx.info('finished processing view filtered view users ', {}) } +async function migrateAccsInSavedFilters (client: MigrationClient): Promise { + const ctx = new MeasureMetricsContext('view migrateAccsInSavedFilters', {}) + const hierarchy = client.hierarchy + const socialKeyByAccount = await getSocialKeyByOldAccount(client) + const socialIdBySocialKey = new Map() + const socialIdByOldAccount = new Map() + + ctx.info('processing view filtered view accounts in filters ', {}) + const affectedViews = await client.find(DOMAIN_VIEW, { + _class: view.class.FilteredView, + filters: { $regex: '%core:class:Account%' } + }) + for (const view of affectedViews) { + const filters = JSON.parse(view.filters) + const newFilters = [] + let needUpdate = false + for (const filter of filters) { + const key = filter?.key + if (key == null) { + newFilters.push(filter) + continue + } + + const type = key.attribute?.type + const objClass = key._class + const objKey = key.key + + if (type == null || objClass == null || objKey == null) { + newFilters.push(filter) + continue + } + + if (type._class !== 'core:class:RefTo' || type.to !== 'core:class:Account') { + newFilters.push(filter) + continue + } + + const newAttrType = hierarchy.getAttribute(objClass, objKey) + + if (newAttrType.type._class !== core.class.TypePersonId) { + newFilters.push(filter) + continue + } + + const newFilter = { ...filter } + newFilter.key.attribute.type = { + _class: newAttrType.type._class, + label: newAttrType.type.label + } + const oldValue = newFilter.value + newFilter.value = [] + for (const accId of oldValue) { + const socialId = await getSocialIdFromOldAccount( + client, + accId, + socialKeyByAccount, + socialIdBySocialKey, + socialIdByOldAccount + ) + + newFilter.value.push(socialId ?? accId) + } + + newFilters.push(newFilter) + needUpdate = true + } + + if (needUpdate) { + await client.update(DOMAIN_VIEW, { _id: view._id }, { filters: JSON.stringify(newFilters) }) + } + } + + ctx.info('finished processing view filtered view accounts in filters ', {}) +} + export const viewOperation: MigrateOperation = { async migrate (client: MigrationClient, mode): Promise { await tryMigrate(mode, client, viewId, [ @@ -193,6 +268,11 @@ export const viewOperation: MigrateOperation = { state: 'social-ids-to-global-accounts', mode: 'upgrade', func: migrateSocialIdsToGlobalAccounts + }, + { + state: 'accs-in-saved-filters', + mode: 'upgrade', + func: migrateAccsInSavedFilters } ]) },