"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLogChanges = exports.createLog = void 0;
const lodash_1 = __importDefault(require("lodash"));
const UserLog_model_1 = require("../models/UserLog.model");
const excludedKeys = ['$set', '$setOnInsert'];
const createLogs = true;
const createLog = async (params) => {
    if (!createLogs) {
        return;
    }
    if (!params.userId) {
        return;
    }
    const newUserLog = new UserLog_model_1.UserLog({
        timestamp: params.timestamp ? params.timestamp : new Date(),
        logType: params.logType,
        userId: params.userId,
        action: params.action,
        details: params.details,
        recordId: params.recordId,
        recordCollection: params.recordCollection,
    });
    try {
        await newUserLog.save();
    }
    catch (e) {
        console.error(`incorrect log`, newUserLog, e);
        throw e;
    }
};
exports.createLog = createLog;
const getLogChanges = (obj1, obj2) => {
    const changedKeys = lodash_1.default.pickBy(obj2, (value, key) => {
        if (excludedKeys.includes(key)) {
            return false;
        }
        const oldValue = obj1[key] ? obj1[key].toString() : obj1[key];
        const newValue = value ? value.toString() : value;
        return oldValue !== newValue;
    });
    return Object.keys(changedKeys).reduce((acc, key) => ({
        ...acc,
        [key]: { oldValue: obj1[key], newValue: obj2[key] },
    }), {});
};
exports.getLogChanges = getLogChanges;
