Signed-off-by: Ilya Sumbatyants <ilya.sumb@gmail.com>
This commit is contained in:
Ilya Sumbatyants
2021-12-17 10:08:37 +01:00
committed by GitHub
parent 34b9495631
commit b0ffc0ed46
44 changed files with 482 additions and 705 deletions
+15 -9
View File
@@ -53,18 +53,22 @@ export { default } from './plugin'
export const DOMAIN_TASK = 'task' as Domain
export const DOMAIN_STATE = 'state' as Domain
export const DOMAIN_KANBAN = 'kanban' as Domain
@Model(task.class.State, core.class.Doc, DOMAIN_STATE)
@Model(task.class.State, core.class.Doc, DOMAIN_STATE, [core.interface.DocWithRank])
export class TState extends TDoc implements State {
@Prop(TypeString(), 'Title' as IntlString)
title!: string
color!: string
declare rank: string
}
@Model(task.class.DoneState, core.class.Doc, DOMAIN_STATE)
@Model(task.class.DoneState, core.class.Doc, DOMAIN_STATE, [core.interface.DocWithRank])
export class TDoneState extends TDoc implements DoneState {
@Prop(TypeString(), 'Title' as IntlString)
title!: string
declare rank: string
}
@Model(task.class.WonState, task.class.DoneState, DOMAIN_STATE)
@@ -78,7 +82,7 @@ export class TLostState extends TDoneState implements LostState {}
*
* No domain is specified, since pure Tasks could not exists
*/
@Model(task.class.Task, core.class.AttachedDoc, DOMAIN_TASK)
@Model(task.class.Task, core.class.AttachedDoc, DOMAIN_TASK, [core.interface.DocWithRank])
export class TTask extends TAttachedDoc implements Task {
@Prop(TypeRef(task.class.State), 'State' as IntlString)
state!: Ref<State>
@@ -91,6 +95,8 @@ export class TTask extends TAttachedDoc implements Task {
// @Prop(TypeRef(contact.class.Employee), 'Assignee' as IntlString)
assignee!: Ref<Employee> | null
declare rank: string
}
@Model(task.class.SpaceWithStates, core.class.Space)
@@ -136,7 +142,6 @@ export class TKanban extends TDoc implements Kanban {
states!: Arr<Ref<State>>
doneStates!: Arr<Ref<DoneState>>
attachedTo!: Ref<Space>
order!: Arr<Ref<Doc>>
}
@Model(task.class.KanbanTemplateSpace, core.class.Space, DOMAIN_MODEL)
@@ -144,19 +149,23 @@ export class TKanbanTemplateSpace extends TSpace implements KanbanTemplateSpace
icon!: AnyComponent
}
@Model(task.class.StateTemplate, core.class.AttachedDoc, DOMAIN_KANBAN)
@Model(task.class.StateTemplate, core.class.AttachedDoc, DOMAIN_KANBAN, [core.interface.DocWithRank])
export class TStateTemplate extends TAttachedDoc implements StateTemplate {
@Prop(TypeString(), 'Title' as IntlString)
title!: string
@Prop(TypeString(), 'Color' as IntlString)
color!: string
declare rank: string
}
@Model(task.class.DoneStateTemplate, core.class.AttachedDoc, DOMAIN_KANBAN)
@Model(task.class.DoneStateTemplate, core.class.AttachedDoc, DOMAIN_KANBAN, [core.interface.DocWithRank])
export class TDoneStateTemplate extends TAttachedDoc implements DoneStateTemplate {
@Prop(TypeString(), 'Title' as IntlString)
title!: string
declare rank: string
}
@Model(task.class.WonStateTemplate, task.class.DoneStateTemplate, DOMAIN_KANBAN)
@@ -170,9 +179,6 @@ export class TKanbanTemplate extends TDoc implements KanbanTemplate {
@Prop(TypeString(), 'Title' as IntlString)
title!: string
states!: Arr<Ref<StateTemplate>>
doneStates!: Arr<Ref<DoneStateTemplate>>
@Prop(Collection(task.class.StateTemplate), 'States' as IntlString)
statesC!: number
+137 -17
View File
@@ -13,7 +13,7 @@
// limitations under the License.
//
import { Class, Doc, Domain, DOMAIN_TX, Ref, TxCUD, TxOperations } from '@anticrm/core'
import { AttachedDoc, Class, Client, Doc, DocWithRank, Domain, DOMAIN_TX, genRanks, Ref, Space, TxCUD, TxOperations } from '@anticrm/core'
import {
MigrateOperation,
MigrateUpdate,
@@ -22,7 +22,7 @@ import {
MigrationUpgradeClient
} from '@anticrm/model'
import core from '@anticrm/model-core'
import { createProjectKanban } from '@anticrm/task'
import { createProjectKanban, KanbanTemplate } from '@anticrm/task'
import { DOMAIN_TASK, DOMAIN_STATE, DOMAIN_KANBAN } from '.'
import task from './plugin'
@@ -134,28 +134,148 @@ export const taskOperation: MigrateOperation = {
console.log('View: Performing model upgrades')
const kanbans = (await client.findAll(task.class.Kanban, {})).filter((kanban) => kanban.doneStates == null)
await createMissingDoneStates(client, ops)
await updateRankItems({ client, ops, _class: task.class.State, extractOrder: (kanban) => kanban.states })
await updateRankItems({ client, ops, _class: task.class.DoneState, extractOrder: (kanban) => kanban.doneStates })
await updateRankItems({ client, ops, _class: task.class.Task, extractOrder: (kanban) => kanban.order })
await updateTemplateRankItems({ client, ops, _class: task.class.StateTemplate, extractOrder: (kanban) => kanban.states })
await updateTemplateRankItems({ client, ops, _class: task.class.DoneStateTemplate, extractOrder: (kanban) => kanban.doneStates })
}
}
await Promise.all(
kanbans.map(async (kanban) => {
console.log(`Updating kanban: ${kanban._id}`)
async function createMissingDoneStates (client: Client, ops: TxOperations): Promise<void> {
const spacesWithStates = await client.findAll(task.class.SpaceWithStates, {})
const doneStates = await client.findAll(task.class.DoneState, {})
const spaceIdsWithDoneStates = new Set(doneStates.map(x => x.space))
const outdatedSpaces = spacesWithStates.filter((space) => !spaceIdsWithDoneStates.has(space._id))
const pairRanks = [...genRanks(2)]
await Promise.all(
outdatedSpaces
.map(async (space) => {
console.log(`Creating done states for space: ${space._id}`)
try {
const doneStates = await Promise.all([
ops.createDoc(task.class.WonState, kanban.space, {
title: 'Won'
await Promise.all([
ops.createDoc(task.class.WonState, space._id, {
title: 'Won',
rank: pairRanks[0]
}),
ops.createDoc(task.class.LostState, kanban.space, {
title: 'Lost'
ops.createDoc(task.class.LostState, space._id, {
title: 'Lost',
rank: pairRanks[1]
})
])
await ops.updateDoc(kanban._class, kanban.space, kanban._id, {
doneStates
})
} catch (e) {
console.error(e)
}
})
)
}))
}
async function updateRankItems<T extends DocWithRank> ({
client,
ops,
_class,
extractOrder
}: {
client: Client
ops: TxOperations
_class: Ref<Class<T>>
extractOrder: (kanban: any) => Ref<T>[]
}): Promise<void> {
const allItems = await client.findAll(_class, {})
const unorderedItems = allItems
.filter((item) => item.rank === undefined)
const groupedUnsortedItems = new Map<Ref<Space>, T[]>()
unorderedItems.forEach((item) => {
const existing = groupedUnsortedItems.get(item.space) ?? []
groupedUnsortedItems.set(item.space, [...existing, item])
})
for (const [space, items] of groupedUnsortedItems.entries()) {
const kanban = await client.findOne(task.class.Kanban, { attachedTo: space })
if (kanban === undefined) {
console.error(`Failed to find kanban attached to space '${space}'`)
continue
}
const order = extractOrder(kanban)
if (order === undefined) {
console.error(`Kanban doesn't contain items order: ${kanban._id}`)
continue
}
const orderedItems = order
.map((id) => items.find(x => x._id === id))
.filter((items): items is T => items !== undefined)
const ranks = genRanks(orderedItems.length)
for (const item of orderedItems) {
const rank = ranks.next().value
if (rank === undefined) {
console.error('Failed to generate rank')
break
}
await ops.updateDoc(item._class as Ref<Class<DocWithRank>>, item.space, item._id, { rank })
}
}
}
async function updateTemplateRankItems<T extends DocWithRank & AttachedDoc> ({
client,
ops,
_class,
extractOrder
}: {
client: Client
ops: TxOperations
_class: Ref<Class<T>>
extractOrder: (kanban: any) => Ref<T>[]
}): Promise<void> {
const allItems = await client.findAll(_class, {})
const unorderedItems = allItems
.filter((state) => state.rank === undefined)
const groupedUnsortedItems = new Map<Ref<Doc>, T[]>()
unorderedItems.forEach((item) => {
const existing = groupedUnsortedItems.get(item.attachedTo) ?? []
groupedUnsortedItems.set(item.attachedTo, [...existing, item])
})
for (const [attachedTo, items] of groupedUnsortedItems.entries()) {
const kanban = await client.findOne(task.class.KanbanTemplate, { _id: attachedTo as Ref<KanbanTemplate> })
if (kanban === undefined) {
console.error(`Failed to find kanban '${attachedTo}'`)
continue
}
const order = extractOrder(kanban)
if (order === undefined) {
console.error(`Kanban doesn't contain items order: ${kanban._id}`)
continue
}
const orderedItems = order
.map((id) => items.find(x => x._id === id))
.filter((items): items is T => items !== undefined)
const ranks = genRanks(orderedItems.length)
for (const item of orderedItems) {
const rank = ranks.next().value
if (rank === undefined) {
console.error('Failed to generate rank')
break
}
await ops.updateDoc(item._class as Ref<Class<DocWithRank>>, item.space, item._id, { rank })
}
}
}