TSK-467: Throw error when used for AttachedDoc (#2649)

Signed-off-by: Vyacheslav Tumanov <me@slavatumanov.me>
This commit is contained in:
Vyacheslav Tumanov 2023-02-16 21:10:19 +05:00 committed by GitHub
parent 463c0a692a
commit a79c030597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -370,4 +370,27 @@ describe('memdb', () => {
expect(attached).toBeDefined()
expect(Hierarchy.mixinOrClass(attached as Doc)).toEqual(test.mixin.TaskMixinTodos)
})
it('createDoc for AttachedDoc', async () => {
expect.assertions(1)
const { model } = await createModel()
const client = new TxOperations(model, core.account.System)
const spaces = await client.findAll(core.class.Space, {})
const task = await client.createDoc(test.class.Task, spaces[0]._id, {
name: 'TSK1',
number: 1,
state: 0
})
try {
await client.createDoc(test.class.TestMixinTodo, spaces[0]._id, {
text: '',
attachedTo: task,
attachedToClass: test.mixin.TaskMixinTodos,
collection: 'todos'
})
} catch (e) {
expect(e).toEqual(new Error('createDoc cannot be used for objects inherited from AttachedDoc'))
}
})
})

View File

@ -59,6 +59,10 @@ export class TxOperations implements Omit<Client, 'notify'> {
modifiedOn?: Timestamp,
modifiedBy?: Ref<Account>
): Promise<Ref<T>> {
const hierarchy = this.client.getHierarchy()
if (hierarchy.isDerived(_class, core.class.AttachedDoc)) {
throw new Error('createDoc cannot be used for objects inherited from AttachedDoc')
}
const tx = this.txFactory.createTxCreateDoc(_class, space, attributes, id, modifiedOn, modifiedBy)
await this.client.tx(tx)
return tx.objectId