diff --git a/packages/api/src/resolvers/labels/index.ts b/packages/api/src/resolvers/labels/index.ts index 7080eda58..31daf9cb9 100644 --- a/packages/api/src/resolvers/labels/index.ts +++ b/packages/api/src/resolvers/labels/index.ts @@ -465,6 +465,7 @@ export const moveLabelResolver = authorized< } newPosition = afterLabel.position } + const moveUp = newPosition < oldPosition // move label to the new position const updated = await AppDataSource.transaction(async (t) => { @@ -474,10 +475,13 @@ export const moveLabelResolver = authorized< const updated = await t.getRepository(Label).update( { user: { id: uid }, - position: Between(oldPosition, newPosition), + position: Between( + Math.min(newPosition, oldPosition), + Math.max(newPosition, oldPosition) + ), }, { - position: () => `position + ${newPosition < oldPosition ? 1 : -1}`, + position: () => `position + ${moveUp ? 1 : -1}`, } ) if (!updated.affected) { diff --git a/packages/api/test/resolvers/labels.test.ts b/packages/api/test/resolvers/labels.test.ts index 1c9e9310f..89f6516a3 100644 --- a/packages/api/test/resolvers/labels.test.ts +++ b/packages/api/test/resolvers/labels.test.ts @@ -646,7 +646,7 @@ describe('Labels API', () => { }) describe('Move label', () => { - const query = (labelId: string, afterLabelId?: string): string => ` + const query = (labelId: string, afterLabelId: string): string => ` mutation { moveLabel( input: { @@ -691,6 +691,12 @@ describe('Labels API', () => { afterLabelId = labels[4].id }) + after(async () => { + await graphqlRequest(query(labelId, labels[0].id), authToken).expect( + 200 + ) + }) + it('moves label after the pointed label', async () => { const res = await graphqlRequest( query(labelId, afterLabelId), @@ -712,13 +718,28 @@ describe('Labels API', () => { }) }) + context('when afterLabelId is null', () => { + before(() => { + labelId = labels[4].id + }) + + it('moves the label to the top', async () => { + const res = await graphqlRequest(query(labelId, ''), authToken).expect( + 200 + ) + expect(res.body.data.moveLabel.label.position).to.eql(1) + }) + }) + context('when label not exist', () => { before(() => { labelId = generateFakeUuid() }) it('returns error code NOT_FOUND', async () => { - const res = await graphqlRequest(query(labelId), authToken).expect(200) + const res = await graphqlRequest(query(labelId, ''), authToken).expect( + 200 + ) expect(res.body.data.moveLabel.errorCodes).to.eql(['NOT_FOUND']) }) })