diff --git a/models/tracker/src/index.ts b/models/tracker/src/index.ts index 07dc0019b5..42e6a4f274 100644 --- a/models/tracker/src/index.ts +++ b/models/tracker/src/index.ts @@ -291,7 +291,7 @@ export function createModel (builder: Builder): void { icon: tracker.icon.CategoryBacklog, color: 0, defaultStatusName: 'Backlog', - order: 0 + order: 2 }, tracker.issueStatusCategory.Backlog ) @@ -317,7 +317,7 @@ export function createModel (builder: Builder): void { icon: tracker.icon.CategoryStarted, color: 2, defaultStatusName: 'In Progress', - order: 2 + order: 0 }, tracker.issueStatusCategory.Started ) diff --git a/models/tracker/src/migration.ts b/models/tracker/src/migration.ts index a0887f0d31..cdf2922b75 100644 --- a/models/tracker/src/migration.ts +++ b/models/tracker/src/migration.ts @@ -13,7 +13,7 @@ // limitations under the License. // -import core, { Doc, generateId, Ref, TxOperations } from '@anticrm/core' +import core, { Doc, generateId, Ref, SortingOrder, TxOperations, TxResult } from '@anticrm/core' import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@anticrm/model' import { IssueStatus, IssueStatusCategory, Team, genRanks, Issue } from '@anticrm/tracker' import { DOMAIN_TRACKER } from '.' @@ -80,7 +80,11 @@ async function createDefaultTeam (tx: TxOperations): Promise { // Create new if not deleted by customers. if (current === undefined && currentDeleted === undefined) { const defaultStatusId: Ref = generateId() - const categories = await tx.findAll(tracker.class.IssueStatusCategory, {}) + const categories = await tx.findAll( + tracker.class.IssueStatusCategory, + {}, + { sort: { order: SortingOrder.Ascending } } + ) await tx.createDoc( tracker.class.Team, @@ -102,11 +106,35 @@ async function createDefaultTeam (tx: TxOperations): Promise { } } +async function fixTeamIssueStatusesOrder (tx: TxOperations, team: Team): Promise { + const statuses = await tx.findAll( + tracker.class.IssueStatus, + { attachedTo: team._id }, + { lookup: { category: tracker.class.IssueStatusCategory } } + ) + statuses.sort((a, b) => (a.$lookup?.category?.order ?? 0) - (b.$lookup?.category?.order ?? 0)) + const issueStatusRanks = genRanks(statuses.length) + return statuses.map((status) => { + const rank = issueStatusRanks.next().value + if (rank === undefined || status.rank === rank) return undefined + return tx.update(status, { rank }) + }) +} + +async function fixTeamsIssueStatusesOrder (tx: TxOperations): Promise { + const teams = await tx.findAll(tracker.class.Team, {}) + await Promise.all(teams.map((team) => fixTeamIssueStatusesOrder(tx, team))) +} + async function upgradeTeamIssueStatuses (tx: TxOperations): Promise { const teams = await tx.findAll(tracker.class.Team, { issueStatuses: undefined }) if (teams.length > 0) { - const categories = await tx.findAll(tracker.class.IssueStatusCategory, {}) + const categories = await tx.findAll( + tracker.class.IssueStatusCategory, + {}, + { sort: { order: SortingOrder.Ascending } } + ) for (const team of teams) { const defaultStatusId: Ref = generateId() @@ -232,6 +260,7 @@ async function createDefaults (tx: TxOperations): Promise { async function upgradeTeams (tx: TxOperations): Promise { await upgradeTeamIssueStatuses(tx) + await fixTeamsIssueStatusesOrder(tx) } async function upgradeIssues (tx: TxOperations): Promise { diff --git a/plugins/tracker-resources/src/components/issues/ListView.svelte b/plugins/tracker-resources/src/components/issues/ListView.svelte index 5fa179474e..eae9146eb7 100644 --- a/plugins/tracker-resources/src/components/issues/ListView.svelte +++ b/plugins/tracker-resources/src/components/issues/ListView.svelte @@ -43,7 +43,7 @@ tracker.class.IssueStatus, { attachedTo: currentSpace }, (result) => { - statuses = result + statuses = [...result] }, { lookup: { category: tracker.class.IssueStatusCategory }, diff --git a/plugins/tracker-resources/src/components/issues/StatusEditor.svelte b/plugins/tracker-resources/src/components/issues/StatusEditor.svelte index 5e9815088f..d805ce0255 100644 --- a/plugins/tracker-resources/src/components/issues/StatusEditor.svelte +++ b/plugins/tracker-resources/src/components/issues/StatusEditor.svelte @@ -48,15 +48,17 @@ await client.update(value, { status: newStatus }) } } - $: query = '_id' in value ? { atachedTo: value.space } : {} - client - .findAll(tracker.class.IssueStatus, query, { - lookup: { category: tracker.class.IssueStatusCategory }, - sort: { order: SortingOrder.Ascending } - }) - .then((result) => { - if (!statuses) statuses = result - }) + $: if (!statuses) { + const query = '_id' in value ? { attachedTo: value.space } : {} + client + .findAll(tracker.class.IssueStatus, query, { + lookup: { category: tracker.class.IssueStatusCategory }, + sort: { rank: SortingOrder.Ascending } + }) + .then((result) => { + statuses = result + }) + } {#if value && statuses}