"use strict";function initModels(e){if(!(e&&e instanceof sequelize_1.Sequelize))throw new Error("Invalid Sequelize instance passed to initModels");const r={},t=path_1.default.basename(__filename),s=isProduction?".js":".ts",i=[];try{!function e(r){fs_1.default.readdirSync(r,{withFileTypes:!0}).forEach(o=>{const n=path_1.default.join(r,o.name);o.isDirectory()?e(n):o.isFile()&&path_1.default.extname(o.name)===s&&o.name!==t&&!o.name.includes("index")&&i.push(n)})}(__dirname);for(const t of i){const s=require(t),i=s.default||s;if(i&&"function"==typeof i.initModel){const s=i.initModel(e),o=s.name;if(!o){console.error(`Model from file ${t} has no modelName set.`);continue}r[o]=s}else console.error(`Model from file ${t} does not have an initModel method or a valid export structure.`)}Object.keys(r).forEach(e=>{const t=r[e];"function"==typeof t.associate&&t.associate(r)})}catch(e){console.error(`Error initializing models: ${e.message}`);throw e}return r}function extractUserIdsFromWhere(e){let r=[];if(e&&e.userId){const t=e.userId;r=Array.isArray(t)?t:[t]}else if(e&&e[sequelize_1.Op.and]){const t=e[sequelize_1.Op.and];for(const e of t)e.userId&&(Array.isArray(e.userId)?r.push(...e.userId):r.push(e.userId))}return[...new Set(r)]}function createUserCacheHooks(e=e=>e.userId){return{afterCreate:async r=>{const t=e(r);await redis.del(`user:${t}:profile`)},afterUpdate:async r=>{const t=e(r);await redis.del(`user:${t}:profile`)},afterDestroy:async r=>{const t=e(r);await redis.del(`user:${t}:profile`)},afterBulkUpdate:async function(r){let t=extractUserIdsFromWhere(r.where);if(!t.length){t=(await this.findAll({where:r.where})).map(r=>e(r))}for(const e of[...new Set(t)])await redis.del(`user:${e}:profile`)},afterBulkDestroy:async function(r){let t=extractUserIdsFromWhere(r.where);if(!t.length){t=(await this.findAll({where:r.where})).map(r=>e(r))}for(const e of[...new Set(t)])await redis.del(`user:${e}:profile`)}}}var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});exports.initModels=initModels;exports.createUserCacheHooks=createUserCacheHooks;const fs_1=__importDefault(require("fs")),path_1=__importDefault(require("path")),sequelize_1=require("sequelize"),redis_1=require("@b/utils/redis"),isProduction="production"===process.env.NODE_ENV,redis=redis_1.RedisSingleton.getInstance();