platform/models/notification/src/migration.ts
Andrey Sobolev 3bec396384
UBERF-7692: Move FindAll slow print into mongo adapter (#6152)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2024-07-28 14:55:42 +07:00

178 lines
5.6 KiB
TypeScript

//
// Copyright © 2022 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import core, { DOMAIN_TX, type Class, type Doc, type DocumentQuery, type Ref, type Space } from '@hcengineering/core'
import {
migrateSpace,
tryMigrate,
type MigrateOperation,
type MigrationClient,
type MigrationUpgradeClient
} from '@hcengineering/model'
import notification, {
notificationId,
NotificationStatus,
type BrowserNotification,
type DocNotifyContext
} from '@hcengineering/notification'
import { DOMAIN_PREFERENCE } from '@hcengineering/preference'
import { DOMAIN_DOC_NOTIFY, DOMAIN_NOTIFICATION, DOMAIN_USER_NOTIFY } from './index'
export async function removeNotifications (
client: MigrationClient,
query: DocumentQuery<DocNotifyContext>
): Promise<void> {
while (true) {
const contexts = await client.find<DocNotifyContext>(
DOMAIN_NOTIFICATION,
{
_class: notification.class.DocNotifyContext,
...query
},
{ limit: 500 }
)
if (contexts.length === 0) {
return
}
const ids = contexts.map(({ _id }) => _id)
await client.deleteMany(DOMAIN_NOTIFICATION, {
_class: notification.class.CommonInboxNotification,
docNotifyContext: { $in: ids }
})
await client.deleteMany(DOMAIN_NOTIFICATION, {
_class: notification.class.ActivityInboxNotification,
docNotifyContext: { $in: ids }
})
await client.deleteMany(DOMAIN_NOTIFICATION, {
_class: notification.class.MentionInboxNotification,
docNotifyContext: { $in: ids }
})
await client.deleteMany(DOMAIN_NOTIFICATION, {
_class: notification.class.DocNotifyContext,
_id: { $in: ids }
})
}
}
export async function migrateSettings (client: MigrationClient): Promise<void> {
await client.update(
DOMAIN_PREFERENCE,
{
_class: 'notification:class:NotificationSetting' as Ref<Class<Doc>>,
attachedTo: 'notification:providers:BrowserNotification' as Ref<Doc>
},
{
_class: notification.class.NotificationTypeSetting,
attachedTo: notification.providers.PushNotificationProvider
}
)
await client.update(
DOMAIN_PREFERENCE,
{
_class: 'notification:class:NotificationSetting' as Ref<Class<Doc>>,
attachedTo: 'notification:providers:PlatformNotification' as Ref<Doc>
},
{
_class: notification.class.NotificationTypeSetting,
attachedTo: notification.providers.InboxNotificationProvider
}
)
}
export const notificationOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {
await tryMigrate(client, notificationId, [
{
state: 'delete-hidden-notifications',
func: async (client) => {
await removeNotifications(client, { hidden: true })
}
},
{
state: 'delete-invalid-notifications',
func: async (client) => {
await removeNotifications(client, { attachedToClass: 'chunter:class:Comment' as Ref<Class<Doc>> })
}
},
{
state: 'remove-old-classes',
func: async (client) => {
await client.deleteMany(DOMAIN_NOTIFICATION, { _class: 'notification:class:DocUpdates' as Ref<Class<Doc>> })
await client.deleteMany(DOMAIN_TX, { objectClass: 'notification:class:DocUpdates' as Ref<Class<Doc>> })
}
},
{
state: 'removeDeprecatedSpace',
func: async (client: MigrationClient) => {
await migrateSpace(client, 'notification:space:Notifications' as Ref<Space>, core.space.Workspace, [
DOMAIN_NOTIFICATION
])
}
},
{
state: 'migrate-setting',
func: migrateSettings
},
{
state: 'move-doc-notify',
func: async (client) => {
await client.move(DOMAIN_NOTIFICATION, { _class: notification.class.DocNotifyContext }, DOMAIN_DOC_NOTIFY)
}
},
{
state: 'remove-last-view',
func: async (client) => {
await client.deleteMany(DOMAIN_NOTIFICATION, { _class: 'notification:class:LastView' as any })
}
},
{
state: 'remove-notification',
func: async (client) => {
await client.deleteMany(DOMAIN_NOTIFICATION, { _class: 'notification:class:Notification' as any })
}
},
{
state: 'remove-email-notification',
func: async (client) => {
await client.deleteMany(DOMAIN_NOTIFICATION, { _class: 'notification:class:EmailNotification' as any })
}
},
{
state: 'move-user',
func: async (client) => {
await client.move(
DOMAIN_NOTIFICATION,
{ _class: { $in: [notification.class.BrowserNotification, notification.class.PushSubscription] } },
DOMAIN_USER_NOTIFY
)
}
}
])
await client.deleteMany<BrowserNotification>(DOMAIN_USER_NOTIFY, {
_class: notification.class.BrowserNotification,
status: NotificationStatus.Notified
})
},
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}