MNEMO / server /src /utils /cardTree.js
AadityaPawarx1's picture
style: fix image rendering and refine AnswerRenderer logic
bcf42ee
raw
history blame contribute delete
886 Bytes
import { Card } from "../models/Card.js";
import { Entry } from "../models/Entry.js";
export async function getDescendantCardIds(rootId) {
const cards = await Card.find({}, { _id: 1, parentId: 1 }).lean();
const childrenMap = new Map();
cards.forEach((card) => {
const key = String(card.parentId || "");
const current = childrenMap.get(key) || [];
current.push(String(card._id));
childrenMap.set(key, current);
});
const result = [];
const stack = [String(rootId)];
while (stack.length > 0) {
const nextId = stack.pop();
result.push(nextId);
const children = childrenMap.get(nextId) || [];
stack.push(...children);
}
return result;
}
export async function deleteCardCascade(cardId) {
const ids = await getDescendantCardIds(cardId);
await Entry.deleteMany({ cardId: { $in: ids } });
await Card.deleteMany({ _id: { $in: ids } });
}