2021-12-09 09:08:15 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 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.
|
|
|
|
//
|
|
|
|
|
2021-12-22 09:25:16 +00:00
|
|
|
import { DOMAIN_TX, TxCreateDoc, TxOperations } from '@anticrm/core'
|
2021-12-16 09:06:56 +00:00
|
|
|
import { MigrateOperation, MigrationClient, MigrationResult, MigrationUpgradeClient } from '@anticrm/model'
|
2021-12-22 09:25:16 +00:00
|
|
|
import core from '@anticrm/model-core'
|
|
|
|
import { DOMAIN_TASK } from '@anticrm/model-task'
|
|
|
|
import { Lead } from '@anticrm/lead'
|
|
|
|
import lead from './plugin'
|
|
|
|
import contact from '@anticrm/model-contact'
|
2021-12-09 09:08:15 +00:00
|
|
|
|
2021-12-22 09:04:07 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2021-12-16 09:06:56 +00:00
|
|
|
function logInfo (msg: string, result: MigrationResult): void {
|
|
|
|
if (result.updated > 0) {
|
|
|
|
console.log(`Lead: Migrate ${msg} ${result.updated}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 09:08:15 +00:00
|
|
|
export const leadOperation: MigrateOperation = {
|
2021-12-22 09:25:16 +00:00
|
|
|
async migrate (client: MigrationClient): Promise<void> {
|
|
|
|
// Update done states for tasks
|
|
|
|
logInfo('lead done states', await client.update(DOMAIN_TASK, { _class: lead.class.Lead, doneState: { $exists: false } }, { doneState: null }))
|
|
|
|
const txes = await client.find<TxCreateDoc<Lead>>(DOMAIN_TX, {
|
|
|
|
_class: core.class.TxCreateDoc,
|
|
|
|
objectClass: lead.class.Lead
|
|
|
|
})
|
|
|
|
for (const tx of txes) {
|
|
|
|
if (tx.attributes.attachedTo !== undefined) continue
|
|
|
|
await client.update<TxCreateDoc<Lead>>(DOMAIN_TX, { _id: tx._id }, {
|
|
|
|
attributes: {
|
|
|
|
...tx.attributes,
|
2021-12-30 09:04:32 +00:00
|
|
|
attachedTo: (tx.attributes as any).customer,
|
2021-12-22 09:25:16 +00:00
|
|
|
attachedToClass: contact.class.Contact
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async upgrade (client: MigrationUpgradeClient): Promise<void> {
|
|
|
|
console.log('Lead: Performing model upgrades')
|
|
|
|
|
|
|
|
const ops = new TxOperations(client, core.account.System)
|
|
|
|
|
|
|
|
const leads = await client.findAll(lead.class.Lead, {})
|
|
|
|
for (const lead of leads) {
|
|
|
|
if (lead.attachedTo === undefined) {
|
|
|
|
await ops.updateDoc(lead._class, lead.space, lead._id, {
|
2021-12-30 09:04:32 +00:00
|
|
|
attachedTo: (lead as any).customer,
|
2021-12-22 09:25:16 +00:00
|
|
|
attachedToClass: contact.class.Contact
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 09:08:15 +00:00
|
|
|
}
|