diff --git a/packages/server/src/models/UserItemModel.ts b/packages/server/src/models/UserItemModel.ts index 4d417b9f3..6a22a5fe9 100644 --- a/packages/server/src/models/UserItemModel.ts +++ b/packages/server/src/models/UserItemModel.ts @@ -114,6 +114,9 @@ export default class UserItemModel extends BaseModel { public async add(userId: Uuid, itemId: Uuid, options: SaveOptions = {}): Promise { const item = await this.models().item().load(itemId, { fields: ['id', 'name'] }); + if (!item) { + throw new ErrorNotFound(`No such item: ${itemId}`); + } await this.addMulti(userId, [item], options); } diff --git a/packages/server/src/models/UserItemModels.test.ts b/packages/server/src/models/UserItemModels.test.ts new file mode 100644 index 000000000..ab30a13fd --- /dev/null +++ b/packages/server/src/models/UserItemModels.test.ts @@ -0,0 +1,23 @@ +import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../utils/testing/testUtils'; + +describe('UserItemModel', () => { + + beforeAll(async () => { + await beforeAllDb('UserItemModel'); + }); + + afterAll(async () => { + await afterAllTests(); + }); + + beforeEach(async () => { + await beforeEachDb(); + }); + + test('should throw error if item does not exist', async () => { + const mockUserId = 'not-a-real-user-id'; + const mockId = 'not-a-real-item-id'; + expect(async () => models().userItem().add(mockUserId, mockId)).rejects.toThrow('No such item: not-a-real-item-id'); + }); +}); +