Files
huly-platform/models/lead/src/migration.ts
T
2023-12-29 14:05:27 +07:00

170 lines
5.3 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 { TxOperations } from '@hcengineering/core'
import { leadId } from '@hcengineering/lead'
import {
tryMigrate,
tryUpgrade,
type MigrateOperation,
type MigrationClient,
type MigrationUpgradeClient
} from '@hcengineering/model'
import core, { DOMAIN_SPACE } from '@hcengineering/model-core'
import task, { createProjectType, createSequence, fixTaskTypes } from '@hcengineering/model-task'
import { PaletteColorIndexes } from '@hcengineering/ui/src/colors'
import lead from './plugin'
async function createSpace (tx: TxOperations): Promise<void> {
const current = await tx.findOne(core.class.Space, {
_id: lead.space.DefaultFunnel
})
if (current === undefined) {
const type = await createProjectType(
tx,
{
name: 'Default funnel',
descriptor: lead.descriptors.FunnelType,
description: '',
tasks: [],
classic: true
},
[
{
_id: lead.taskType.Lead,
name: 'Lead',
descriptor: lead.descriptors.Lead,
ofClass: lead.class.Lead,
targetClass: lead.class.Lead,
statusClass: core.class.Status,
statusCategories: [
task.statusCategory.UnStarted,
task.statusCategory.Active,
task.statusCategory.Won,
task.statusCategory.Lost
],
kind: 'task',
factory: [
{
color: PaletteColorIndexes.Coin,
name: 'Backlog',
ofAttribute: lead.attribute.State,
category: task.statusCategory.UnStarted
},
{
color: PaletteColorIndexes.Coin,
name: 'Incoming',
ofAttribute: lead.attribute.State,
category: task.statusCategory.Active
},
{
color: PaletteColorIndexes.Arctic,
name: 'Negotation',
ofAttribute: lead.attribute.State,
category: task.statusCategory.Active
},
{
color: PaletteColorIndexes.Watermelon,
name: 'Offer preparing',
ofAttribute: lead.attribute.State,
category: task.statusCategory.Active
},
{
color: PaletteColorIndexes.Orange,
name: 'Make a decision',
ofAttribute: lead.attribute.State,
category: task.statusCategory.Active
},
{
color: PaletteColorIndexes.Ocean,
name: 'Contract conclusion',
ofAttribute: lead.attribute.State,
category: task.statusCategory.Active
},
{ name: 'Won', ofAttribute: lead.attribute.State, category: task.statusCategory.Won },
{ name: 'Lost', ofAttribute: lead.attribute.State, category: task.statusCategory.Lost }
]
}
],
lead.template.DefaultFunnel
)
await tx.createDoc(
lead.class.Funnel,
core.space.Space,
{
name: 'Funnel',
description: 'Default funnel',
private: false,
archived: false,
members: [],
type
},
lead.space.DefaultFunnel
)
}
}
async function createDefaults (tx: TxOperations): Promise<void> {
await createSpace(tx)
await createSequence(tx, lead.class.Lead)
}
export const leadOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {
await tryMigrate(client, leadId, [
{
state: 'fix-category-descriptors',
func: async (client) => {
await client.update(
DOMAIN_SPACE,
{ _class: task.class.ProjectType, category: 'lead:category:FunnelTypeCategory' },
{
$set: { descriptor: lead.descriptors.FunnelType },
$unset: { category: 1 }
}
)
}
},
{
state: 'fixTaskStatus',
func: async (client): Promise<void> => {
await fixTaskTypes(client, lead.descriptors.FunnelType, async () => [
{
name: 'Lead',
descriptor: lead.descriptors.Lead,
ofClass: lead.class.Lead,
targetClass: lead.class.Lead,
statusCategories: [
task.statusCategory.UnStarted,
task.statusCategory.Active,
task.statusCategory.Won,
task.statusCategory.Lost
],
statusClass: core.class.Status,
kind: 'task'
}
])
}
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
const ops = new TxOperations(client, core.account.System)
await createDefaults(ops)
await tryUpgrade(client, leadId, [])
}
}