Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions plugins/setting-resources/src/__tests__/attributeUsage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Copyright © 2026 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//

import type { AnyAttribute, Class, Doc, Domain, Ref } from '@hcengineering/core'
import { isAttributeUsed } from '../attributeUsage'

function makeClient (domain: Domain | undefined, found: Doc | undefined): any {
return {
getHierarchy: () => ({ findDomain: jest.fn(() => domain) }),
findOne: jest.fn(async () => found)
}
}

const attribute: Pick<AnyAttribute, 'attributeOf' | 'name'> = {
attributeOf: 'test:mixin:Custom' as Ref<Class<Doc>>,
name: 'custom1'
}

describe('isAttributeUsed', () => {
it('queries the attribute class when it has a domain', async () => {
const client = makeClient('task' as Domain, { _id: 'doc1' } as unknown as Doc)
expect(await isAttributeUsed(client, attribute)).toBe(true)
expect(client.findOne).toHaveBeenCalledWith(attribute.attributeOf, { custom1: { $exists: true } })
})

it('returns false when no document has the attribute', async () => {
const client = makeClient('task' as Domain, undefined)
expect(await isAttributeUsed(client, attribute)).toBe(false)
})

it('does not query a domainless class and treats the attribute as used', async () => {
const client = makeClient(undefined, undefined)
expect(await isAttributeUsed(client, attribute)).toBe(true)
expect(client.findOne).not.toHaveBeenCalled()
})
})
34 changes: 34 additions & 0 deletions plugins/setting-resources/src/attributeUsage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright © 2026 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//

import type { AnyAttribute, Client } from '@hcengineering/core'

/**
* Check whether any document has a value for the attribute.
*
* Mixins declared on a domainless class (e.g. a mixin on core.class.Doc) can be
* applied to documents of any domain, so they cannot be queried directly. The
* attribute is treated as used in that case, which keeps its type locked and
* shows the stronger delete confirmation.
*/
export async function isAttributeUsed (
client: Pick<Client, 'findOne' | 'getHierarchy'>,
attribute: Pick<AnyAttribute, 'attributeOf' | 'name'>
): Promise<boolean> {
if (client.getHierarchy().findDomain(attribute.attributeOf) === undefined) {
return true
}
return (await client.findOne(attribute.attributeOf, { [attribute.name]: { $exists: true } })) !== undefined
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import { ObjectPresenter } from '@hcengineering/view-resources'
import { onDestroy } from 'svelte'
import settings from '../plugin'
import { isAttributeUsed } from '../attributeUsage'
import { clearSettingsStore, settingsStore } from '../store'
import ClassAttributesList from './ClassAttributesList.svelte'
import CreateAttribute from './CreateAttribute.svelte'
Expand Down Expand Up @@ -147,7 +148,7 @@
const handleSelect = async (event: CustomEvent): Promise<void> => {
selected = event.detail as AnyAttribute
if (selected?._id != null) {
const exist = (await client.findOne(selected.attributeOf, { [selected.name]: { $exists: true } })) !== undefined
const exist = await isAttributeUsed(client, selected)
$settingsStore = {
id: selected._id,
component: EditAttribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
} from '@hcengineering/ui'
import { getContextActions, SortableList } from '@hcengineering/view-resources'
import settings from '../plugin'
import { isAttributeUsed } from '../attributeUsage'
import ClassAttributeRow from './ClassAttributeRow.svelte'
import { makeRank } from '@hcengineering/rank'
import EditAttribute from './EditAttribute.svelte'
Expand Down Expand Up @@ -133,7 +134,7 @@

async function showMenu (ev: MouseEvent, attribute: AnyAttribute): Promise<void> {
hovered = attribute._id
const exist = (await client.findOne(attribute.attributeOf, { [attribute.name]: { $exists: true } })) !== undefined
const exist = await isAttributeUsed(client, attribute)
const actions: Action[] = [
{
label: presentation.string.Edit,
Expand Down
Loading