diff --git a/plugins/controlled-documents-resources/src/components/hierarchy/DocumentSpacePresenter.svelte b/plugins/controlled-documents-resources/src/components/hierarchy/DocumentSpacePresenter.svelte index bcbc6a5d82..b35655b010 100644 --- a/plugins/controlled-documents-resources/src/components/hierarchy/DocumentSpacePresenter.svelte +++ b/plugins/controlled-documents-resources/src/components/hierarchy/DocumentSpacePresenter.svelte @@ -116,7 +116,7 @@ } async function selectProject (space: DocumentSpace): Promise { - project = getCurrentProject(space._id) ?? (await getLatestProjectId(space._id)) ?? documents.ids.NoProject + project = getCurrentProject(space._id) ?? (await getLatestProjectId(space._id, true)) ?? documents.ids.NoProject } function handleDocumentSelected (doc: WithLookup): void { diff --git a/plugins/controlled-documents-resources/src/utils.ts b/plugins/controlled-documents-resources/src/utils.ts index 158e88626f..4d71a8fee9 100644 --- a/plugins/controlled-documents-resources/src/utils.ts +++ b/plugins/controlled-documents-resources/src/utils.ts @@ -15,6 +15,7 @@ import core, { type AttachedData, type Class, type Doc, + type DocumentQuery, type Hierarchy, type Ref, type Tx, @@ -689,16 +690,22 @@ export function setCurrentProject (space: Ref, project: Ref): Promise { +async function getLatestProject (space: Ref, includeReadonly = false): Promise { const client = getClient() - return await client.findOne( - documents.class.Project, - { space }, - { limit: 1, sort: { createdOn: SortingOrder.Descending } } - ) + + // TODO we should use better approach on selecting the latest available project + // consider assigning sequential numbers to projects + const query: DocumentQuery = includeReadonly ? { space } : { space, readonly: false } + return await client.findOne(documents.class.Project, query, { + limit: 1, + sort: { createdOn: SortingOrder.Descending } + }) } -export async function getLatestProjectId (spaceRef: Ref): Promise | undefined> { +export async function getLatestProjectId ( + spaceRef: Ref, + includeReadonly = false +): Promise | undefined> { const client = getClient() const space = await client.findOne( documents.class.DocumentSpace, @@ -714,7 +721,7 @@ export async function getLatestProjectId (spaceRef: Ref): Promise if (space !== undefined) { if (space.$lookup?.type?.projects ?? false) { - const project = await getLatestProject(spaceRef) + const project = await getLatestProject(spaceRef, includeReadonly) return project?._id } else { return documents.ids.NoProject