fix(palace): deleteMemory now decrements wing/room memoryCount

This commit is contained in:
saravanakumardb1 2026-04-10 01:40:34 -07:00
parent 471660b771
commit 97662d1dc0
2 changed files with 32 additions and 0 deletions

View File

@ -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');

View File

@ -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<void> {
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,