fix: move action without alter updatedAt (#235)

* fix: move action without alter updatedAt

* fix: touch updatedAt on scope change

* fix: mysql test
This commit is contained in:
ChengLei Shao 2022-03-13 19:38:27 +08:00 committed by GitHub
parent 9e27e50595
commit cf279409b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 9 deletions

View File

@ -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,

View File

@ -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,
},
);
}
}