mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-28 20:45:00 +02:00
UBER-1053 (#3884)
This commit is contained in:
+25
-124
@@ -13,37 +13,27 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import {
|
||||
Attribute,
|
||||
Class,
|
||||
Data,
|
||||
DocumentQuery,
|
||||
IdMap,
|
||||
Ref,
|
||||
SortingOrder,
|
||||
Status,
|
||||
TxOperations
|
||||
} from '@hcengineering/core'
|
||||
import { Class, Data, DocumentQuery, IdMap, Ref, 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 '.'
|
||||
import task, { Project, ProjectType } from '.'
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export const genRanks = (count: number): Generator<string, void, unknown> =>
|
||||
(function * () {
|
||||
const sys = new LexoNumeralSystem36()
|
||||
const base = 36
|
||||
const max = base ** 6
|
||||
const gap = LexoDecimal.parse(Math.trunc(max / (count + 2)).toString(base), sys)
|
||||
let cur = LexoDecimal.parse('0', sys)
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
cur = cur.add(gap)
|
||||
yield new LexoRank(LexoRankBucket.BUCKET_0, cur).toString()
|
||||
}
|
||||
})()
|
||||
export function genRanks (count: number): string[] {
|
||||
const sys = new LexoNumeralSystem36()
|
||||
const base = 36
|
||||
const max = base ** 6
|
||||
const gap = LexoDecimal.parse(Math.trunc(max / (count + 2)).toString(base), sys)
|
||||
let cur = LexoDecimal.parse('0', sys)
|
||||
const res: string[] = []
|
||||
for (let i = 0; i < count; i++) {
|
||||
cur = cur.add(gap)
|
||||
res.push(new LexoRank(LexoRankBucket.BUCKET_0, cur).toString())
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
@@ -60,14 +50,14 @@ export const calcRank = (prev?: { rank: string }, next?: { rank: string }): stri
|
||||
/**
|
||||
* @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
|
||||
export function getStates (space: Project | undefined, types: IdMap<ProjectType>, statuses: IdMap<Status>): Status[] {
|
||||
if (space === undefined) return []
|
||||
return (
|
||||
(types
|
||||
.get(space.type)
|
||||
?.statuses?.map((p) => statuses.get(p._id))
|
||||
?.filter((p) => p !== undefined) as Status[]) ?? []
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,8 +66,7 @@ export function getStates (space: SpaceWithStates | undefined, statusStore: IdMa
|
||||
export async function createState<T extends Status> (
|
||||
client: TxOperations,
|
||||
_class: Ref<Class<T>>,
|
||||
data: Data<T>,
|
||||
_id?: Ref<T>
|
||||
data: Data<T>
|
||||
): Promise<Ref<T>> {
|
||||
const query: DocumentQuery<Status> = { name: data.name, ofAttribute: data.ofAttribute }
|
||||
if (data.category !== undefined) {
|
||||
@@ -87,94 +76,6 @@ export async function createState<T extends Status> (
|
||||
if (exists !== undefined) {
|
||||
return exists._id as Ref<T>
|
||||
}
|
||||
const res = await client.createDoc(_class, task.space.Statuses, data, _id)
|
||||
const res = await client.createDoc(_class, task.space.Statuses, data)
|
||||
return res
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function createStates (
|
||||
client: TxOperations,
|
||||
ofAttribute: Ref<Attribute<Status>>,
|
||||
doneAtrtribute?: Ref<Attribute<DoneState>>,
|
||||
templateId?: Ref<KanbanTemplate>
|
||||
): Promise<[Ref<Status>[], Ref<DoneState>[]]> {
|
||||
if (templateId === undefined) {
|
||||
const state = await createState(client, task.class.State, {
|
||||
ofAttribute,
|
||||
name: 'New State',
|
||||
color: 9
|
||||
})
|
||||
|
||||
const doneStates: Ref<DoneState>[] = []
|
||||
|
||||
doneStates.push(
|
||||
await createState(client, task.class.WonState, {
|
||||
ofAttribute: doneAtrtribute ?? ofAttribute,
|
||||
name: 'Won'
|
||||
})
|
||||
)
|
||||
doneStates.push(
|
||||
await createState(client, task.class.LostState, {
|
||||
ofAttribute: doneAtrtribute ?? ofAttribute,
|
||||
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,
|
||||
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: doneAtrtribute ?? ofAttribute,
|
||||
description: state.description,
|
||||
name: state.name
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
return [states, doneStates]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user