/** * TopUp Dynamic Handler - Minimal Version */ define('walletncredit:views/top-up-dynamic-handler', [], function () { var Handler = function (recordView) { this.recordView = recordView; this.model = recordView.model; }; _.extend(Handler.prototype, { init: function () { console.log('TopUp Dynamic Handler: Minimal version loaded successfully!'); var self = this; // Listen to account changes this.recordView.listenTo(this.model, 'change:accountId', function () { var accountId = self.model.get('accountId'); console.log('TopUp Dynamic Handler: accountId changed to:', accountId); self.handleAccountChange(accountId); }); // Apply initial check after delay with retry setTimeout(function() { self.handleAccountChange(self.model.get('accountId')); }, 1000); // Also listen to when recordView is rendered this.recordView.listenTo(this.recordView, 'after:render', function() { console.log('TopUp Dynamic Handler: RecordView rendered, checking fields...'); setTimeout(function() { self.handleAccountChange(self.model.get('accountId')); }, 200); }); // Global AJAX hook as fallback this.setupGlobalAjaxHook(); }, handleAccountChange: function (accountId) { console.log('TopUp Dynamic Handler: handleAccountChange called with:', accountId); // Debug: Check if recordView is rendered console.log('TopUp Dynamic Handler: RecordView isRendered:', this.recordView.isRendered()); console.log('TopUp Dynamic Handler: RecordView type:', this.recordView.type); // Debug: List all available field views var fieldViews = this.recordView.fieldViews || {}; console.log('TopUp Dynamic Handler: Available field views:', Object.keys(fieldViews)); // Debug: Check if fieldViews exist but empty if (Object.keys(fieldViews).length === 0) { console.log('TopUp Dynamic Handler: fieldViews is empty, checking recordView properties...'); console.log('TopUp Dynamic Handler: RecordView keys:', Object.keys(this.recordView)); } // Try different wallet field names var walletView = this.recordView.getFieldView('wallet') || this.recordView.getFieldView('walletId') || this.recordView.getFieldView('walletName') || this.recordView.getFieldView('Wallet'); console.log('TopUp Dynamic Handler: walletView found:', !!walletView); if (!walletView) { // Try to find any field with 'wallet' in name for (var fieldName in fieldViews) { if (fieldName.toLowerCase().indexOf('wallet') !== -1) { console.log('TopUp Dynamic Handler: Found wallet-related field:', fieldName); walletView = fieldViews[fieldName]; break; } } } if (walletView && accountId) { console.log('TopUp Dynamic Handler: Will apply filter for account:', accountId); this.applyWalletFilter(walletView, accountId); } else if (!walletView) { console.log('TopUp Dynamic Handler: No wallet field found, will retry in 1 second...'); // Retry after 1 second var self = this; setTimeout(function() { console.log('TopUp Dynamic Handler: Retrying field search...'); self.handleAccountChange(accountId); }, 1000); } }, applyWalletFilter: function (walletView, accountId) { console.log('TopUp Dynamic Handler: applyWalletFilter called'); // Try different EspoCRM filter approaches // Approach 1: whereAdditional (EspoCRM standard) walletView.selectFilterData = { whereAdditional: [{ type: 'equals', attribute: 'accountId', value: accountId }] }; // Approach 2: Also try primary filter walletView.selectPrimaryFilter = 'linkedWith'; // Approach 3: Override the select options completely var originalActionSelect = walletView.actionSelect; if (originalActionSelect) { walletView.actionSelect = function() { console.log('TopUp Dynamic Handler: Overriding actionSelect with whereAdditional'); var viewName = 'views/modals/select-records'; var options = { scope: 'Wallet', multiple: false, whereAdditional: [{ type: 'equals', attribute: 'accountId', value: accountId }] }; console.log('TopUp Dynamic Handler: Modal options:', options); this.createView('selectModal', viewName, options, function (view) { // Override getSelectFilters to ensure whereAdditional is applied var originalGetSelectFilters = view.getSelectFilters; view.getSelectFilters = function() { var filters = originalGetSelectFilters ? originalGetSelectFilters.call(this) : {}; // Add whereAdditional to filters filters.whereAdditional = [{ type: 'equals', attribute: 'accountId', value: accountId }]; console.log('TopUp Dynamic Handler: Modal getSelectFilters returning:', filters); return filters; }; // Override collection fetch to add whereAdditional parameter var originalAfterRender = view.afterRender; view.afterRender = function() { if (originalAfterRender) { originalAfterRender.call(this); } // Override collection fetch method if (this.collection && this.collection.fetch) { var originalFetch = this.collection.fetch.bind(this.collection); this.collection.fetch = function(options) { options = options || {}; // Add whereAdditional to fetch options options.whereAdditional = [{ type: 'equals', attribute: 'accountId', value: accountId }]; console.log('TopUp Dynamic Handler: Collection fetch with options:', options); return originalFetch(options); }; console.log('TopUp Dynamic Handler: Collection fetch method overridden'); } }; view.render(); this.listenToOnce(view, 'select', function (model) { console.log('TopUp Dynamic Handler: Selected wallet:', model.get('name')); this.select(model); view.close(); }, this); }, this); }; } console.log('TopUp Dynamic Handler: All filter approaches applied'); }, setupGlobalAjaxHook: function() { var self = this; // Hook 1: jQuery AJAX var originalAjax = $.ajax; $.ajax = function(options) { console.log('TopUp Dynamic Handler: jQuery AJAX called with URL:', options.url); if (options.url && options.url.indexOf('/api/v1/Wallet') !== -1) { var accountId = self.model.get('accountId'); console.log('TopUp Dynamic Handler: jQuery - Wallet request detected, accountId:', accountId); if (accountId) { var separator = options.url.indexOf('?') !== -1 ? '&' : '?'; var whereAdditional = encodeURIComponent(JSON.stringify([{ type: 'equals', attribute: 'accountId', value: accountId }])); options.url += separator + 'whereAdditional=' + whereAdditional; console.log('TopUp Dynamic Handler: jQuery - Modified URL:', options.url); } } return originalAjax.apply(this, arguments); }; // Hook 2: Native fetch API var originalFetch = window.fetch; window.fetch = function(url, options) { console.log('TopUp Dynamic Handler: Fetch called with URL:', url); if (typeof url === 'string' && url.indexOf('/api/v1/Wallet') !== -1) { var accountId = self.model.get('accountId'); console.log('TopUp Dynamic Handler: Fetch - Wallet request detected, accountId:', accountId); if (accountId) { var separator = url.indexOf('?') !== -1 ? '&' : '?'; var whereAdditional = encodeURIComponent(JSON.stringify([{ type: 'equals', attribute: 'accountId', value: accountId }])); url += separator + 'whereAdditional=' + whereAdditional; console.log('TopUp Dynamic Handler: Fetch - Modified URL:', url); } } return originalFetch.apply(this, arguments); }; // Hook 3: XMLHttpRequest var originalOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function(method, url, async, user, password) { console.log('TopUp Dynamic Handler: XHR open called with URL:', url); if (typeof url === 'string' && url.indexOf('/api/v1/Wallet') !== -1) { var accountId = self.model.get('accountId'); console.log('TopUp Dynamic Handler: XHR - Wallet request detected, accountId:', accountId); if (accountId) { var separator = url.indexOf('?') !== -1 ? '&' : '?'; var whereAdditional = encodeURIComponent(JSON.stringify([{ type: 'equals', attribute: 'accountId', value: accountId }])); url += separator + 'whereAdditional=' + whereAdditional; console.log('TopUp Dynamic Handler: XHR - Modified URL:', url); } } return originalOpen.call(this, method, url, async, user, password); }; console.log('TopUp Dynamic Handler: All AJAX hooks installed (jQuery, Fetch, XHR)'); } }); return Handler; });