mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-22 09:35:00 +02:00
145 lines
3.9 KiB
TypeScript
145 lines
3.9 KiB
TypeScript
//
|
|
|
|
import { Contact } from '@hcengineering/contact'
|
|
import { DOMAIN_TX, TxCreateDoc, TxOperations } from '@hcengineering/core'
|
|
import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model'
|
|
import core from '@hcengineering/model-core'
|
|
import contact, { DOMAIN_CONTACT } from './index'
|
|
|
|
async function createSpace (tx: TxOperations): Promise<void> {
|
|
const current = await tx.findOne(core.class.Space, {
|
|
_id: contact.space.Employee
|
|
})
|
|
if (current === undefined) {
|
|
await tx.createDoc(
|
|
core.class.Space,
|
|
core.space.Space,
|
|
{
|
|
name: 'Employees',
|
|
description: 'Employees',
|
|
private: false,
|
|
archived: false,
|
|
members: []
|
|
},
|
|
contact.space.Employee
|
|
)
|
|
}
|
|
const contacts = await tx.findOne(core.class.Space, {
|
|
_id: contact.space.Contacts
|
|
})
|
|
if (contacts === undefined) {
|
|
await tx.createDoc(
|
|
core.class.Space,
|
|
core.space.Space,
|
|
{
|
|
name: 'Contacts',
|
|
description: 'Contacts',
|
|
private: false,
|
|
archived: false,
|
|
members: []
|
|
},
|
|
contact.space.Contacts
|
|
)
|
|
}
|
|
}
|
|
|
|
let totalCreateOn = 0
|
|
async function setCreate (client: MigrationClient): Promise<void> {
|
|
while (true) {
|
|
const docs = await client.find<Contact>(
|
|
DOMAIN_CONTACT,
|
|
{
|
|
_class: {
|
|
$in: [contact.class.Contact, contact.class.Organization, contact.class.Person, contact.class.Employee]
|
|
},
|
|
createOn: { $exists: false }
|
|
},
|
|
{ limit: 500 }
|
|
)
|
|
if (docs.length === 0) {
|
|
break
|
|
}
|
|
totalCreateOn += docs.length
|
|
console.log('processing createOn migration', totalCreateOn)
|
|
const creates = await client.find<TxCreateDoc<Contact>>(DOMAIN_TX, {
|
|
objectId: { $in: docs.map((it) => it._id) },
|
|
_class: core.class.TxCreateDoc
|
|
})
|
|
for (const doc of docs) {
|
|
const tx = creates.find((it) => it.objectId === doc._id)
|
|
if (tx !== undefined) {
|
|
await client.update(
|
|
DOMAIN_CONTACT,
|
|
{
|
|
_id: doc._id
|
|
},
|
|
{
|
|
createOn: tx.modifiedOn
|
|
}
|
|
)
|
|
await client.update(
|
|
DOMAIN_TX,
|
|
{
|
|
_id: tx._id
|
|
},
|
|
{
|
|
'attributes.createOn': tx.modifiedOn
|
|
}
|
|
)
|
|
} else {
|
|
await client.update(
|
|
DOMAIN_CONTACT,
|
|
{
|
|
_id: doc._id
|
|
},
|
|
{
|
|
createOn: doc.modifiedOn
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function createEmployeeEmail (client: TxOperations): Promise<void> {
|
|
const employees = await client.findAll(contact.class.Employee, {})
|
|
const channels = await client.findAll(contact.class.Channel, {
|
|
provider: contact.channelProvider.Email,
|
|
attachedTo: { $in: employees.map((p) => p._id) }
|
|
})
|
|
const channelsMap = new Map(channels.map((p) => [p.attachedTo, p]))
|
|
for (const employee of employees) {
|
|
const acc = await client.findOne(contact.class.EmployeeAccount, { employee: employee._id })
|
|
if (acc === undefined) continue
|
|
const current = channelsMap.get(employee._id)
|
|
if (current === undefined) {
|
|
await client.addCollection(
|
|
contact.class.Channel,
|
|
contact.space.Contacts,
|
|
employee._id,
|
|
contact.class.Employee,
|
|
'channels',
|
|
{
|
|
provider: contact.channelProvider.Email,
|
|
value: acc.email.trim()
|
|
},
|
|
undefined,
|
|
employee.modifiedOn
|
|
)
|
|
} else if (current.value !== acc.email.trim()) {
|
|
await client.update(current, { value: acc.email.trim() }, false, current.modifiedOn)
|
|
}
|
|
}
|
|
}
|
|
|
|
export const contactOperation: MigrateOperation = {
|
|
async migrate (client: MigrationClient): Promise<void> {
|
|
await setCreate(client)
|
|
},
|
|
async upgrade (client: MigrationUpgradeClient): Promise<void> {
|
|
const tx = new TxOperations(client, core.account.System)
|
|
await createSpace(tx)
|
|
await createEmployeeEmail(tx)
|
|
}
|
|
}
|