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
+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]
}