From bebe1d15e5e5a405226d475de2afe2753399fa9d Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Mon, 9 Oct 2023 23:05:23 +0800 Subject: [PATCH] fix: field history with reverse field (#2776) --- .../src/__tests__/fieldsHistory.test.ts | 44 ++++++++++++++++++- .../src/server/plugin.ts | 33 +++++++++----- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-snapshot-field/src/__tests__/fieldsHistory.test.ts b/packages/plugins/@nocobase/plugin-snapshot-field/src/__tests__/fieldsHistory.test.ts index 0317b57581..c237e65c06 100644 --- a/packages/plugins/@nocobase/plugin-snapshot-field/src/__tests__/fieldsHistory.test.ts +++ b/packages/plugins/@nocobase/plugin-snapshot-field/src/__tests__/fieldsHistory.test.ts @@ -17,10 +17,52 @@ describe('actions', () => { }); afterEach(async () => { - await app.cleanDb(); await app.destroy(); }); + it('should not throw error when create field with reverse field', async () => { + const agent = app.agent(); + + await app.db.getRepository('fieldsHistory').create({ + values: { + key: 'testKey', + collectionName: 'targets', + name: 'test', + }, + }); + + await agent.resource('collections').create({ + values: { + name: 'tests', + }, + }); + + await agent.resource('collections').create({ + values: { + name: 'targets', + }, + }); + + const response = await agent.resource('fields').create({ + values: { + type: 'hasMany', + name: 'targets', + collectionName: 'tests', + foreignKey: 'test_id', + onDelete: 'SET NULL', + target: 'targets', + interface: 'o2m', + reverseField: { + interface: 'm2o', + type: 'belongsTo', + name: 'test', + }, + }, + }); + + expect(response.statusCode).toBe(200); + }); + it('fieldsHistory collectionName and name conflict between tables', async () => { const agent = app.agent(); diff --git a/packages/plugins/@nocobase/plugin-snapshot-field/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-snapshot-field/src/server/plugin.ts index 9520f6adfd..31c39f1272 100644 --- a/packages/plugins/@nocobase/plugin-snapshot-field/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-snapshot-field/src/server/plugin.ts @@ -16,6 +16,7 @@ export class SnapshotFieldPlugin extends Plugin { filter: { name: collectionDoc.name, }, + transaction, }); if (existCollection) { @@ -38,20 +39,30 @@ export class SnapshotFieldPlugin extends Plugin { this.app.db.on('collections.afterCreateWithAssociations', collectionHandler); - const fieldHandler = async (model: Model, { transaction }) => { - const fieldDoc = model.get(); + const deleteField = async (field, transaction) => { const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory'); - const existField: Model = await fieldsHistoryRepository.findOne({ - filter: { - name: fieldDoc.name, - collectionName: fieldDoc.collectionName, - }, + + const { name, collectionName } = field; + + await fieldsHistoryRepository.destroy({ + filter: { name, collectionName }, + transaction, }); - if (existField) { - await existField.destroy({ - transaction, - }); + }; + + const fieldHandler = async (model: Model, { transaction }) => { + const fieldsHistoryRepository = this.app.db.getRepository('fieldsHistory'); + + const fieldDoc = model.get(); + + await deleteField(fieldDoc, transaction); + + const reverseField = fieldDoc.reverseField; + + if (reverseField) { + await deleteField(reverseField, transaction); } + await fieldsHistoryRepository.create({ values: JSON.parse(JSON.stringify(fieldDoc)), transaction,