2022-09-21 08:08:25 +00:00
|
|
|
import { Class, Data, Doc, DocumentUpdate, Ref, Space, TxOperations } from '@hcengineering/core'
|
2022-02-21 09:11:31 +00:00
|
|
|
import { deepEqual } from 'fast-equals'
|
|
|
|
|
|
|
|
function toUndef (value: any): any {
|
|
|
|
return value === null ? undefined : value
|
|
|
|
}
|
|
|
|
|
|
|
|
function diffAttributes (doc: Data<Doc>, newDoc: Data<Doc>): DocumentUpdate<Doc> {
|
|
|
|
const result: DocumentUpdate<any> = {}
|
|
|
|
const allDocuments = new Map(Object.entries(doc))
|
|
|
|
const newDocuments = new Map(Object.entries(newDoc))
|
|
|
|
|
|
|
|
for (const [key, value] of allDocuments) {
|
|
|
|
const newValue = toUndef(newDocuments.get(key))
|
|
|
|
if (!deepEqual(newValue, toUndef(value))) {
|
|
|
|
// update is required, since values are different
|
|
|
|
result[key] = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const [key, value] of newDocuments) {
|
|
|
|
const oldValue = toUndef(allDocuments.get(key))
|
|
|
|
if (oldValue === undefined && value !== undefined) {
|
|
|
|
// Update with new value.
|
|
|
|
result[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-25 09:02:38 +00:00
|
|
|
* Create or update document if modified only by system account.
|
2022-02-21 09:11:31 +00:00
|
|
|
* @public
|
|
|
|
*/
|
2022-04-29 05:27:17 +00:00
|
|
|
export async function createOrUpdate<T extends Doc> (
|
|
|
|
client: TxOperations,
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
space: Ref<Space>,
|
|
|
|
data: Data<T>,
|
|
|
|
_id: Ref<T>
|
|
|
|
): Promise<void> {
|
2022-02-21 09:11:31 +00:00
|
|
|
const existingDoc = await client.findOne<Doc>(_class, { _id })
|
|
|
|
if (existingDoc !== undefined) {
|
|
|
|
const { _class: _oldClass, _id, space: _oldSpace, modifiedBy, modifiedOn, ...oldData } = existingDoc
|
2022-02-25 09:02:38 +00:00
|
|
|
if (modifiedBy === client.txFactory.account) {
|
|
|
|
const updateOp = diffAttributes(oldData, data)
|
|
|
|
if (Object.keys(updateOp).length > 0) {
|
|
|
|
await client.update(existingDoc, updateOp)
|
|
|
|
}
|
2022-02-21 09:11:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await client.createDoc<T>(_class, space, data, _id)
|
|
|
|
}
|
|
|
|
}
|