2022-02-16 12:02:31 +03:00
|
|
|
import { AnyAttribute, Class, Client, Doc, Ref, TxOperations } from '@anticrm/core'
|
2021-12-30 12:04:32 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface KeyedAttribute {
|
|
|
|
key: string
|
|
|
|
attr: AnyAttribute
|
|
|
|
}
|
|
|
|
|
2022-04-29 08:27:17 +03:00
|
|
|
export async function updateAttribute (
|
|
|
|
client: TxOperations,
|
|
|
|
object: Doc,
|
|
|
|
_class: Ref<Class<Doc>>,
|
|
|
|
attribute: KeyedAttribute,
|
|
|
|
value: any
|
|
|
|
): Promise<void> {
|
2021-12-30 12:04:32 +03:00
|
|
|
const doc = object
|
|
|
|
const attributeKey = attribute.key
|
|
|
|
const attr = attribute.attr
|
|
|
|
if (client.getHierarchy().isMixin(attr.attributeOf)) {
|
|
|
|
await client.updateMixin(doc._id, _class, doc.space, attr.attributeOf, { [attributeKey]: value })
|
|
|
|
} else {
|
2022-01-13 12:06:50 +03:00
|
|
|
await client.update(object, { [attributeKey]: value })
|
2021-12-30 12:04:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttribute (client: Client, object: any, key: KeyedAttribute): any {
|
|
|
|
// Check if attr is mixin and return it's value
|
|
|
|
if (client.getHierarchy().isMixin(key.attr.attributeOf)) {
|
2022-04-29 08:27:17 +03:00
|
|
|
return object[key.attr.attributeOf]?.[key.key]
|
2021-12-30 12:04:32 +03:00
|
|
|
} else {
|
|
|
|
return object[key.key]
|
|
|
|
}
|
|
|
|
}
|