import { AttachedData, AttachedDoc, Class, Data, Doc, DocumentUpdate, MeasureContext, Ref, Space, TxOperations } from '@hcengineering/core' export async function findOrUpdate ( ctx: MeasureContext, client: TxOperations, space: Ref, _class: Ref>, objectId: Ref, data: Data ): Promise { const existingObj = await client.findOne(_class, { _id: objectId, space }) if (existingObj !== undefined) { await client.updateDoc(_class, space, objectId, data) return false } else { await client.createDoc(_class, space, data, objectId) return true } } export async function findOrUpdateAttached ( ctx: MeasureContext, client: TxOperations, space: Ref, _class: Ref>, objectId: Ref, data: AttachedData, attached: { attachedTo: Ref, attachedClass: Ref>, collection: string } ): Promise { const existingObj = await client.findOne(_class, { _id: objectId, space }) 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 ) } }