2021-12-07 18:45:11 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2021-12-15 09:04:43 +00:00
|
|
|
import { Class, Doc, Domain, DOMAIN_TX, Ref, TxCUD, TxOperations } from '@anticrm/core'
|
2021-12-16 09:06:56 +00:00
|
|
|
import {
|
|
|
|
MigrateOperation,
|
|
|
|
MigrateUpdate,
|
|
|
|
MigrationClient,
|
|
|
|
MigrationResult,
|
|
|
|
MigrationUpgradeClient
|
|
|
|
} from '@anticrm/model'
|
2021-12-07 18:45:11 +00:00
|
|
|
import core from '@anticrm/model-core'
|
|
|
|
import { createProjectKanban } from '@anticrm/task'
|
2021-12-15 09:04:43 +00:00
|
|
|
import { DOMAIN_TASK, DOMAIN_STATE, DOMAIN_KANBAN } from '.'
|
2021-12-07 18:45:11 +00:00
|
|
|
import task from './plugin'
|
|
|
|
|
2021-12-15 09:04:43 +00:00
|
|
|
function logInfo (msg: string, result: MigrationResult): void {
|
|
|
|
if (result.updated > 0) {
|
2021-12-16 09:06:56 +00:00
|
|
|
console.log(`Task: Migrate ${msg} ${result.updated}`)
|
2021-12-15 09:04:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-16 09:06:56 +00:00
|
|
|
async function migrateClass<T extends Doc> (
|
|
|
|
client: MigrationClient,
|
|
|
|
domain: Domain,
|
|
|
|
from: Ref<Class<Doc>>,
|
|
|
|
to: Ref<Class<T>>,
|
|
|
|
extraOps: MigrateUpdate<T> = {},
|
|
|
|
txExtraOps: MigrateUpdate<TxCUD<Doc>> = {}
|
|
|
|
): Promise<void> {
|
|
|
|
logInfo(`${from} => ${to}: `, await client.update<Doc>(domain, { _class: from }, { ...extraOps, _class: to }))
|
|
|
|
logInfo(
|
|
|
|
`${from} => ${to} Transactions`,
|
|
|
|
await client.update<TxCUD<Doc>>(DOMAIN_TX, { objectClass: from }, { ...txExtraOps, objectClass: to })
|
|
|
|
)
|
2021-12-15 09:04:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 18:45:11 +00:00
|
|
|
export const taskOperation: MigrateOperation = {
|
|
|
|
async migrate (client: MigrationClient): Promise<void> {
|
2021-12-15 09:04:43 +00:00
|
|
|
// Since we should not have Task class instances, we convert them all to Issue.
|
|
|
|
await migrateClass(client, DOMAIN_TASK, task.class.Task, task.class.Issue)
|
|
|
|
await migrateClass(client, DOMAIN_STATE, 'core:class:State' as Ref<Class<Doc>>, task.class.State)
|
2021-12-15 09:36:17 +00:00
|
|
|
await migrateClass(client, DOMAIN_STATE, 'core:class:WonState' as Ref<Class<Doc>>, task.class.WonState)
|
|
|
|
await migrateClass(client, DOMAIN_STATE, 'core:class:LostState' as Ref<Class<Doc>>, task.class.LostState)
|
2021-12-15 09:04:43 +00:00
|
|
|
await migrateClass(client, DOMAIN_KANBAN, 'view:class:Kanban' as Ref<Class<Doc>>, task.class.Kanban)
|
2021-12-16 09:06:56 +00:00
|
|
|
await migrateClass(
|
|
|
|
client,
|
|
|
|
DOMAIN_KANBAN,
|
|
|
|
'view:class:Sequence' as Ref<Class<Doc>>,
|
|
|
|
task.class.Sequence,
|
|
|
|
{ space: task.space.Sequence },
|
|
|
|
{ objectSpace: task.space.Sequence }
|
|
|
|
)
|
2021-12-15 09:04:43 +00:00
|
|
|
|
|
|
|
// Update attached to for task
|
2021-12-16 09:06:56 +00:00
|
|
|
await client.update(
|
|
|
|
DOMAIN_KANBAN,
|
|
|
|
{ _class: task.class.Sequence, attachedTo: task.class.Task },
|
|
|
|
{ attachedTo: task.class.Issue }
|
|
|
|
)
|
2021-12-15 09:04:43 +00:00
|
|
|
|
|
|
|
await migrateClass(client, DOMAIN_KANBAN, 'view:class:KanbanTemplate' as Ref<Class<Doc>>, task.class.KanbanTemplate)
|
|
|
|
await migrateClass(client, DOMAIN_KANBAN, 'view:class:StateTemplate' as Ref<Class<Doc>>, task.class.StateTemplate)
|
2021-12-16 09:06:56 +00:00
|
|
|
await migrateClass(
|
|
|
|
client,
|
|
|
|
DOMAIN_KANBAN,
|
|
|
|
'view:class:DoneStateTemplate' as Ref<Class<Doc>>,
|
|
|
|
task.class.DoneStateTemplate
|
|
|
|
)
|
|
|
|
await migrateClass(
|
|
|
|
client,
|
|
|
|
DOMAIN_KANBAN,
|
|
|
|
'view:class:LostStateTemplate' as Ref<Class<Doc>>,
|
|
|
|
task.class.LostStateTemplate
|
|
|
|
)
|
|
|
|
|
|
|
|
await client.move(
|
|
|
|
'recruit' as Domain,
|
|
|
|
{
|
|
|
|
_class: 'recruit:class:Applicant' as Ref<Class<Doc>>
|
|
|
|
},
|
|
|
|
DOMAIN_TASK
|
|
|
|
)
|
2021-12-07 18:45:11 +00:00
|
|
|
|
2021-12-16 09:06:56 +00:00
|
|
|
await client.move(
|
|
|
|
'lead' as Domain,
|
|
|
|
{
|
|
|
|
_class: 'lead:class:Lead' as Ref<Class<Doc>>
|
|
|
|
},
|
|
|
|
DOMAIN_TASK
|
|
|
|
)
|
2021-12-15 09:04:43 +00:00
|
|
|
|
2021-12-16 09:06:56 +00:00
|
|
|
// Update done states for tasks
|
|
|
|
await client.update(DOMAIN_TASK, { _class: task.class.Issue, doneState: { $exists: false } }, { doneState: null })
|
2021-12-07 18:45:11 +00:00
|
|
|
},
|
|
|
|
async upgrade (client: MigrationUpgradeClient): Promise<void> {
|
|
|
|
console.log('Task: Performing model upgrades')
|
|
|
|
|
|
|
|
const ops = new TxOperations(client, core.account.System)
|
2021-12-16 09:06:56 +00:00
|
|
|
if ((await client.findOne(task.class.Sequence, { attachedTo: task.class.Issue })) === undefined) {
|
|
|
|
console.info('Task: Create sequence for default task project.')
|
2021-12-07 18:45:11 +00:00
|
|
|
// We need to create sequence
|
2021-12-15 09:04:43 +00:00
|
|
|
await ops.createDoc(task.class.Sequence, task.space.Sequence, {
|
|
|
|
attachedTo: task.class.Issue,
|
2021-12-07 18:45:11 +00:00
|
|
|
sequence: 0
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log('Task: => sequence is ok')
|
|
|
|
}
|
2021-12-16 09:06:56 +00:00
|
|
|
if ((await client.findOne(task.class.Kanban, { attachedTo: task.space.TasksPublic })) === undefined) {
|
|
|
|
console.info('Task: Create kanban for default task project.')
|
2021-12-07 18:45:11 +00:00
|
|
|
await createProjectKanban(task.space.TasksPublic, async (_class, space, data, id) => {
|
|
|
|
const doc = await ops.findOne<Doc>(_class, { _id: id })
|
|
|
|
if (doc === undefined) {
|
|
|
|
await ops.createDoc(_class, space, data, id)
|
|
|
|
} else {
|
|
|
|
await ops.updateDoc(_class, space, id, data)
|
|
|
|
}
|
|
|
|
}).catch((err) => console.error(err))
|
|
|
|
} else {
|
|
|
|
console.log('Task: => public project Kanban is ok')
|
|
|
|
}
|
2021-12-14 13:33:30 +00:00
|
|
|
|
2021-12-15 09:04:43 +00:00
|
|
|
console.log('View: Performing model upgrades')
|
|
|
|
|
2021-12-16 09:06:56 +00:00
|
|
|
const kanbans = (await client.findAll(task.class.Kanban, {})).filter((kanban) => kanban.doneStates == null)
|
2021-12-15 09:04:43 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
2021-12-16 09:06:56 +00:00
|
|
|
kanbans.map(async (kanban) => {
|
|
|
|
console.log(`Updating kanban: ${kanban._id}`)
|
|
|
|
try {
|
|
|
|
const doneStates = await Promise.all([
|
|
|
|
ops.createDoc(task.class.WonState, kanban.space, {
|
|
|
|
title: 'Won'
|
|
|
|
}),
|
|
|
|
ops.createDoc(task.class.LostState, kanban.space, {
|
|
|
|
title: 'Lost'
|
2021-12-15 09:04:43 +00:00
|
|
|
})
|
2021-12-16 09:06:56 +00:00
|
|
|
])
|
2021-12-15 09:04:43 +00:00
|
|
|
|
2021-12-16 09:06:56 +00:00
|
|
|
await ops.updateDoc(kanban._class, kanban.space, kanban._id, {
|
|
|
|
doneStates
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2021-12-14 13:33:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2021-12-07 18:45:11 +00:00
|
|
|
}
|
|
|
|
}
|