Source

services/investigation/contradictions.js

import { getLogger } from "@utils/asyncLocalStorage";
/**
 * Adds a contradiction to a specific field in an investigation.
 * @category Services
 * @param {Partial<IInvestigation>} investigation - The investigation object to update.
 * @param {IContradictionInput} contradiction - The details of the contradiction to add.
 * @returns {Partial<IInvestigation>} - The investigation object with the updated contradiction.
 * @throws {Error} - If the field is not found in the investigation.
 */
export function addContradictionInPlace(investigation, contradiction) {
    const logger = getLogger();
    try {
        logger.info({ contradiction }, "Adding contradiction to investigation field");
        // Get the field to update - properly typed as IEditableField
        const fieldToUpdate = investigation[contradiction.fieldName];
        if (!fieldToUpdate) {
            logger.warn({ fieldName: contradiction.fieldName }, "Field not found in investigation");
            throw new Error(`Field '${contradiction.fieldName}' not found in investigation`);
        }
        // Update the field in place with contradiction information
        fieldToUpdate.isContradicting = true;
        fieldToUpdate.targetFieldName = contradiction.targetFieldName;
        fieldToUpdate.contradictionReason = contradiction.contradictionReasoning;
        logger.info({
            fieldName: contradiction.fieldName,
            targetFieldName: contradiction.targetFieldName,
        }, "Contradiction added successfully");
        // Return the updated investigation (mutated in place)
        return investigation;
    }
    catch (error) {
        logger.error({ error, contradiction }, "Failed to add contradiction to investigation");
        throw new Error("Failed to add contradiction to investigation");
    }
}