UNPKG

cc-core-cli

Version:

Command Line Interface tool for generating project templates for the (Your Platform's Name) platform.

311 lines (268 loc) 7.77 kB
const mongoose = require('mongoose'); const { Schema } = mongoose; // const jest = require('jest-mock'); // ---- Mock BaseSchema ---- class BaseSchema { constructor(definition = {}, options = {}) { this.definition = definition; this.options = options; this._id = new mongoose.Types.ObjectId(); this.id = this._id.toString(); this.created_at = new Date(); this.updated_at = new Date(); this.created_by = 'test-user'; this.updated_by = 'test-user'; this.is_deleted = false; this.is_active = true; } save() { return Promise.resolve(this); } // Add mock schema methods add = jest.fn(); pre = jest.fn().mockReturnThis(); post = jest.fn().mockReturnThis(); plugin = jest.fn().mockReturnThis(); index = jest.fn().mockReturnThis(); set = jest.fn().mockReturnThis(); virtual = jest.fn().mockReturnThis(); path = jest.fn().mockReturnThis(); } // ---- Mock Services & Classes ---- class BasePipelineProvider { constructor() { this._settings = null; } async init(settings) { this._settings = settings; return '[PIPELINE] Initialized'; } async process(data) { return data; } async queue(data) { return data; } } class BaseService { constructor(cls, model) { this.cls = cls; this.model = { create: jest.fn().mockImplementation((data) => { console.log('🔥 MOCK: BBaseService.model.create called:', data); return data; }) }; } static prefix = ''; async find(query) { const result = this.model.find(query); return result.exec ? result.exec() : result; } async findOne(query, field, ignore) { const result = this.model.findOne(query); return result.exec ? result.exec() : result; } async findById(id) { const result = this.model.findById(id); return result.exec ? result.exec() : result; } async findByIdAndUpdate(id, update, options) { const result = this.model.findByIdAndUpdate(id, update, options); return result.exec ? result.exec() : result; } async findOneAndUpdate(query, update, options) { const result = this.model.findOneAndUpdate(query, update, options); return result.exec ? result.exec() : result; } async create(data) { return data; } async update(id, data) { return { _id: id, ...data }; } async delete(id) { return { _id: id, deleted: true }; } } class BaseModuleSettingService { constructor(code, setting, cls) { this.code = code; this.setting = setting; this.cls = cls; } async init() { return 'Initialized'; } async getSetting(key) { // Use the injected setting service if available, otherwise return default data if (this.setting && this.setting.getSetting) { return await this.setting.getSetting(key); } return { transaction: { format: 'TR${ _date("YYMMDD","7h","") }', running: 4, }, }; } async setSetting(data, key) { // Use the injected setting service if available, otherwise return the data if (this.setting && this.setting.setSetting) { return await this.setting.setSetting(data, key); } return data; } async defaultPermission() { return {}; } async setPermission(permission) { return []; } async getModuleConfig() { return { point_expiration_days: 30, birthday_points: 100, signup_points: 50, programs: [ { rule_type: 'PURCHASE', points: 10, min_spend: 100, max_points: 1000 }, { rule_type: 'QR_SCAN', points: 5 }, { rule_type: 'GAME_REWARD', points: 20 }, ], }; } async updateModuleConfig(config) { return config; } async getScheduleConfig() { return {}; } async updateScheduleConfig(config) { return config; } } class BaseController { constructor(service) { this.service = service; } } // ---- Helper Mocks ---- const BaseSchemaFactory = { createForClass: jest.fn((cls) => ({ prototype: new cls(), })), }; const callAddQueueInternal = jest.fn().mockResolvedValue(true); const callCountInternalService = jest.fn().mockResolvedValue(1); const callFindInternalService = jest.fn().mockResolvedValue({}); const callListInternalService = jest.fn().mockResolvedValue({ rows: [] }); const callCreateInternalService = jest.fn().mockResolvedValue({}); const callDeleteInternalService = jest.fn().mockResolvedValue({}); const callBulkDeleteInternalService = jest.fn().mockResolvedValue({}); const callBulkUpdateInternalService = jest.fn().mockResolvedValue({}); const callUpdateInternalService = jest.fn().mockResolvedValue({}); const callInternalService = jest.fn().mockResolvedValue({}); const callGetInternalService = jest.fn(); const transformString = jest.fn((str, data) => str); const executeFunc = jest.fn((str, entity, data) => str); const generateRunningNumber = jest.fn((prefix, template, running) => `${template}${running}`); const loadEntity = jest.fn().mockResolvedValue({ id: 'test-entity' }); const getNextExecuteTime = jest.fn(() => new Date()); const transform = jest.fn((data) => data); const generateOtp = jest.fn().mockResolvedValue({ ref: 'test-ref', otp: '123456' }); const validateOtp = jest.fn().mockResolvedValue(true); const initQueue = jest.fn().mockResolvedValue(true); const randomString = jest.fn((length) => 'a'.repeat(length || 8)); const convertConditionToMongoQuery = jest.fn((condition) => condition); const addSystemSchedule = jest.fn(); // Basic decorator mock - works for both class and method decorators const Basic = () => { return (target, propertyKey, descriptor) => { // If descriptor is provided, it's a method decorator if (descriptor) { return descriptor; } // Otherwise, it's a class decorator return target; }; }; // Public decorator mock const Public = () => { return (target, propertyKey, descriptor) => { // If descriptor is provided, it's a method decorator if (descriptor) { return descriptor; } // Otherwise, it's a class decorator return target; }; }; class EntitiesService { refreshSystemSchema = jest.fn().mockResolvedValue(true); } const SettingService = jest.fn(() => ({ getSetting: jest.fn().mockResolvedValue({}), setSetting: jest.fn().mockResolvedValue({}), })); const UserPermission = { creator: 'creator', viewer: 'viewer', access_denied: 'access_denied', }; // Mock LayoutEntity (interface, so just an empty object) const LayoutEntity = {}; // Mock WorkflowEntity (interface, so just an empty object) const WorkflowEntity = {}; // Mock IModuleSetting (interface, so just an empty object) const IModuleSetting = {}; // Mock PipelineExecuteDto (interface, so just an empty object) const PipelineExecuteDto = {}; // Mock CONST object const CONST = { CUSTOM_PROVIDERS: [], IGNORE_ENTITY: [] }; // ---- Export Everything ---- module.exports = { BaseSchema, BaseService, BaseController, BasePipelineProvider, BaseModuleSettingService, BaseSchemaFactory, callAddQueueInternal, callCountInternalService, callFindInternalService, callListInternalService, callCreateInternalService, callDeleteInternalService, callBulkDeleteInternalService, callBulkUpdateInternalService, callUpdateInternalService, callInternalService, callGetInternalService, loadEntity, getNextExecuteTime, transformString, executeFunc, generateRunningNumber, EntitiesService, SettingService, UserPermission, LayoutEntity, WorkflowEntity, IModuleSetting, PipelineExecuteDto, CONST, transform, generateOtp, validateOtp, initQueue, randomString, Basic, Public, convertConditionToMongoQuery, addSystemSchedule }; // Optional: ESM Compatibility module.exports.default = module.exports;