Skip to content

Commit

Permalink
fix(server): addAssets and removeAssets handle duplicate assetIds (im…
Browse files Browse the repository at this point in the history
…mich-app#9436)

* fix(server): addAssets and removeAssets handle duplicate assetIds

* chore(server): Add e2e tests for duplicate album additions and removals
  • Loading branch information
eric-barch authored May 14, 2024
1 parent e479e55 commit 6fd6a8b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions e2e/src/api/specs/album.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,20 @@ describe('/album', () => {
expect(status).toBe(400);
expect(body).toEqual(errorDto.badRequest('Not found or no album.addAsset access'));
});

it('should add duplicate assets only once', async () => {
const asset = await utils.createAsset(user1.accessToken);
const { status, body } = await request(app)
.put(`/album/${user1Albums[0].id}/assets`)
.set('Authorization', `Bearer ${user1.accessToken}`)
.send({ ids: [asset.id, asset.id] });

expect(status).toBe(200);
expect(body).toEqual([
expect.objectContaining({ id: asset.id, success: true }),
expect.objectContaining({ id: asset.id, success: false, error: 'duplicate' }),
]);
});
});

describe('PATCH /album/:id', () => {
Expand Down Expand Up @@ -557,6 +571,19 @@ describe('/album', () => {
expect(status).toBe(400);
expect(body).toEqual(errorDto.badRequest('Not found or no album.removeAsset access'));
});

it('should remove duplicate assets only once', async () => {
const { status, body } = await request(app)
.delete(`/album/${user1Albums[1].id}/assets`)
.set('Authorization', `Bearer ${user1.accessToken}`)
.send({ ids: [user1Asset1.id, user1Asset1.id] });

expect(status).toBe(200);
expect(body).toEqual([
expect.objectContaining({ id: user1Asset1.id, success: true }),
expect.objectContaining({ id: user1Asset1.id, success: false, error: 'not_found' }),
]);
});
});

describe('PUT :id/users', () => {
Expand Down
2 changes: 2 additions & 0 deletions server/src/utils/asset.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const addAssets = async (
continue;
}

existingAssetIds.add(assetId);
results.push({ id: assetId, success: true });
}

Expand Down Expand Up @@ -79,6 +80,7 @@ export const removeAssets = async (
continue;
}

existingAssetIds.delete(assetId);
results.push({ id: assetId, success: true });
}

Expand Down

0 comments on commit 6fd6a8b

Please sign in to comment.