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:
parent
9e27e50595
commit
cf279409b4
@ -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,
|
||||
|
@ -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({
|
||||
await sourceInstance.update(
|
||||
{
|
||||
[fieldName]: targetSort,
|
||||
});
|
||||
},
|
||||
{
|
||||
silent: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user