Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov
2023-09-04 23:06:34 +06:00
committed by GitHub
parent a2388cd1d4
commit 2422baa108
97 changed files with 1678 additions and 1268 deletions
+10 -97
View File
@@ -25,15 +25,13 @@ import {
Ref,
Space,
Status,
Timestamp,
TxOperations
Timestamp
} from '@hcengineering/core'
import { NotificationType } from '@hcengineering/notification'
import type { Asset, IntlString, Plugin } from '@hcengineering/platform'
import { plugin } from '@hcengineering/platform'
import type { AnyComponent } from '@hcengineering/ui'
import { Action, ViewletDescriptor } from '@hcengineering/view'
import { genRanks } from './utils'
/**
* @public
@@ -47,6 +45,8 @@ export interface DocWithRank extends Doc {
*/
export interface SpaceWithStates extends Space {
templateId?: Ref<KanbanTemplate>
states: Ref<State>[]
doneStates?: Ref<DoneState>[]
}
// S T A T E
@@ -61,9 +61,7 @@ export interface State extends Status {
/**
* @public
*/
export interface DoneState extends Status {
name: string
}
export interface DoneState extends Status {}
/**
* @public
@@ -108,13 +106,6 @@ export interface KanbanCard extends Class<Doc> {
card: AnyComponent
}
/**
* @public
*/
export interface Kanban extends Doc {
attachedTo: Ref<Space>
}
/**
* @public
*/
@@ -128,6 +119,7 @@ export interface Sequence extends Doc {
*/
export interface StateTemplate extends Doc, State {
attachedTo: Ref<KanbanTemplate>
rank: string
}
/**
@@ -135,6 +127,7 @@ export interface StateTemplate extends Doc, State {
*/
export interface DoneStateTemplate extends Doc, DoneState {
attachedTo: Ref<KanbanTemplate>
rank: string
}
/**
@@ -248,7 +241,6 @@ const task = plugin(taskId, {
LostState: '' as Ref<Class<LostState>>,
SpaceWithStates: '' as Ref<Class<SpaceWithStates>>,
Task: '' as Ref<Class<Task>>,
Kanban: '' as Ref<Class<Kanban>>,
Sequence: '' as Ref<Class<Sequence>>,
StateTemplate: '' as Ref<Class<StateTemplate>>,
DoneStateTemplate: '' as Ref<Class<DoneStateTemplate>>,
@@ -277,13 +269,15 @@ const task = plugin(taskId, {
Task: '' as Ref<Task>
},
space: {
Sequence: '' as Ref<Space>
Sequence: '' as Ref<Space>,
Statuses: '' as Ref<Space>
},
component: {
KanbanTemplateEditor: '' as AnyComponent,
KanbanTemplateSelector: '' as AnyComponent,
TodoItemsPopup: '' as AnyComponent,
CreateStatePopup: '' as AnyComponent
CreateStatePopup: '' as AnyComponent,
CreateStateTemplatePopup: '' as AnyComponent
},
ids: {
AssigneedNotification: '' as Ref<NotificationType>
@@ -292,84 +286,3 @@ const task = plugin(taskId, {
export default task
export * from './utils'
/**
* @public
*/
export async function createKanban (
client: TxOperations,
attachedTo: Ref<Space>,
templateId?: Ref<KanbanTemplate>
): Promise<Ref<Kanban>> {
if (templateId === undefined) {
await client.createDoc(task.class.State, attachedTo, {
ofAttribute: task.attribute.State,
name: 'New State',
color: 9,
rank: [...genRanks(1)][0]
})
const ranks = [...genRanks(2)]
await Promise.all([
client.createDoc(task.class.WonState, attachedTo, {
ofAttribute: task.attribute.DoneState,
name: 'Won',
rank: ranks[0]
}),
client.createDoc(task.class.LostState, attachedTo, {
ofAttribute: task.attribute.DoneState,
name: 'Lost',
rank: ranks[1]
})
])
return await client.createDoc(task.class.Kanban, attachedTo, {
attachedTo
})
}
const template = await client.findOne(task.class.KanbanTemplate, { _id: templateId })
if (template === undefined) {
throw Error(`Failed to find target kanban template: ${templateId}`)
}
const tmplStates = await client.findAll(task.class.StateTemplate, { attachedTo: template._id })
await Promise.all(
tmplStates.map(
async (state) =>
await client.createDoc(task.class.State, attachedTo, {
ofAttribute: task.attribute.State,
color: state.color,
description: state.description,
name: state.name,
rank: state.rank
})
)
)
const doneClassMap = new Map<Ref<Class<DoneStateTemplate>>, Ref<Class<DoneState>>>([
[task.class.WonStateTemplate, task.class.WonState],
[task.class.LostStateTemplate, task.class.LostState]
])
const tmplDoneStates = await client.findAll(task.class.DoneStateTemplate, { attachedTo: template._id })
await Promise.all(
tmplDoneStates.map(async (state) => {
const cl = doneClassMap.get(state._class)
if (cl === undefined) {
return
}
return await client.createDoc(cl, attachedTo, {
ofAttribute: task.attribute.DoneState,
description: state.description,
name: state.name,
rank: state.rank
})
})
)
return await client.createDoc(task.class.Kanban, attachedTo, {
attachedTo
})
}
+123 -1
View File
@@ -13,8 +13,10 @@
// limitations under the License.
//
import { LexoRank, LexoDecimal, LexoNumeralSystem36 } from 'lexorank'
import { Class, Data, DocumentQuery, IdMap, Ref, SortingOrder, Status, TxOperations } from '@hcengineering/core'
import { LexoDecimal, LexoNumeralSystem36, LexoRank } from 'lexorank'
import LexoRankBucket from 'lexorank/lib/lexoRank/lexoRankBucket'
import task, { DoneState, DoneStateTemplate, KanbanTemplate, SpaceWithStates, State } from '.'
/**
* @public
@@ -44,3 +46,123 @@ export const calcRank = (prev?: { rank: string }, next?: { rank: string }): stri
}
return a.between(b).toString()
}
/**
* @public
*/
export function getStates (space: SpaceWithStates | undefined, statusStore: IdMap<Status>): Status[] {
if (space === undefined) {
return []
}
const states = space.states.map((x) => statusStore.get(x) as Status).filter((p) => p !== undefined)
return states
}
/**
* @public
*/
export async function createState<T extends Status> (
client: TxOperations,
_class: Ref<Class<T>>,
data: Data<T>,
_id?: Ref<T>
): Promise<Ref<T>> {
const query: DocumentQuery<Status> = { name: data.name, ofAttribute: data.ofAttribute }
if (data.category !== undefined) {
query.category = data.category
}
const exists = await client.findOne(_class, query)
if (exists !== undefined) {
return exists._id as Ref<T>
}
const res = await client.createDoc(_class, task.space.Statuses, data, _id)
return res
}
/**
* @public
*/
export async function createStates (
client: TxOperations,
templateId?: Ref<KanbanTemplate>
): Promise<[Ref<Status>[], Ref<DoneState>[]]> {
if (templateId === undefined) {
const state = await createState(client, task.class.State, {
ofAttribute: task.attribute.State,
name: 'New State',
color: 9
})
const doneStates: Ref<DoneState>[] = []
doneStates.push(
await createState(client, task.class.WonState, {
ofAttribute: task.attribute.DoneState,
name: 'Won'
})
)
doneStates.push(
await createState(client, task.class.LostState, {
ofAttribute: task.attribute.DoneState,
name: 'Lost'
})
)
return [[state], doneStates]
}
const template = await client.findOne(task.class.KanbanTemplate, { _id: templateId })
if (template === undefined) {
throw Error(`Failed to find target kanban template: ${templateId}`)
}
const states: Ref<State>[] = []
const doneStates: Ref<DoneState>[] = []
const tmplStates = await client.findAll(
task.class.StateTemplate,
{ attachedTo: template._id },
{ sort: { rank: SortingOrder.Ascending } }
)
for (const state of tmplStates) {
states.push(
await createState(client, task.class.State, {
ofAttribute: task.attribute.State,
color: state.color,
description: state.description,
name: state.name
})
)
}
const doneClassMap = new Map<Ref<Class<DoneStateTemplate>>, Ref<Class<DoneState>>>([
[task.class.WonStateTemplate, task.class.WonState],
[task.class.LostStateTemplate, task.class.LostState]
])
const tmplDoneStates = await client.findAll(
task.class.DoneStateTemplate,
{ attachedTo: template._id },
{ sort: { rank: SortingOrder.Ascending } }
)
for (const state of tmplDoneStates) {
const cl = doneClassMap.get(state._class)
if (cl === undefined) {
continue
}
doneStates.push(
await createState(client, cl, {
ofAttribute: task.attribute.DoneState,
description: state.description,
name: state.name
})
)
}
return [states, doneStates]
}