Add Cache Reset Actions

- Add configuration file for module actions that should reset cache
- Expose cache reset actions in the system config api
- Add route event listener to reset cache when navigating a way from defined cache reset actions
- Update unit tests
This commit is contained in:
Clemente Raposo 2020-06-25 12:54:03 +01:00 committed by Dillon-Brown
parent b52bbf6a6d
commit ac0b0eb1b4
6 changed files with 76 additions and 9 deletions

View file

@ -33,6 +33,7 @@ services:
$themeImageSupportedTypes: '%themes.image_supported_types%' $themeImageSupportedTypes: '%themes.image_supported_types%'
$frontendExcludedModules: '%legacy.frontend_excluded_modules%' $frontendExcludedModules: '%legacy.frontend_excluded_modules%'
$datetimeFormatMap: '%legacy.datetime_format_map%' $datetimeFormatMap: '%legacy.datetime_format_map%'
$cacheResetActions: '%legacy.cache_reset_actions%'
_instanceof: _instanceof:
App\Service\ProcessHandlerInterface: App\Service\ProcessHandlerInterface:
tags: ['app.process.handler'] tags: ['app.process.handler']

View file

@ -0,0 +1,4 @@
parameters:
legacy.cache_reset_actions:
users:
- edit

View file

@ -16,3 +16,4 @@ parameters:
timef: true timef: true
currency: true currency: true
list_max_entries_per_page: true list_max_entries_per_page: true
cache_reset_actions: true

View file

@ -1,27 +1,33 @@
import {Component, ViewChild, ViewContainerRef, OnInit} from '@angular/core'; import {Component, ViewChild, ViewContainerRef} from '@angular/core';
import {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router} from '@angular/router'; import {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router} from '@angular/router';
import {AppState, AppStateStore} from '@store/app-state/app-state.store'; import {AppState, AppStateStore} from '@store/app-state/app-state.store';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {StateManager} from '@store/state-manager';
import {SystemConfigStore} from '@store/system-config/system-config.store';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
templateUrl: './app.component.html' templateUrl: './app.component.html'
}) })
export class AppComponent implements OnInit { export class AppComponent {
@ViewChild('mainOutlet', {read: ViewContainerRef, static: true}) @ViewChild('mainOutlet', {read: ViewContainerRef, static: true})
mainOutlet: ViewContainerRef | undefined; mainOutlet: ViewContainerRef | undefined;
appState$: Observable<AppState> = this.appStateStore.vm$; appState$: Observable<AppState> = this.appStateStore.vm$;
constructor(private router: Router, private appStateStore: AppStateStore) { constructor(
private router: Router,
private appStateStore: AppStateStore,
protected stateManager: StateManager,
protected systemConfigs: SystemConfigStore
) {
router.events.subscribe((routerEvent: Event) => this.checkRouterEvent(routerEvent)); router.events.subscribe((routerEvent: Event) => this.checkRouterEvent(routerEvent));
} }
ngOnInit() { protected checkRouterEvent(routerEvent: Event): void {
}
private checkRouterEvent(routerEvent: Event) {
if (routerEvent instanceof NavigationStart) { if (routerEvent instanceof NavigationStart) {
this.appStateStore.updateLoading('router-navigation', true); this.appStateStore.updateLoading('router-navigation', true);
this.conditionalCacheReset();
} }
if (routerEvent instanceof NavigationEnd || if (routerEvent instanceof NavigationEnd ||
@ -30,4 +36,27 @@ export class AppComponent implements OnInit {
this.appStateStore.updateLoading('router-navigation', false); this.appStateStore.updateLoading('router-navigation', false);
} }
} }
protected conditionalCacheReset(): void {
const cacheClearActions = this.systemConfigs.getConfigValue('cache_reset_actions');
const previousModule = this.appStateStore.getModule();
const view = this.appStateStore.getView();
if (!cacheClearActions || !previousModule) {
return;
}
const resetCacheActions: string[] = cacheClearActions[previousModule];
if (!resetCacheActions || !resetCacheActions.length) {
return;
}
resetCacheActions.some(action => {
if (action === 'any' || action === view) {
this.stateManager.clearAuthBased();
return true;
}
});
}
} }

View file

@ -47,6 +47,7 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
* @param ClassicViewRoutingExclusionsHandler $exclusionsManager * @param ClassicViewRoutingExclusionsHandler $exclusionsManager
* @param SystemConfigMappers $mappers * @param SystemConfigMappers $mappers
* @param array $systemConfigKeyMap * @param array $systemConfigKeyMap
* @param array $cacheResetActions
*/ */
public function __construct( public function __construct(
string $projectDir, string $projectDir,
@ -59,7 +60,8 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
ModuleNameMapperInterface $moduleNameMapper, ModuleNameMapperInterface $moduleNameMapper,
ClassicViewRoutingExclusionsHandler $exclusionsManager, ClassicViewRoutingExclusionsHandler $exclusionsManager,
SystemConfigMappers $mappers, SystemConfigMappers $mappers,
array $systemConfigKeyMap array $systemConfigKeyMap,
array $cacheResetActions
) { ) {
parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState); parent::__construct($projectDir, $legacyDir, $legacySessionName, $defaultSessionName, $legacyScopeState);
$this->exposedSystemConfigs = $exposedSystemConfigs; $this->exposedSystemConfigs = $exposedSystemConfigs;
@ -67,6 +69,7 @@ class SystemConfigHandler extends LegacyHandler implements SystemConfigProviderI
$this->injectedSystemConfigs['module_name_map'] = $moduleNameMapper->getLegacyToFrontendMap(); $this->injectedSystemConfigs['module_name_map'] = $moduleNameMapper->getLegacyToFrontendMap();
$this->injectedSystemConfigs['action_name_map'] = $actionNameMapper->getMap(); $this->injectedSystemConfigs['action_name_map'] = $actionNameMapper->getMap();
$this->injectedSystemConfigs['classicview_routing_exclusions'] = $exclusionsManager->get(); $this->injectedSystemConfigs['classicview_routing_exclusions'] = $exclusionsManager->get();
$this->injectedSystemConfigs['cache_reset_actions'] = $cacheResetActions;
$this->mappers = $mappers; $this->mappers = $mappers;
$this->systemConfigKeyMap = $systemConfigKeyMap; $this->systemConfigKeyMap = $systemConfigKeyMap;
} }

View file

@ -56,6 +56,7 @@ class SystemConfigHandlerTest extends Unit
'datef' => true, 'datef' => true,
'timef' => true, 'timef' => true,
'currency' => true, 'currency' => true,
'cache_reset_actions' => true
]; ];
$moduleMapper = new ModuleNameMapperHandler( $moduleMapper = new ModuleNameMapperHandler(
@ -130,6 +131,12 @@ class SystemConfigHandlerTest extends Unit
$sugar_config['datef'] = 'm/d/Y'; $sugar_config['datef'] = 'm/d/Y';
$sugar_config['timef'] = 'H:i'; $sugar_config['timef'] = 'H:i';
$cacheResetActions = [
'users' => [
'edit'
]
];
$this->handler = new SystemConfigHandler( $this->handler = new SystemConfigHandler(
$projectDir, $projectDir,
$legacyDir, $legacyDir,
@ -141,7 +148,8 @@ class SystemConfigHandlerTest extends Unit
$moduleMapper, $moduleMapper,
$classicViewExclusionHandler, $classicViewExclusionHandler,
$mappers, $mappers,
$systemConfigKeyMap $systemConfigKeyMap,
$cacheResetActions
); );
} }
@ -293,4 +301,25 @@ class SystemConfigHandlerTest extends Unit
static::assertArrayHasKey('iso4217', $currency); static::assertArrayHasKey('iso4217', $currency);
static::assertNotEmpty($currency['iso4217']); static::assertNotEmpty($currency['iso4217']);
} }
/**
* Test cache reset actions
*/
public function testCacheResetActionsConfig(): void
{
$cacheClearActionsConfig = $this->handler->getSystemConfig('cache_reset_actions');
static::assertNotNull($cacheClearActionsConfig);
static::assertEquals('cache_reset_actions', $cacheClearActionsConfig->getId());
static::assertNull($cacheClearActionsConfig->getValue());
static::assertIsArray($cacheClearActionsConfig->getItems());
static::assertNotEmpty($cacheClearActionsConfig->getItems());
$cacheClearActions = $cacheClearActionsConfig->getItems();
static::assertNotNull($cacheClearActions);
static::assertNotEmpty($cacheClearActions);
static::assertArrayHasKey('users', $cacheClearActions);
static::assertNotEmpty($cacheClearActions['users']);
static::assertContains('edit', $cacheClearActions['users']);
}
} }