From cf279409b4918b1570a24393150ef32a9e40d5ea Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Sun, 13 Mar 2022 19:38:27 +0800 Subject: [PATCH] fix: move action without alter updatedAt (#235) * fix: move action without alter updatedAt * fix: touch updatedAt on scope change * fix: mysql test --- .../actions/src/__tests__/move-action.test.ts | 62 +++++++++++++++++++ packages/actions/src/actions/move.ts | 38 +++++++++--- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/packages/actions/src/__tests__/move-action.test.ts b/packages/actions/src/__tests__/move-action.test.ts index 6348d31bb..77b03ece3 100644 --- a/packages/actions/src/__tests__/move-action.test.ts +++ b/packages/actions/src/__tests__/move-action.test.ts @@ -198,6 +198,68 @@ describe('sort action', () => { return api.destroy(); }); + it('should not touch updatedAt on no scope change', async () => { + const moveItemId = 1; + const getUpdatedAts = async () => { + return ( + await api.db.getRepository('tests').find({ + order: ['id'], + }) + ).map((item) => item.get('updatedAt')); + }; + + const beforeUpdatedAts = await getUpdatedAts(); + + await api.agent().resource('tests').move({ + sourceId: moveItemId, + targetId: 4, + }); + + const afterUpdatedAts = await getUpdatedAts(); + + expect(afterUpdatedAts).toEqual(beforeUpdatedAts); + }); + + it('should only touch updatedAt on change scope item', async () => { + const moveItemId = 1; + const getUpdatedAts = async () => { + return ( + await api.db.getRepository('tests').find({ + order: ['id'], + filter: { + id: { $ne: moveItemId }, + }, + }) + ).map((item) => item.get('updatedAt')); + }; + + const findMoveItem = async () => { + return await api.db.getRepository('tests').findOne({ + filter: { + id: moveItemId, + }, + }); + }; + + const beforeMoveItem = await findMoveItem(); + + const beforeUpdatedAts = await getUpdatedAts(); + + const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); + await sleep(1000); + + await api.agent().resource('tests').move({ + sourceId: moveItemId, + targetId: 6, + }); + + const afterUpdatedAts = await getUpdatedAts(); + const afterMoveItem = await findMoveItem(); + + expect(afterUpdatedAts).toEqual(beforeUpdatedAts); + expect(beforeMoveItem.get('updatedAt')).not.toEqual(afterMoveItem.get('updatedAt')); + }); + it('targetId/1->6', async () => { await api.agent().resource('tests').move({ sourceId: 1, diff --git a/packages/actions/src/actions/move.ts b/packages/actions/src/actions/move.ts index 290884fce..3f9b1117c 100644 --- a/packages/actions/src/actions/move.ts +++ b/packages/actions/src/actions/move.ts @@ -62,8 +62,9 @@ export class SortAbleCollection { const targetInstance = await this.collection.repository.findById(targetInstanceId); if (this.scopeKey && sourceInstance.get(this.scopeKey) !== targetInstance.get(this.scopeKey)) { - await sourceInstance.set(this.scopeKey, targetInstance.get(this.scopeKey)); - await sourceInstance.save(); + await sourceInstance.update({ + [this.scopeKey]: targetInstance.get(this.scopeKey), + }); } await this.sameScopeMove(sourceInstance, targetInstance, options); @@ -74,8 +75,14 @@ export class SortAbleCollection { const targetScopeValue = targetScope[this.scopeKey]; if (targetScopeValue && sourceInstance.get(this.scopeKey) !== targetScopeValue) { - await sourceInstance.set(this.scopeKey, targetScopeValue); - await sourceInstance.save(); + await sourceInstance.update( + { + [this.scopeKey]: targetScopeValue, + }, + { + silent: true, + }, + ); if (method === 'prepend') { await this.sticky(sourceInstanceId); @@ -85,8 +92,14 @@ export class SortAbleCollection { async sticky(sourceInstanceId: TargetKey) { const sourceInstance = await this.collection.repository.findById(sourceInstanceId); - sourceInstance.set(this.field.get('name'), 0); - await sourceInstance.save(); + await sourceInstance.update( + { + [this.field.get('name')]: 0, + }, + { + silent: true, + }, + ); } async sameScopeMove(sourceInstance: Model, targetInstance: Model, options: MoveOptions) { @@ -126,13 +139,20 @@ export class SortAbleCollection { [Op.eq]: scopeValue, }; } + await this.collection.model.increment(fieldName, { where, by: change, + silent: true, }); - await sourceInstance.update({ - [fieldName]: targetSort, - }); + await sourceInstance.update( + { + [fieldName]: targetSort, + }, + { + silent: true, + }, + ); } }