import { type AttachedData, type AttachedDoc, type Class, type Doc, type DocumentUpdate, type Ref, type Space, type TxOperations } from '@hcengineering/core' export async function findOrUpdateAttached ( client: TxOperations, space: Ref, _class: Ref>, objectId: Ref, data: AttachedData, attached: { attachedTo: Ref, attachedClass: Ref>, collection: string } ): Promise { let existingObj = (await client.findOne(_class, { _id: objectId, space })) as T if (existingObj !== undefined) { await client.updateCollection( _class, space, objectId, attached.attachedTo, attached.attachedClass, attached.collection, data as unknown as DocumentUpdate ) } else { await client.addCollection( _class, space, attached.attachedTo, attached.attachedClass, attached.collection, data, objectId ) existingObj = { _id: objectId, _class, space, ...data, ...attached } as unknown as T } return existingObj }