diff --git a/packages/server/src/models/ItemModel.test.ts b/packages/server/src/models/ItemModel.test.ts index cab168100..b4efbfd31 100644 --- a/packages/server/src/models/ItemModel.test.ts +++ b/packages/server/src/models/ItemModel.test.ts @@ -196,6 +196,34 @@ describe('ItemModel', function() { expect((await models().user().load(user3.id)).total_item_size).toBe(totalSize3); }); + test('should update total size when an item is deleted', async function() { + const { user: user1 } = await createUserAndSession(1); + + await createItemTree3(user1.id, '', '', [ + { + id: '000000000000000000000000000000F1', + children: [ + { + id: '00000000000000000000000000000001', + }, + ], + }, + ]); + + const folder1 = await models().item().loadByJopId(user1.id, '000000000000000000000000000000F1'); + const note1 = await models().item().loadByJopId(user1.id, '00000000000000000000000000000001'); + + await models().item().updateTotalSizes(); + + expect((await models().user().load(user1.id)).total_item_size).toBe(folder1.content_size + note1.content_size); + + await models().item().delete(note1.id); + + await models().item().updateTotalSizes(); + + expect((await models().user().load(user1.id)).total_item_size).toBe(folder1.content_size); + }); + test('should include shared items in total size calculation', async function() { const { user: user1, session: session1 } = await createUserAndSession(1); const { user: user2, session: session2 } = await createUserAndSession(2); diff --git a/packages/server/src/models/ItemModel.ts b/packages/server/src/models/ItemModel.ts index c54546cfa..cdf06d0cd 100644 --- a/packages/server/src/models/ItemModel.ts +++ b/packages/server/src/models/ItemModel.ts @@ -623,7 +623,11 @@ export default class ItemModel extends BaseModel { } else { const itemIds: Uuid[] = unique(changes.map(c => c.item_id)); const userItems: UserItem[] = await this.db('user_items').select('user_id').whereIn('item_id', itemIds); - const userIds: Uuid[] = unique(userItems.map(u => u.user_id)); + const userIds: Uuid[] = unique( + userItems + .map(u => u.user_id) + .concat(changes.map(c => c.user_id)) + ); const totalSizes: TotalSizeRow[] = []; for (const userId of userIds) {