From 97662d1dc023f086153422d9cb57928ef7db3bea Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Fri, 10 Apr 2026 01:40:34 -0700 Subject: [PATCH] fix(palace): deleteMemory now decrements wing/room memoryCount --- backend/src/modules/palace/palace.test.ts | 14 ++++++++++++++ backend/src/modules/palace/repository.ts | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/backend/src/modules/palace/palace.test.ts b/backend/src/modules/palace/palace.test.ts index b316a8b..d4a2f05 100644 --- a/backend/src/modules/palace/palace.test.ts +++ b/backend/src/modules/palace/palace.test.ts @@ -198,6 +198,20 @@ describe('Palace Module — Phase N1', () => { expect(memB[0].content).toBe('B secret'); }); + it('deleteMemory decrements wing and room memoryCount', async () => { + const wing = await ensureWing(USER_A, PRODUCT, 'ws-1', 'Work'); + const room = await ensureRoom(USER_A, PRODUCT, wing.id, 'auth'); + + const mem = await storeMemory(USER_A, PRODUCT, wing.id, room.id, 'decisions', 'Fact 1'); + await storeMemory(USER_A, PRODUCT, wing.id, room.id, 'decisions', 'Fact 2'); + + // Delete one memory + await deleteMemory(USER_A, PRODUCT, mem.id); + + const updatedWing = await getWing(USER_A, PRODUCT, wing.id); + expect(updatedWing!.memoryCount).toBe(1); + }); + it('productId scoping: wrong productId returns empty results', async () => { const wing = await ensureWing(USER_A, PRODUCT, 'ws-1', 'Work'); const room = await ensureRoom(USER_A, PRODUCT, wing.id, 'auth'); diff --git a/backend/src/modules/palace/repository.ts b/backend/src/modules/palace/repository.ts index 672b2f6..5e84743 100644 --- a/backend/src/modules/palace/repository.ts +++ b/backend/src/modules/palace/repository.ts @@ -261,9 +261,27 @@ export async function deleteMemory( const doc = await memoriesCollection().findById(memoryId, userId); if (!doc || doc.productId !== productId) return false; await memoriesCollection().delete(memoryId, userId); + await decrementMemoryCount(userId, doc.wingId, doc.roomId); return true; } +async function decrementMemoryCount(userId: string, wingId: string, roomId: string): Promise { + const wing = await wingsCollection().findById(wingId, userId); + if (wing && (wing.memoryCount || 0) > 0) { + await wingsCollection().update(wingId, userId, { + memoryCount: wing.memoryCount - 1, + updatedAt: new Date().toISOString(), + }); + } + const room = await roomsCollection().findById(roomId, userId); + if (room && (room.memoryCount || 0) > 0) { + await roomsCollection().update(roomId, userId, { + memoryCount: room.memoryCount - 1, + updatedAt: new Date().toISOString(), + }); + } +} + export async function listMemories( userId: string, productId: string,