2021-12-15 09:04:43 +00:00
|
|
|
|
2021-12-22 09:02:51 +00:00
|
|
|
import { MeasureContext, Ref, TxOperations } from '@anticrm/core'
|
2021-12-21 09:09:46 +00:00
|
|
|
import task, { DoneState, genRanks, Kanban, SpaceWithStates, State } from '@anticrm/task'
|
2021-12-10 09:47:54 +00:00
|
|
|
import { findOrUpdate } from './utils'
|
|
|
|
|
2021-12-22 09:02:51 +00:00
|
|
|
export async function createUpdateSpaceKanban (ctx: MeasureContext, spaceId: Ref<SpaceWithStates>, client: TxOperations): Promise<Ref<State>[]> {
|
2021-12-14 09:24:14 +00:00
|
|
|
const rawStates = [
|
2022-01-14 09:04:56 +00:00
|
|
|
{ color: 9, name: 'Initial' },
|
|
|
|
{ color: 10, name: 'Intermidiate' },
|
|
|
|
{ color: 1, name: 'OverIntermidiate' },
|
|
|
|
{ color: 0, name: 'Done' },
|
|
|
|
{ color: 11, name: 'Invalid' }
|
2021-12-10 09:47:54 +00:00
|
|
|
]
|
2021-12-14 09:24:14 +00:00
|
|
|
const states: Array<Ref<State>> = []
|
2021-12-17 09:08:37 +00:00
|
|
|
const stateRanks = genRanks(rawStates.length)
|
2021-12-14 09:24:14 +00:00
|
|
|
for (const st of rawStates) {
|
2021-12-17 09:08:37 +00:00
|
|
|
const rank = stateRanks.next().value
|
|
|
|
|
|
|
|
if (rank === undefined) {
|
|
|
|
console.error('Failed to generate rank')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:47:54 +00:00
|
|
|
const sid = ('generated-' + spaceId + '.state.' + st.name.toLowerCase().replace(' ', '_')) as Ref<State>
|
2021-12-22 09:02:51 +00:00
|
|
|
|
|
|
|
await ctx.with('find-or-update', {}, (ctx) => findOrUpdate(ctx, client, spaceId, task.class.State,
|
2021-12-10 09:47:54 +00:00
|
|
|
sid,
|
|
|
|
{
|
|
|
|
title: st.name,
|
2021-12-17 09:08:37 +00:00
|
|
|
color: st.color,
|
|
|
|
rank
|
2021-12-10 09:47:54 +00:00
|
|
|
}
|
2021-12-22 09:02:51 +00:00
|
|
|
))
|
2021-12-14 09:24:14 +00:00
|
|
|
states.push(sid)
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:08:37 +00:00
|
|
|
const doneStates = [
|
2021-12-15 09:04:43 +00:00
|
|
|
{ class: task.class.WonState, title: 'Won' },
|
|
|
|
{ class: task.class.LostState, title: 'Lost' }
|
2021-12-14 09:24:14 +00:00
|
|
|
]
|
2021-12-17 09:08:37 +00:00
|
|
|
const doneStateRanks = genRanks(doneStates.length)
|
|
|
|
for (const st of doneStates) {
|
|
|
|
const rank = doneStateRanks.next().value
|
|
|
|
|
|
|
|
if (rank === undefined) {
|
|
|
|
console.error('Failed to generate rank')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-12-14 09:24:14 +00:00
|
|
|
const sid = ('generated-' + spaceId + '.done-state.' + st.title.toLowerCase().replace(' ', '_')) as Ref<DoneState>
|
2021-12-22 09:02:51 +00:00
|
|
|
await ctx.with('gen-done-state', {}, (ctx) => findOrUpdate(ctx, client, spaceId, st.class,
|
2021-12-14 09:24:14 +00:00
|
|
|
sid,
|
|
|
|
{
|
2021-12-17 09:08:37 +00:00
|
|
|
title: st.title,
|
|
|
|
rank
|
2021-12-14 09:24:14 +00:00
|
|
|
}
|
2021-12-22 09:02:51 +00:00
|
|
|
))
|
2021-12-10 09:47:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 09:02:51 +00:00
|
|
|
await ctx.with('create-kanban', {}, (ctx) => findOrUpdate(ctx, client, spaceId, task.class.Kanban,
|
2021-12-10 09:47:54 +00:00
|
|
|
('generated-' + spaceId + '.kanban') as Ref<Kanban>,
|
|
|
|
{
|
2021-12-17 09:08:37 +00:00
|
|
|
attachedTo: spaceId
|
2021-12-10 09:47:54 +00:00
|
|
|
}
|
2021-12-22 09:02:51 +00:00
|
|
|
))
|
2021-12-14 09:24:14 +00:00
|
|
|
return states
|
2021-12-10 09:47:54 +00:00
|
|
|
}
|