Source

services/investigation/delete.js

import { Chat } from "@models/Chat.model";
import { Investigation } from "@models/investigation/investigation.model";
import { InvestigationStatus } from "@typez/investigation";
import { getLogger } from "@utils/asyncLocalStorage";
import { Types } from "mongoose";
/**
 * Deletes the data of a given investigation.
 * @category Services
 * @param {IInvestigation} investigation - The investigation to clear.
 * @returns {IInvestigation} - The investigation object with its values emptied.
 * @throws {Error} - If an error occurs during the deletion process.
 */
export function deleteInvestigationData(investigation) {
    const logger = getLogger();
    try {
        logger.info("Deleting investigation Data.");
        investigation.status = InvestigationStatus.DRAFT_INCOMPLETE;
        investigation.curriculum.value = null;
        investigation.curriculum.aiGeneratedValue = null;
        investigation.unitNumberAndTitle.value = null;
        investigation.unitNumberAndTitle.aiGeneratedValue = null;
        investigation.grade.value = null;
        investigation.grade.aiGeneratedValue = null;
        investigation.lessonNumberAndTitle.value = null;
        investigation.lessonNumberAndTitle.aiGeneratedValue = null;
        investigation.day.value = null;
        investigation.day.aiGeneratedValue = null;
        investigation.title.value = investigation.title.value + " (empty)";
        investigation.title.aiGeneratedValue = investigation.title.aiGeneratedValue + " (empty)";
        investigation.ngss.value = null;
        investigation.ngss.aiGeneratedValue = null;
        investigation.objectives.value = null;
        investigation.objectives.aiGeneratedValue = null;
        investigation.analyticalFacts.value = null;
        investigation.analyticalFacts.aiGeneratedValue = null;
        investigation.goals.value = null;
        investigation.goals.aiGeneratedValue = null;
        investigation.steps = [];
        investigation.objects = [];
        return investigation;
    }
    catch (error) {
        throw error;
    }
}
/**
 * Permanently deletes an investigation and its related chat history.
 * @category Services
 * @param {string} investigationId - The ID of the investigation to delete.
 * @returns {Promise<IDeleteInvestigationByIdResponse>} - An object summarizing the deletion status of the investigation and related chats.
 * @throws {Error} - If an error occurs during the deletion process.
 */
export async function deleteInvestigationById(investigationId) {
    const logger = getLogger();
    try {
        logger.info({ investigationId }, "Deleting investigation and related chat history");
        const objectId = new Types.ObjectId(investigationId);
        const investigation = await Investigation.findById(investigationId);
        if (!investigation) {
            throw new Error("Investigation not found");
        }
        if (investigation.status === InvestigationStatus.IN_DEVELOPMENT) {
            throw new Error("Unable to delete investigation, when its status is InDevelopment.");
        }
        const [deletedInvestigation, chatDeleteResult] = await Promise.all([
            Investigation.findByIdAndDelete(objectId),
            Chat.deleteMany({ investigationId: objectId }),
        ]);
        const deletedChats = chatDeleteResult.deletedCount ?? 0;
        const investigationDeleted = Boolean(deletedInvestigation);
        logger.info({ investigationId, investigationDeleted, deletedChats }, "Deletion completed");
        return {
            investigationDeleted,
            deletedChats,
        };
    }
    catch (error) {
        logger.error({ error, investigationId }, "Failed to delete investigation and chat");
        throw error;
    }
}